/src/Socket/SocketSerialized.cpp

https://github.com/Krozark/cpp-Socket · C++ · 372 lines · 218 code · 28 blank · 126 comment · 22 complexity · a38912f99d7133e91b0a8594cb69b7ae MD5 · raw file

  1. #include <Socket/SocketSerialized.hpp>
  2. #include <Socket/define.hpp>
  3. #include <Socket/Status.hpp>
  4. #include <thread>
  5. namespace ntw {
  6. #define HEADER_SIZE (4+2)
  7. SocketSerialized::SocketSerialized(Socket::Domain dommaine,Socket::Type type,int protocole): Serializer(255), Socket(dommaine,type,protocole), status(Status::ok)
  8. {
  9. //reserver les 2 premier bits pour la taille
  10. _cursor_end =_cursor_begin = HEADER_SIZE;
  11. //is_send=false;
  12. };
  13. SocketSerialized::SocketSerialized(Socket&& s) : Serializer(255), Socket(s.need_connect)
  14. {
  15. std::swap(s.sock,sock);
  16. std::swap(s.sock_cfg,sock_cfg);
  17. _cursor_end =_cursor_begin = HEADER_SIZE;
  18. };
  19. SocketSerialized::~SocketSerialized()
  20. {
  21. }
  22. /*
  23. int SocketSerialized::init_receive()
  24. {
  25. //recuperer la taille dans les 6 premier oct
  26. int res = Socket::receive(_buffer,HEADER_SIZE);
  27. if (res <= 0)
  28. return -1;
  29. int size;
  30. {
  31. //size
  32. uint8_t d[4];
  33. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  34. d[0]= _buffer[0];
  35. d[1]= _buffer[1];
  36. d[2]= _buffer[2];
  37. d[3]= _buffer[3];
  38. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  39. d[3]= _buffer[0];
  40. d[2]= _buffer[1];
  41. d[1]= _buffer[2];
  42. d[0]= _buffer[3];
  43. #else
  44. #error "byte orden not suported (PDP endian)"
  45. #endif
  46. size = (int)(*(uint32_t*)&d);
  47. }
  48. {
  49. //status
  50. uint8_t d[2];
  51. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  52. d[0]= _buffer[4];
  53. d[1]= _buffer[5];
  54. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  55. d[1]= _buffer[4];
  56. d[0]= _buffer[5];
  57. #else
  58. #error "byte orden not suported (PDP endian)"
  59. #endif
  60. status = *(uint16_t*)&d;
  61. }
  62. //reset
  63. if (int(_buffer_size) < (int)HEADER_SIZE+size)
  64. resize(HEADER_SIZE+size);
  65. //else _buffer_size ne change pas
  66. _cursor_begin = HEADER_SIZE;
  67. _cursor_end = HEADER_SIZE+size;
  68. return size;
  69. }
  70. int SocketSerialized::receive()
  71. {
  72. //recuperer la taille dans les 6 premier oct
  73. int res = init_receive();
  74. if(res <0)
  75. {
  76. clear();
  77. setStatus(NTW_STOP_CONNEXION);
  78. }
  79. if (res > 0)
  80. {
  81. int recv_left = res;
  82. int recv = 0;
  83. while(recv_left > 0)
  84. {
  85. recv = Socket::receive(_buffer+res,recv_left);
  86. if(recv <= 0)
  87. //TODO ERROR
  88. break;
  89. res+=recv;
  90. recv_left -=recv;
  91. }
  92. }
  93. //else == 0
  94. return res;
  95. };
  96. int SocketSerialized::receive(Socket& src)
  97. {
  98. //recuperer la taille dans les 6 premier oct
  99. int res = init_receive();
  100. if(res <0)
  101. {
  102. clear();
  103. setStatus(NTW_STOP_CONNEXION);
  104. }
  105. if (res > 0)
  106. {
  107. int recv_left = res;
  108. int recv = 0;
  109. while(recv_left > 0)
  110. {
  111. recv = Socket::receive(_buffer+res,recv_left,0,src);
  112. if(recv <= 0)
  113. //TODO ERROR
  114. break;
  115. res+=recv;
  116. recv_left -=recv;
  117. }
  118. }
  119. //else == 0
  120. return res;
  121. }
  122. */
  123. int SocketSerialized::init_send()
  124. {
  125. int size = _cursor_end - _cursor_begin;
  126. uint8_t *d = (uint8_t *)&size;
  127. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  128. _buffer[_cursor_begin - 6] = d[0];
  129. _buffer[_cursor_begin - 5] = d[1];
  130. _buffer[_cursor_begin - 4] = d[2];
  131. _buffer[_cursor_begin - 3] = d[3];
  132. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  133. _buffer[_cursor_begin - 6] = d[3];
  134. _buffer[_cursor_begin - 5] = d[2];
  135. _buffer[_cursor_begin - 4] = d[1];
  136. _buffer[_cursor_begin - 3] = d[0];
  137. #else
  138. #error "byte orden not suported (PDP endian)"
  139. #endif
  140. {
  141. uint16_t st = status;
  142. d = (uint8_t*)&st;
  143. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  144. _buffer[_cursor_begin - 2] = d[0];
  145. _buffer[_cursor_begin - 1] = d[1];
  146. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  147. _buffer[_cursor_begin - 2] = d[1];
  148. _buffer[_cursor_begin - 1] = d[0];
  149. #else
  150. #error "byte orden not suported (PDP endian)"
  151. #endif
  152. }
  153. return size;
  154. }
  155. int SocketSerialized::send()
  156. {
  157. int size = init_send();
  158. //envoyer
  159. int res;
  160. try
  161. {
  162. res = Socket::send(_buffer+_cursor_begin-(int)HEADER_SIZE, (int)HEADER_SIZE+size);
  163. }
  164. catch (ntw::SocketExeption& e)
  165. {
  166. res = Status::stop;
  167. }
  168. //reset
  169. //clear();
  170. return res;
  171. };
  172. int SocketSerialized::send(const Socket& dest)
  173. {
  174. int size = init_send();
  175. //envoyer
  176. int res;
  177. try
  178. {
  179. res = Socket::send(_buffer+_cursor_begin-(int)HEADER_SIZE, (int)HEADER_SIZE+size,0,dest);
  180. }
  181. catch (ntw::SocketExeption& e)
  182. {
  183. res = Status::stop;
  184. }
  185. //reset
  186. //clear();
  187. return res;
  188. }
  189. int SocketSerialized::init_receive(uint32_t* size,int flags)
  190. {
  191. int res = Socket::receive(_buffer,HEADER_SIZE,flags);
  192. if (res > 0)
  193. {
  194. {
  195. uint8_t d[4];
  196. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  197. d[0]= _buffer[0];
  198. d[1]= _buffer[1];
  199. d[2]= _buffer[2];
  200. d[3]= _buffer[3];
  201. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  202. d[3]= _buffer[0];
  203. d[2]= _buffer[1];
  204. d[1]= _buffer[2];
  205. d[0]= _buffer[3];
  206. #else
  207. #error "byte orden not suported (PDP endian)"
  208. #endif
  209. *size = *(uint32_t*)&d;
  210. }
  211. {
  212. uint8_t d[2];
  213. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  214. d[0]= _buffer[4];
  215. d[1]= _buffer[5];
  216. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  217. d[1]= _buffer[4];
  218. d[0]= _buffer[5];
  219. #else
  220. #error "byte orden not suported (PDP endian)"
  221. #endif
  222. status = *(uint16_t*)&d;
  223. }
  224. //reset
  225. if (int(_buffer_size) < (int)HEADER_SIZE+(int)*size)
  226. resize((int)HEADER_SIZE+(int)*size);
  227. //else _buffer_size ne change pas
  228. _cursor_begin = (int)HEADER_SIZE;
  229. _cursor_end = (int)HEADER_SIZE+(int)*size;
  230. }
  231. else
  232. {
  233. clear();
  234. setStatus(Status::stop);
  235. }
  236. return res;
  237. }
  238. int SocketSerialized::receive()
  239. {
  240. //recuperer la taille dans les 6 premier oct
  241. uint32_t size = 0;
  242. int res;
  243. if(need_connect)
  244. {
  245. res = init_receive(&size);
  246. if(res > 0 and size>0)
  247. {
  248. int recv_left = size;
  249. int recv = 0;
  250. while(recv_left > 0)
  251. {
  252. recv = Socket::receive(_buffer+res,recv_left);
  253. if(recv<=0)
  254. //TODO ERROR
  255. break;
  256. res+=recv;
  257. recv_left -=recv;
  258. }
  259. }
  260. }
  261. else
  262. {
  263. res = init_receive(&size,MSG_PEEK);
  264. if(res > 0 and size>0)
  265. {
  266. char buffer[HEADER_SIZE + size];
  267. res = Socket::receive(buffer,HEADER_SIZE + size);
  268. //if(res <= 0)
  269. //TODO ERROR
  270. ::memcpy(_buffer+_cursor_begin,buffer+HEADER_SIZE,size);
  271. }
  272. }
  273. return res;
  274. };
  275. int SocketSerialized::receive(Socket& other)
  276. {
  277. //recuperer la taille dans les 6 premier oct
  278. uint32_t size = 0;
  279. int res;
  280. if(need_connect)
  281. {
  282. res = init_receive(&size);
  283. if(res > 0 and size>0)
  284. {
  285. int recv_left = size;
  286. int recv = 0;
  287. while(recv_left > 0)
  288. {
  289. recv = Socket::receive(_buffer+res,recv_left,0,other);
  290. if(recv<=0)
  291. //TODO ERROR
  292. break;
  293. res+=recv;
  294. recv_left -=recv;
  295. }
  296. }
  297. }
  298. else
  299. {
  300. res = init_receive(&size,MSG_PEEK);
  301. if(res > 0 and size>0)
  302. {
  303. char buffer[HEADER_SIZE + size];
  304. res = Socket::receive(buffer,HEADER_SIZE + size,0,other);
  305. //if(res <= 0)
  306. //TODO ERROR
  307. ::memcpy(_buffer+_cursor_begin,buffer+HEADER_SIZE,size);
  308. }
  309. }
  310. return res;
  311. };
  312. void SocketSerialized::setStatus(short int st)
  313. {
  314. status = st;
  315. }
  316. short int SocketSerialized::getStatus()const
  317. {
  318. return status;
  319. }
  320. void SocketSerialized::clear()
  321. {
  322. Serializer::clear();
  323. _cursor_begin = _cursor_end = HEADER_SIZE;
  324. }
  325. std::ostream& operator<<(std::ostream& output,const SocketSerialized& self)
  326. {
  327. output<<"[id("<<self.id()<<"),size("<<self.size()<<"),status("<<self.status<<")]";
  328. for(unsigned int i=self._cursor_begin; i<self._cursor_end;++i)
  329. {
  330. if(self._buffer[i] < 33 or self._buffer[i] >126)
  331. output<<"<"<<(int)self._buffer[i]<<">";
  332. else
  333. output<<"'"<<(char)self._buffer[i]<<"'";
  334. }
  335. return output;
  336. };
  337. };