connection.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Gearman server and library
  2. * Copyright (C) 2008 Brian Aker, Eric Day
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. #include "server_common.h"
  9. gearman_connection_st *gearman_connection_create(gearman_connection_st *ptr)
  10. {
  11. if (ptr == NULL)
  12. {
  13. ptr= (gearman_connection_st *)malloc(sizeof(gearman_connection_st));
  14. if (!ptr)
  15. return NULL; /* GEARMAN_MEMORY_ALLOCATION_FAILURE */
  16. memset(ptr, 0, sizeof(gearman_connection_st));
  17. ptr->is_allocated= true;
  18. }
  19. else
  20. {
  21. memset(ptr, 0, sizeof(gearman_connection_st));
  22. }
  23. /* TODO Figure out how to handle punting of the NULL here */
  24. assert(gearman_result_create(NULL, &ptr->result));
  25. return ptr;
  26. }
  27. void gearman_connection_free(gearman_connection_st *ptr)
  28. {
  29. gearman_result_free(&ptr->result);
  30. if (ptr->is_allocated)
  31. free(ptr);
  32. }
  33. /*
  34. clone is the destination, while ptr is the structure to clone.
  35. If ptr is NULL the call is the same as if a gearman_create() was
  36. called.
  37. */
  38. gearman_connection_st *gearman_connection_clone(gearman_connection_st *clone, gearman_connection_st *ptr)
  39. {
  40. gearman_connection_st *new_clone;
  41. if (ptr == NULL)
  42. return gearman_connection_create(clone);
  43. if (ptr->is_allocated)
  44. {
  45. WATCHPOINT_ASSERT(0);
  46. return NULL;
  47. }
  48. new_clone= gearman_connection_create(clone);
  49. if (new_clone == NULL)
  50. return NULL;
  51. return new_clone;
  52. }
  53. bool gearman_connection_add_fd(gearman_connection_st *ptr, int fd)
  54. {
  55. int x;
  56. bool was_found;
  57. for (x= 0, was_found= false; x < GEARMAN_CONNECTION_MAX_FDS ; x++)
  58. {
  59. if (ptr->fds[x] == -1)
  60. {
  61. ptr->fds[x]= fd;
  62. was_found= true;
  63. }
  64. }
  65. return was_found;
  66. }
  67. bool gearman_connection_buffered(gearman_connection_st *ptr)
  68. {
  69. /* check something in ptr? do this to suppress compiler warning for now */
  70. ptr= NULL;
  71. return false;
  72. }