packet.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #pragma once
  39. #include "libgearman/magic.h"
  40. struct gearman_universal_st;
  41. /**
  42. * @ingroup gearman_packet
  43. */
  44. struct gearman_packet_st
  45. {
  46. struct Options {
  47. const bool is_allocated;
  48. bool complete;
  49. bool free_data;
  50. Options(bool is_allocted_) :
  51. is_allocated{is_allocted_},
  52. complete{false},
  53. free_data{false}
  54. { }
  55. } options;
  56. enum gearman_magic_t magic;
  57. gearman_command_t command;
  58. uint8_t argc;
  59. size_t args_size;
  60. size_t data_size;
  61. gearman_universal_st *universal;
  62. gearman_packet_st *next;
  63. gearman_packet_st *prev;
  64. char *args;
  65. const void *data;
  66. char *arg[GEARMAN_MAX_COMMAND_ARGS];
  67. size_t arg_size[GEARMAN_MAX_COMMAND_ARGS];
  68. char args_buffer[GEARMAN_ARGS_BUFFER_SIZE];
  69. #ifdef GEARMAN_PACKET_TRACE
  70. uint32_t _id;
  71. #endif
  72. gearman_packet_st(bool is_allocted_= false) :
  73. options{is_allocted_},
  74. magic{GEARMAN_MAGIC_TEXT},
  75. command{GEARMAN_COMMAND_TEXT},
  76. argc{0},
  77. args_size{0},
  78. data_size{0},
  79. universal{0},
  80. next{nullptr},
  81. prev{nullptr},
  82. args{nullptr},
  83. data{nullptr},
  84. arg{},
  85. arg_size{},
  86. args_buffer{}
  87. {
  88. }
  89. ~gearman_packet_st()
  90. {
  91. reset();
  92. }
  93. void free__data();
  94. bool failed() const
  95. {
  96. if (command == GEARMAN_COMMAND_WORK_FAIL)
  97. {
  98. return true;
  99. }
  100. return false;
  101. }
  102. size_t size() const
  103. {
  104. return data_size;
  105. }
  106. const char* value(size_t& length_) const
  107. {
  108. length_= data_size;
  109. if (length_)
  110. {
  111. return (const char*)data;
  112. }
  113. return nullptr;
  114. }
  115. const char* value() const
  116. {
  117. if (data_size)
  118. {
  119. return (const char*)data;
  120. }
  121. return nullptr;
  122. }
  123. void reset();
  124. };