linux/net_socket.cpp
Go to the documentation of this file.
1 /*
2  * RoboPeak Project
3  * HAL Layer - Socket Interface
4  * Copyright 2009 - 2013 RoboPeak Project
5  *
6  * POXIS Implementation
7  */
8 
9 
10 #include "sdkcommon.h"
11 #include "../../hal/socket.h"
12 
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <netinet/tcp.h>
16 #include <arpa/inet.h>
17 #include <netdb.h>
18 
19 namespace rp{ namespace net {
20 
21 
23 {
24  switch (type) {
26  return AF_INET;
28  return AF_INET6;
30  return AF_UNSPEC;
31 
32  default:
33  assert(!"should not reach here");
34  return AF_UNSPEC;
35  }
36 }
37 
38 
40 {
41  _platform_data = reinterpret_cast<void *>(new sockaddr_storage);
42  memset(_platform_data, 0, sizeof(sockaddr_storage));
43 
44  reinterpret_cast<sockaddr_storage *>(_platform_data)->ss_family = AF_INET;
45 }
46 
48 {
49  _platform_data = reinterpret_cast<void *>(new sockaddr_storage);
50  memcpy(_platform_data, src._platform_data, sizeof(sockaddr_storage));
51 }
52 
53 
54 
56 {
57  _platform_data = reinterpret_cast<void *>(new sockaddr_storage);
58  memset(_platform_data, 0, sizeof(sockaddr_storage));
59 
60  // default to ipv4 in case the following operation fails
61  reinterpret_cast<sockaddr_storage *>(_platform_data)->ss_family = AF_INET;
62 
63  setAddressFromString(addrString, type);
64  setPort(port);
65 }
66 
67 SocketAddress::SocketAddress(void * platform_data)
68  : _platform_data(platform_data)
69 {}
70 
72 {
73  memcpy(_platform_data, src._platform_data, sizeof(sockaddr_storage));
74  return *this;
75 }
76 
77 
79 {
80  delete reinterpret_cast<sockaddr_storage *>(_platform_data);
81 }
82 
84 {
85  switch(reinterpret_cast<const sockaddr_storage *>(_platform_data)->ss_family) {
86  case AF_INET:
87  return ADDRESS_TYPE_INET;
88  case AF_INET6:
89  return ADDRESS_TYPE_INET6;
90  default:
91  assert(!"should not reach here");
92  return ADDRESS_TYPE_INET;
93  }
94 }
95 
97 {
98  switch (getAddressType()) {
99  case ADDRESS_TYPE_INET:
100  return (int)ntohs(reinterpret_cast<const sockaddr_in *>(_platform_data)->sin_port);
101  case ADDRESS_TYPE_INET6:
102  return (int)ntohs(reinterpret_cast<const sockaddr_in6 *>(_platform_data)->sin6_port);
103  default:
104  return 0;
105  }
106 }
107 
109 {
110  switch (getAddressType()) {
111  case ADDRESS_TYPE_INET:
112  reinterpret_cast<sockaddr_in *>(_platform_data)->sin_port = htons((short)port);
113  break;
114  case ADDRESS_TYPE_INET6:
115  reinterpret_cast<sockaddr_in6 *>(_platform_data)->sin6_port = htons((short)port);
116  break;
117  default:
118  return RESULT_OPERATION_FAIL;
119  }
120  return RESULT_OK;
121 }
122 
124 {
125  int ans = 0;
126  int prevPort = getPort();
127  switch (type) {
128  case ADDRESS_TYPE_INET:
129  reinterpret_cast<sockaddr_storage *>(_platform_data)->ss_family = AF_INET;
130  ans = inet_pton(AF_INET,
131  address_string,
132  &reinterpret_cast<sockaddr_in *>(_platform_data)->sin_addr);
133  break;
134 
135 
136  case ADDRESS_TYPE_INET6:
137 
138  reinterpret_cast<sockaddr_storage *>(_platform_data)->ss_family = AF_INET6;
139  ans = inet_pton(AF_INET6,
140  address_string,
141  &reinterpret_cast<sockaddr_in6 *>(_platform_data)->sin6_addr);
142  break;
143 
144  default:
145  return RESULT_INVALID_DATA;
146 
147  }
148  setPort(prevPort);
149 
150  return ans<=0?RESULT_INVALID_DATA:RESULT_OK;
151 }
152 
153 
154 u_result SocketAddress::getAddressAsString(char * buffer, size_t buffersize) const
155 {
156  int net_family = reinterpret_cast<const sockaddr_storage *>(_platform_data)->ss_family;
157  const char *ans = NULL;
158  switch (net_family) {
159  case AF_INET:
160  ans = inet_ntop(net_family, &reinterpret_cast<const sockaddr_in *>(_platform_data)->sin_addr,
161  buffer, buffersize);
162  break;
163 
164  case AF_INET6:
165  ans = inet_ntop(net_family, &reinterpret_cast<const sockaddr_in6 *>(_platform_data)->sin6_addr,
166  buffer, buffersize);
167 
168  break;
169  }
170  return ans<=0?RESULT_OPERATION_FAIL:RESULT_OK;
171 }
172 
173 
174 
175 size_t SocketAddress::LoopUpHostName(const char * hostname, const char * sevicename, std::vector<SocketAddress> &addresspool , bool performDNS, SocketAddress::address_type_t type)
176 {
177  struct addrinfo hints;
178  struct addrinfo *result;
179  int ans;
180 
181  memset(&hints, 0, sizeof(struct addrinfo));
182  hints.ai_family = _halAddrTypeToOSType(type);
183  hints.ai_flags = AI_PASSIVE;
184 
185  if (!performDNS) {
186  hints.ai_family |= AI_NUMERICSERV | AI_NUMERICHOST;
187 
188  }
189 
190  ans = getaddrinfo(hostname, sevicename, &hints, &result);
191 
192  addresspool.clear();
193 
194  if (ans != 0) {
195  // hostname loopup failed
196  return 0;
197  }
198 
199 
200  for (struct addrinfo * cursor = result; cursor != NULL; cursor = cursor->ai_next) {
201  if (cursor->ai_family == ADDRESS_TYPE_INET || cursor->ai_family == ADDRESS_TYPE_INET6) {
202  sockaddr_storage * storagebuffer = new sockaddr_storage;
203  assert(sizeof(sockaddr_storage) >= cursor->ai_addrlen);
204  memcpy(storagebuffer, cursor->ai_addr, cursor->ai_addrlen);
205  addresspool.push_back(SocketAddress(storagebuffer));
206  }
207  }
208 
209 
210  freeaddrinfo(result);
211 
212  return addresspool.size();
213 }
214 
215 
216 u_result SocketAddress::getRawAddress(_u8 * buffer, size_t bufferSize) const
217 {
218  switch (getAddressType()) {
219  case ADDRESS_TYPE_INET:
220  if (bufferSize < sizeof(in_addr::s_addr)) return RESULT_INSUFFICIENT_MEMORY;
221 
222  memcpy(buffer, &reinterpret_cast<const sockaddr_in *>(_platform_data)->sin_addr.s_addr, sizeof(reinterpret_cast<const sockaddr_in *>(_platform_data)->sin_addr.s_addr));
223 
224 
225  break;
226  case ADDRESS_TYPE_INET6:
227  if (bufferSize < sizeof(in6_addr::s6_addr)) return RESULT_INSUFFICIENT_MEMORY;
228  memcpy(buffer, reinterpret_cast<const sockaddr_in6 *>(_platform_data)->sin6_addr.s6_addr, sizeof(reinterpret_cast<const sockaddr_in6 *>(_platform_data)->sin6_addr.s6_addr));
229 
230  break;
231  default:
232  return RESULT_OPERATION_FAIL;
233  }
234  return RESULT_OK;
235 }
236 
237 
239 {
240 
241  int prevPort = getPort();
242  switch (type) {
243  case ADDRESS_TYPE_INET:
244  {
245  sockaddr_in * addrv4 = reinterpret_cast<sockaddr_in *>(_platform_data);
246  addrv4->sin_family = AF_INET;
247  addrv4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
248  }
249  break;
250  case ADDRESS_TYPE_INET6:
251  {
252  sockaddr_in6 * addrv6 = reinterpret_cast<sockaddr_in6 *>(_platform_data);
253  addrv6->sin6_family = AF_INET6;
254  addrv6->sin6_addr = in6addr_loopback;
255 
256  }
257  break;
258  default:
259  return;
260  }
261 
262  setPort(prevPort);
263 }
264 
266 {
267 
268  int prevPort = getPort();
269  sockaddr_in * addrv4 = reinterpret_cast<sockaddr_in *>(_platform_data);
270  addrv4->sin_family = AF_INET;
271  addrv4->sin_addr.s_addr = htonl(INADDR_BROADCAST);
272  setPort(prevPort);
273 
274 }
275 
277 {
278  int prevPort = getPort();
279  switch (type) {
280  case ADDRESS_TYPE_INET:
281  {
282  sockaddr_in * addrv4 = reinterpret_cast<sockaddr_in *>(_platform_data);
283  addrv4->sin_family = AF_INET;
284  addrv4->sin_addr.s_addr = htonl(INADDR_ANY);
285  }
286  break;
287  case ADDRESS_TYPE_INET6:
288  {
289  sockaddr_in6 * addrv6 = reinterpret_cast<sockaddr_in6 *>(_platform_data);
290  addrv6->sin6_family = AF_INET6;
291  addrv6->sin6_addr = in6addr_any;
292 
293  }
294  break;
295  default:
296  return;
297  }
298 
299  setPort(prevPort);
300 
301 
302 }
303 
304 
305 }}
306 
307 
308 
310 
311 
312 namespace rp { namespace arch { namespace net{
313 
314 using namespace rp::net;
315 
317 {
318 public:
319 
321  : _socket_fd(fd)
322  {
323  assert(fd>=0);
324  int bool_true = 1;
325  ::setsockopt( _socket_fd, SOL_SOCKET, SO_REUSEADDR , (char *)&bool_true, sizeof(bool_true) );
326 
327  enableNoDelay(true);
328  this->setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
329  }
330 
331  virtual ~StreamSocketImpl()
332  {
333  close(_socket_fd);
334  }
335 
336  virtual void dispose()
337  {
338  delete this;
339  }
340 
341 
342  virtual u_result bind(const SocketAddress & localaddr)
343  {
344  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(localaddr.getPlatformData());
345  assert(addr);
346  int ans = ::bind(_socket_fd, addr, sizeof(sockaddr_storage));
347  if (ans) {
348  return RESULT_OPERATION_FAIL;
349  } else {
350  return RESULT_OK;
351  }
352  }
353 
355  {
356  struct sockaddr * addr = reinterpret_cast<struct sockaddr *>( const_cast<void *>(localaddr.getPlatformData())); //donnot do this at home...
357  assert(addr);
358 
359  size_t actualsize = sizeof(sockaddr_storage);
360  int ans = ::getsockname(_socket_fd, addr, (socklen_t*)&actualsize);
361 
362  assert(actualsize <= sizeof(sockaddr_storage));
363  assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
364 
365  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
366  }
367 
369  {
370  int ans;
371  timeval tv;
372  tv.tv_sec = timeout / 1000;
373  tv.tv_usec = (timeout % 1000) * 1000;
374 
375  if (msk & SOCKET_DIR_RD) {
376  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv) );
377  if (ans) return RESULT_OPERATION_FAIL;
378  }
379 
380  if (msk & SOCKET_DIR_WR) {
381  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv) );
382  if (ans) return RESULT_OPERATION_FAIL;
383  }
384 
385  return RESULT_OK;
386  }
387 
388  virtual u_result connect(const SocketAddress & pairAddress)
389  {
390  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(pairAddress.getPlatformData());
391  int ans = ::connect(_socket_fd, addr, sizeof(sockaddr_storage));
392  if (!ans) return RESULT_OK;
393 
394 
395  switch (errno) {
396  case EAFNOSUPPORT:
398 #if 0
399  case EINPROGRESS:
400  return RESULT_OK; //treat async connection as good status
401 #endif
402  case ETIMEDOUT:
404  default:
405  return RESULT_OPERATION_FAIL;
406  }
407  }
408 
409  virtual u_result listen(int backlog)
410  {
411  int ans = ::listen( _socket_fd, backlog);
412 
413  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
414  }
415 
416  virtual StreamSocket * accept(SocketAddress * pairAddress)
417  {
418  size_t addrsize;
419  addrsize = sizeof(sockaddr_storage);
420  int pair_socket = ::accept( _socket_fd, pairAddress?reinterpret_cast<struct sockaddr *>(const_cast<void *>(pairAddress->getPlatformData())):NULL
421  , (socklen_t*)&addrsize);
422 
423  if (pair_socket>=0) {
424  return new StreamSocketImpl(pair_socket);
425  } else {
426  return NULL;
427  }
428  }
429 
431  {
432  return waitforData(timeout);
433  }
434 
435  virtual u_result send(const void * buffer, size_t len)
436  {
437  size_t ans = ::send( _socket_fd, buffer, len, MSG_NOSIGNAL);
438  if (ans == (int)len) {
439  return RESULT_OK;
440  } else {
441  switch (errno) {
442  case EAGAIN:
443 #if EWOULDBLOCK!=EAGAIN
444  case EWOULDBLOCK:
445 #endif
447  default:
448  return RESULT_OPERATION_FAIL;
449  }
450  }
451 
452  }
453 
454 
455  virtual u_result recv(void *buf, size_t len, size_t & recv_len)
456  {
457  size_t ans = ::recv( _socket_fd, buf, len, 0);
458  if (ans == (size_t)-1) {
459  recv_len = 0;
460 
461  switch (errno) {
462  case EAGAIN:
463 #if EWOULDBLOCK!=EAGAIN
464  case EWOULDBLOCK:
465 #endif
467  default:
468  return RESULT_OPERATION_FAIL;
469  }
470 
471 
472 
473  } else {
474  recv_len = ans;
475  return RESULT_OK;
476  }
477  }
478 
479 #if 0
480  virtual u_result recvNoWait(void *buf, size_t len, size_t & recv_len)
481  {
482  size_t ans = ::recv( _socket_fd, buf, len, MSG_DONTWAIT);
483  if (ans == (size_t)-1) {
484  recv_len = 0;
485  if (errno == EAGAIN || errno == EWOULDBLOCK) {
486  return RESULT_OK;
487  } else {
488  return RESULT_OPERATION_FAIL;
489  }
490 
491 
492  } else {
493  recv_len = ans;
494  return RESULT_OK;
495  }
496 
497  }
498 #endif
499 
501  {
502  struct sockaddr * addr = reinterpret_cast<struct sockaddr *>(const_cast<void *>(peerAddr.getPlatformData())); //donnot do this at home...
503  assert(addr);
504  size_t actualsize = sizeof(sockaddr_storage);
505  int ans = ::getpeername(_socket_fd, addr, (socklen_t*)&actualsize);
506 
507  assert(actualsize <= sizeof(sockaddr_storage));
508  assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
509 
510  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
511 
512  }
513 
515  {
516  int shutdw_opt ;
517 
518  switch (mask) {
519  case SOCKET_DIR_RD:
520  shutdw_opt = SHUT_RD;
521  break;
522  case SOCKET_DIR_WR:
523  shutdw_opt = SHUT_WR;
524  break;
525  case SOCKET_DIR_BOTH:
526  default:
527  shutdw_opt = SHUT_RDWR;
528  }
529 
530  int ans = ::shutdown(_socket_fd, shutdw_opt);
531  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
532  }
533 
534  virtual u_result enableKeepAlive(bool enable)
535  {
536  int bool_true = enable?1:0;
537  return ::setsockopt( _socket_fd, SOL_SOCKET, SO_KEEPALIVE , &bool_true, sizeof(bool_true) )?RESULT_OPERATION_FAIL:RESULT_OK;
538  }
539 
540  virtual u_result enableNoDelay(bool enable )
541  {
542  int bool_true = enable?1:0;
543  return ::setsockopt( _socket_fd, IPPROTO_TCP, TCP_NODELAY,&bool_true, sizeof(bool_true) )?RESULT_OPERATION_FAIL:RESULT_OK;
544  }
545 
546  virtual u_result waitforSent(_u32 timeout )
547  {
548  fd_set wrset;
549  FD_ZERO(&wrset);
550  FD_SET(_socket_fd, &wrset);
551 
552  timeval tv;
553  tv.tv_sec = timeout / 1000;
554  tv.tv_usec = (timeout % 1000) * 1000;
555  int ans = ::select(_socket_fd+1, NULL, &wrset, NULL, &tv);
556 
557  switch (ans) {
558  case 1:
559  // fired
560  return RESULT_OK;
561  case 0:
562  // timeout
564  default:
565  delay(0); //relax cpu
566  return RESULT_OPERATION_FAIL;
567  }
568  }
569 
570  virtual u_result waitforData(_u32 timeout )
571  {
572  fd_set rdset;
573  FD_ZERO(&rdset);
574  FD_SET(_socket_fd, &rdset);
575 
576  timeval tv;
577  tv.tv_sec = timeout / 1000;
578  tv.tv_usec = (timeout % 1000) * 1000;
579  int ans = ::select(_socket_fd+1, &rdset, NULL, NULL, &tv);
580 
581  switch (ans) {
582  case 1:
583  // fired
584  return RESULT_OK;
585  case 0:
586  // timeout
588  default:
589  delay(0); //relax cpu
590  return RESULT_OPERATION_FAIL;
591  }
592  }
593 
594 protected:
596 
597 
598 };
599 
600 
602 {
603 public:
604 
606  : _socket_fd(fd)
607  {
608  assert(fd>=0);
609  int bool_true = 1;
610  ::setsockopt( _socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST , (char *)&bool_true, sizeof(bool_true) );
611  setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
612  }
613 
614  virtual ~DGramSocketImpl()
615  {
616  close(_socket_fd);
617  }
618 
619  virtual void dispose()
620  {
621  delete this;
622  }
623 
624 
625  virtual u_result bind(const SocketAddress & localaddr)
626  {
627  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(localaddr.getPlatformData());
628  assert(addr);
629  int ans = ::bind(_socket_fd, addr, sizeof(sockaddr_storage));
630  if (ans) {
631  return RESULT_OPERATION_FAIL;
632  } else {
633  return RESULT_OK;
634  }
635  }
636 
638  {
639  struct sockaddr * addr = reinterpret_cast<struct sockaddr *>(const_cast<void *>((localaddr.getPlatformData()))); //donnot do this at home...
640  assert(addr);
641 
642  size_t actualsize = sizeof(sockaddr_storage);
643  int ans = ::getsockname(_socket_fd, addr, (socklen_t*)&actualsize);
644 
645  assert(actualsize <= sizeof(sockaddr_storage));
646  assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
647 
648  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
649  }
650 
652  {
653  int ans;
654  timeval tv;
655  tv.tv_sec = timeout / 1000;
656  tv.tv_usec = (timeout % 1000) * 1000;
657 
658  if (msk & SOCKET_DIR_RD) {
659  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv) );
660  if (ans) return RESULT_OPERATION_FAIL;
661  }
662 
663  if (msk & SOCKET_DIR_WR) {
664  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv) );
665  if (ans) return RESULT_OPERATION_FAIL;
666  }
667 
668  return RESULT_OK;
669  }
670 
671 
672  virtual u_result waitforSent(_u32 timeout )
673  {
674  fd_set wrset;
675  FD_ZERO(&wrset);
676  FD_SET(_socket_fd, &wrset);
677 
678  timeval tv;
679  tv.tv_sec = timeout / 1000;
680  tv.tv_usec = (timeout % 1000) * 1000;
681  int ans = ::select(_socket_fd+1, NULL, &wrset, NULL, &tv);
682 
683  switch (ans) {
684  case 1:
685  // fired
686  return RESULT_OK;
687  case 0:
688  // timeout
690  default:
691  delay(0); //relax cpu
692  return RESULT_OPERATION_FAIL;
693  }
694  }
695 
696  virtual u_result waitforData(_u32 timeout )
697  {
698  fd_set rdset;
699  FD_ZERO(&rdset);
700  FD_SET(_socket_fd, &rdset);
701 
702  timeval tv;
703  tv.tv_sec = timeout / 1000;
704  tv.tv_usec = (timeout % 1000) * 1000;
705  int ans = ::select(_socket_fd+1, &rdset, NULL, NULL, &tv);
706 
707  switch (ans) {
708  case 1:
709  // fired
710  return RESULT_OK;
711  case 0:
712  // timeout
714  default:
715  delay(0); //relax cpu
716  return RESULT_OPERATION_FAIL;
717  }
718  }
719 
720  virtual u_result sendTo(const SocketAddress & target, const void * buffer, size_t len)
721  {
722  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(target.getPlatformData());
723  assert(addr);
724  size_t ans = ::sendto( _socket_fd, buffer, len, 0, addr, sizeof(sockaddr_storage));
725  if (ans != (size_t)-1) {
726  assert(ans == (int)len);
727  return RESULT_OK;
728  } else {
729  switch (errno) {
730  case EAGAIN:
731 #if EWOULDBLOCK!=EAGAIN
732  case EWOULDBLOCK:
733 #endif
735 
736  case EMSGSIZE:
737  return RESULT_INVALID_DATA;
738  default:
739  return RESULT_OPERATION_FAIL;
740  }
741 
742  }
743 
744  }
745 
746 
747  virtual u_result recvFrom(void *buf, size_t len, size_t & recv_len, SocketAddress * sourceAddr)
748  {
749  struct sockaddr * addr = (sourceAddr?reinterpret_cast<struct sockaddr *>(const_cast<void *>(sourceAddr->getPlatformData())):NULL);
750  size_t source_addr_size = (sourceAddr?sizeof(sockaddr_storage):0);
751 
752  size_t ans = ::recvfrom( _socket_fd, buf, len, 0, addr, (socklen_t*)&source_addr_size);
753  if (ans == (size_t)-1) {
754  recv_len = 0;
755  switch (errno) {
756  case EAGAIN:
757 #if EWOULDBLOCK!=EAGAIN
758  case EWOULDBLOCK:
759 #endif
761  default:
762  return RESULT_OPERATION_FAIL;
763  }
764 
765  } else {
766  recv_len = ans;
767  return RESULT_OK;
768  }
769 
770  }
771 
772 #if 0
773  virtual u_result recvFromNoWait(void *buf, size_t len, size_t & recv_len, SocketAddress * sourceAddr)
774  {
775  struct sockaddr * addr = (sourceAddr?reinterpret_cast<struct sockaddr *>(const_cast<void *>(sourceAddr->getPlatformData())):NULL);
776  size_t source_addr_size = (sourceAddr?sizeof(sockaddr_storage):0);
777 
778 
779  size_t ans = ::recvfrom( _socket_fd, buf, len, MSG_DONTWAIT, addr, &source_addr_size);
780 
781  if (ans == (size_t)-1) {
782  recv_len = 0;
783  if (errno == EAGAIN || errno == EWOULDBLOCK) {
784  return RESULT_OK;
785  } else {
786  return RESULT_OPERATION_FAIL;
787  }
788 
789 
790  } else {
791  recv_len = ans;
792  return RESULT_OK;
793  }
794 
795  }
796 #endif
797 
798 protected:
800 
801 };
802 
803 
804 }}}
805 
806 
807 namespace rp { namespace net{
808 
809 
811 {
812  switch (family) {
814  return AF_INET;
816  return AF_INET6;
818  return AF_PACKET;
819  default:
820  assert(!"should not reach here");
821  return AF_INET; // force treating as IPv4 in release mode
822  }
823 
824 }
825 
827 {
828  if (family == SOCKET_FAMILY_RAW) return NULL;
829 
830 
831  int socket_family = _socketHalFamilyToOSFamily(family);
832  int socket_fd = ::socket(socket_family, SOCK_STREAM, 0);
833  if (socket_fd == -1) return NULL;
834 
835  StreamSocket * newborn = static_cast<StreamSocket *>(new rp::arch::net::StreamSocketImpl(socket_fd));
836  return newborn;
837 
838 }
839 
840 
842 {
843  int socket_family = _socketHalFamilyToOSFamily(family);
844 
845 
846  int socket_fd = ::socket(socket_family, (family==SOCKET_FAMILY_RAW)?SOCK_RAW:SOCK_DGRAM, 0);
847  if (socket_fd == -1) return NULL;
848 
849  DGramSocket * newborn = static_cast<DGramSocket *>(new rp::arch::net::DGramSocketImpl(socket_fd));
850  return newborn;
851 
852 }
853 
854 
855 }}
856 
virtual StreamSocket * accept(SocketAddress *pairAddress)
static int _socketHalFamilyToOSFamily(SocketBase::socket_family_t family)
virtual void setAnyAddress(address_type_t=ADDRESS_TYPE_INET)
virtual u_result setPort(int port)
virtual void setLoopbackAddress(address_type_t=ADDRESS_TYPE_INET)
virtual u_result connect(const SocketAddress &pairAddress)
virtual u_result setTimeout(_u32 timeout, socket_direction_mask msk)
static int _halAddrTypeToOSType(SocketAddress::address_type_t type)
_u32 result
Definition: rplidar_cmd.h:2
virtual void setBroadcastAddressIPv4()
#define RESULT_OK
Definition: rptypes.h:102
virtual u_result recvFrom(void *buf, size_t len, size_t &recv_len, SocketAddress *sourceAddr)
SocketAddress & operator=(const SocketAddress &)
virtual u_result recv(void *buf, size_t len, size_t &recv_len)
virtual u_result getAddressAsString(char *buffer, size_t buffersize) const
virtual u_result waitforData(_u32 timeout)
#define RESULT_INSUFFICIENT_MEMORY
Definition: rptypes.h:111
static size_t LoopUpHostName(const char *hostname, const char *sevicename, std::vector< SocketAddress > &addresspool, bool performDNS=true, address_type_t=ADDRESS_TYPE_INET)
virtual u_result enableKeepAlive(bool enable)
virtual u_result waitforSent(_u32 timeout)
#define delay(x)
Definition: win32/timer.h:39
uint8_t _u8
Definition: rptypes.h:63
virtual u_result setTimeout(_u32 timeout, socket_direction_mask msk)
virtual u_result setAddressFromString(const char *address_string, address_type_t=ADDRESS_TYPE_INET)
const void * getPlatformData() const
Definition: socket.h:47
#define _single_thread
Definition: types.h:84
virtual u_result waitforSent(_u32 timeout)
void * _platform_data
Definition: socket.h:59
virtual u_result sendTo(const SocketAddress &target, const void *buffer, size_t len)
virtual u_result getLocalAddress(SocketAddress &localaddr)
virtual u_result listen(int backlog)
virtual u_result bind(const SocketAddress &localaddr)
static StreamSocket * CreateSocket(socket_family_t family=SOCKET_FAMILY_INET)
#define RESULT_OPERATION_TIMEOUT
Definition: rptypes.h:107
virtual u_result getLocalAddress(SocketAddress &localaddr)
virtual u_result send(const void *buffer, size_t len)
virtual u_result waitforData(_u32 timeout)
virtual address_type_t getAddressType() const
virtual u_result enableNoDelay(bool enable)
_u32 type
Definition: rplidar_cmd.h:2
virtual u_result getPeerAddress(SocketAddress &peerAddr)
virtual u_result shutdown(socket_direction_mask mask)
uint32_t _u32
Definition: rptypes.h:69
#define RESULT_OPERATION_NOT_SUPPORT
Definition: rptypes.h:109
virtual u_result bind(const SocketAddress &localaddr)
#define RESULT_OPERATION_FAIL
Definition: rptypes.h:106
virtual u_result getRawAddress(_u8 *buffer, size_t bufferSize) const
virtual int getPort() const
#define RESULT_INVALID_DATA
Definition: rptypes.h:105
static DGramSocket * CreateSocket(socket_family_t family=SOCKET_FAMILY_INET)
uint32_t u_result
Definition: rptypes.h:100
virtual u_result waitforIncomingConnection(_u32 timeout)


rplidar_ros
Author(s):
autogenerated on Wed Jan 1 2020 04:01:40