macOS/net_socket.cpp
Go to the documentation of this file.
1 /*
2  * RoboPeak Project
3  * HAL Layer - Socket Interface
4  * Copyright 2018 RoboPeak Project
5  *
6  * macOS 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 
55 SocketAddress::SocketAddress(const char * addrString, int port, SocketAddress::address_type_t type)
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 
96 int SocketAddress::getPort() const
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?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_t)) 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)) 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 
316 class _single_thread StreamSocketImpl : public StreamSocket
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  ::setsockopt( _socket_fd, SOL_SOCKET, SO_NOSIGPIPE, (char*)&bool_true, sizeof(bool_true));
327 
328  enableNoDelay(true);
329  this->setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
330  }
331 
332  virtual ~StreamSocketImpl()
333  {
334  close(_socket_fd);
335  }
336 
337  virtual void dispose()
338  {
339  delete this;
340  }
341 
342 
343  virtual u_result bind(const SocketAddress & localaddr)
344  {
345  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(localaddr.getPlatformData());
346  assert(addr);
347  int ans = ::bind(_socket_fd, addr, sizeof(sockaddr_storage));
348  if (ans) {
349  return RESULT_OPERATION_FAIL;
350  } else {
351  return RESULT_OK;
352  }
353  }
354 
356  {
357  struct sockaddr * addr = reinterpret_cast<struct sockaddr *>( const_cast<void *>(localaddr.getPlatformData())); //donnot do this at home...
358  assert(addr);
359 
360  size_t actualsize = sizeof(sockaddr_storage);
361  int ans = ::getsockname(_socket_fd, addr, (socklen_t*)&actualsize);
362 
363  assert(actualsize <= sizeof(sockaddr_storage));
364  assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
365 
366  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
367  }
368 
370  {
371  int ans;
372  timeval tv;
373  tv.tv_sec = timeout / 1000;
374  tv.tv_usec = (timeout % 1000) * 1000;
375 
376  if (msk & SOCKET_DIR_RD) {
377  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv) );
378  if (ans) return RESULT_OPERATION_FAIL;
379  }
380 
381  if (msk & SOCKET_DIR_WR) {
382  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv) );
383  if (ans) return RESULT_OPERATION_FAIL;
384  }
385 
386  return RESULT_OK;
387  }
388 
389  virtual u_result connect(const SocketAddress & pairAddress)
390  {
391  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(pairAddress.getPlatformData());
392  int ans = ::connect(_socket_fd, addr, sizeof(sockaddr_storage));
393  if (!ans) return RESULT_OK;
394 
395 
396  switch (errno) {
397  case EAFNOSUPPORT:
399 #if 0
400  case EINPROGRESS:
401  return RESULT_OK; //treat async connection as good status
402 #endif
403  case ETIMEDOUT:
405  default:
406  return RESULT_OPERATION_FAIL;
407  }
408  }
409 
410  virtual u_result listen(int backlog)
411  {
412  int ans = ::listen( _socket_fd, backlog);
413 
414  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
415  }
416 
417  virtual StreamSocket * accept(SocketAddress * pairAddress)
418  {
419  size_t addrsize;
420  addrsize = sizeof(sockaddr_storage);
421  int pair_socket = ::accept( _socket_fd, pairAddress?reinterpret_cast<struct sockaddr *>(const_cast<void *>(pairAddress->getPlatformData())):NULL
422  , (socklen_t*)&addrsize);
423 
424  if (pair_socket>=0) {
425  return new StreamSocketImpl(pair_socket);
426  } else {
427  return NULL;
428  }
429  }
430 
432  {
433  return waitforData(timeout);
434  }
435 
436  virtual u_result send(const void * buffer, size_t len)
437  {
438  size_t ans = ::send( _socket_fd, buffer, len, 0);
439  if (ans == (int)len) {
440  return RESULT_OK;
441  } else {
442  switch (errno) {
443  case EAGAIN:
444 #if EWOULDBLOCK!=EAGAIN
445  case EWOULDBLOCK:
446 #endif
448  default:
449  return RESULT_OPERATION_FAIL;
450  }
451  }
452 
453  }
454 
455 
456  virtual u_result recv(void *buf, size_t len, size_t & recv_len)
457  {
458  size_t ans = ::recv( _socket_fd, buf, len, 0);
459  if (ans == (size_t)-1) {
460  recv_len = 0;
461 
462  switch (errno) {
463  case EAGAIN:
464 #if EWOULDBLOCK!=EAGAIN
465  case EWOULDBLOCK:
466 #endif
468  default:
469  return RESULT_OPERATION_FAIL;
470  }
471 
472 
473 
474  } else {
475  recv_len = ans;
476  return RESULT_OK;
477  }
478  }
479 
480 #if 0
481  virtual u_result recvNoWait(void *buf, size_t len, size_t & recv_len)
482  {
483  size_t ans = ::recv( _socket_fd, buf, len, MSG_DONTWAIT);
484  if (ans == (size_t)-1) {
485  recv_len = 0;
486  if (errno == EAGAIN || errno == EWOULDBLOCK) {
487  return RESULT_OK;
488  } else {
489  return RESULT_OPERATION_FAIL;
490  }
491 
492 
493  } else {
494  recv_len = ans;
495  return RESULT_OK;
496  }
497 
498  }
499 #endif
500 
502  {
503  struct sockaddr * addr = reinterpret_cast<struct sockaddr *>(const_cast<void *>(peerAddr.getPlatformData())); //donnot do this at home...
504  assert(addr);
505  size_t actualsize = sizeof(sockaddr_storage);
506  int ans = ::getpeername(_socket_fd, addr, (socklen_t*)&actualsize);
507 
508  assert(actualsize <= sizeof(sockaddr_storage));
509  assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
510 
511  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
512 
513  }
514 
516  {
517  int shutdw_opt ;
518 
519  switch (mask) {
520  case SOCKET_DIR_RD:
521  shutdw_opt = SHUT_RD;
522  break;
523  case SOCKET_DIR_WR:
524  shutdw_opt = SHUT_WR;
525  break;
526  case SOCKET_DIR_BOTH:
527  default:
528  shutdw_opt = SHUT_RDWR;
529  }
530 
531  int ans = ::shutdown(_socket_fd, shutdw_opt);
532  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
533  }
534 
535  virtual u_result enableKeepAlive(bool enable)
536  {
537  int bool_true = enable?1:0;
538  return ::setsockopt( _socket_fd, SOL_SOCKET, SO_KEEPALIVE , &bool_true, sizeof(bool_true) )?RESULT_OPERATION_FAIL:RESULT_OK;
539  }
540 
541  virtual u_result enableNoDelay(bool enable )
542  {
543  int bool_true = enable?1:0;
544  return ::setsockopt( _socket_fd, IPPROTO_TCP, TCP_NODELAY,&bool_true, sizeof(bool_true) )?RESULT_OPERATION_FAIL:RESULT_OK;
545  }
546 
547  virtual u_result waitforSent(_u32 timeout )
548  {
549  fd_set wrset;
550  FD_ZERO(&wrset);
551  FD_SET(_socket_fd, &wrset);
552 
553  timeval tv;
554  tv.tv_sec = timeout / 1000;
555  tv.tv_usec = (timeout % 1000) * 1000;
556  int ans = ::select(_socket_fd+1, NULL, &wrset, NULL, &tv);
557 
558  switch (ans) {
559  case 1:
560  // fired
561  return RESULT_OK;
562  case 0:
563  // timeout
565  default:
566  delay(0); //relax cpu
567  return RESULT_OPERATION_FAIL;
568  }
569  }
570 
571  virtual u_result waitforData(_u32 timeout )
572  {
573  fd_set rdset;
574  FD_ZERO(&rdset);
575  FD_SET(_socket_fd, &rdset);
576 
577  timeval tv;
578  tv.tv_sec = timeout / 1000;
579  tv.tv_usec = (timeout % 1000) * 1000;
580  int ans = ::select(_socket_fd+1, &rdset, NULL, NULL, &tv);
581 
582  switch (ans) {
583  case 1:
584  // fired
585  return RESULT_OK;
586  case 0:
587  // timeout
589  default:
590  delay(0); //relax cpu
591  return RESULT_OPERATION_FAIL;
592  }
593  }
594 
595 protected:
596  int _socket_fd;
597 
598 
599 };
600 
601 
602 class _single_thread DGramSocketImpl : public DGramSocket
603 {
604 public:
605 
607  : _socket_fd(fd)
608  {
609  assert(fd>=0);
610  int bool_true = 1;
611  ::setsockopt( _socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST , (char *)&bool_true, sizeof(bool_true) );
612  setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
613  }
614 
615  virtual ~DGramSocketImpl()
616  {
617  close(_socket_fd);
618  }
619 
620  virtual void dispose()
621  {
622  delete this;
623  }
624 
625 
626  virtual u_result bind(const SocketAddress & localaddr)
627  {
628  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(localaddr.getPlatformData());
629  assert(addr);
630  int ans = ::bind(_socket_fd, addr, sizeof(sockaddr_storage));
631  if (ans) {
632  return RESULT_OPERATION_FAIL;
633  } else {
634  return RESULT_OK;
635  }
636  }
637 
639  {
640  struct sockaddr * addr = reinterpret_cast<struct sockaddr *>(const_cast<void *>((localaddr.getPlatformData()))); //donnot do this at home...
641  assert(addr);
642 
643  size_t actualsize = sizeof(sockaddr_storage);
644  int ans = ::getsockname(_socket_fd, addr, (socklen_t*)&actualsize);
645 
646  assert(actualsize <= sizeof(sockaddr_storage));
647  assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
648 
649  return ans?RESULT_OPERATION_FAIL:RESULT_OK;
650  }
651 
653  {
654  int ans;
655  timeval tv;
656  tv.tv_sec = timeout / 1000;
657  tv.tv_usec = (timeout % 1000) * 1000;
658 
659  if (msk & SOCKET_DIR_RD) {
660  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv) );
661  if (ans) return RESULT_OPERATION_FAIL;
662  }
663 
664  if (msk & SOCKET_DIR_WR) {
665  ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv) );
666  if (ans) return RESULT_OPERATION_FAIL;
667  }
668 
669  return RESULT_OK;
670  }
671 
672 
673  virtual u_result waitforSent(_u32 timeout )
674  {
675  fd_set wrset;
676  FD_ZERO(&wrset);
677  FD_SET(_socket_fd, &wrset);
678 
679  timeval tv;
680  tv.tv_sec = timeout / 1000;
681  tv.tv_usec = (timeout % 1000) * 1000;
682  int ans = ::select(_socket_fd+1, NULL, &wrset, NULL, &tv);
683 
684  switch (ans) {
685  case 1:
686  // fired
687  return RESULT_OK;
688  case 0:
689  // timeout
691  default:
692  delay(0); //relax cpu
693  return RESULT_OPERATION_FAIL;
694  }
695  }
696 
697  virtual u_result waitforData(_u32 timeout )
698  {
699  fd_set rdset;
700  FD_ZERO(&rdset);
701  FD_SET(_socket_fd, &rdset);
702 
703  timeval tv;
704  tv.tv_sec = timeout / 1000;
705  tv.tv_usec = (timeout % 1000) * 1000;
706  int ans = ::select(_socket_fd+1, &rdset, NULL, NULL, &tv);
707 
708  switch (ans) {
709  case 1:
710  // fired
711  return RESULT_OK;
712  case 0:
713  // timeout
715  default:
716  delay(0); //relax cpu
717  return RESULT_OPERATION_FAIL;
718  }
719  }
720 
721  virtual u_result sendTo(const SocketAddress & target, const void * buffer, size_t len)
722  {
723  const struct sockaddr * addr = reinterpret_cast<const struct sockaddr *>(target.getPlatformData());
724  assert(addr);
725  size_t ans = ::sendto( _socket_fd, buffer, len, 0, addr, sizeof(sockaddr_storage));
726  if (ans != (size_t)-1) {
727  assert(ans == (int)len);
728  return RESULT_OK;
729  } else {
730  switch (errno) {
731  case EAGAIN:
732 #if EWOULDBLOCK!=EAGAIN
733  case EWOULDBLOCK:
734 #endif
736 
737  case EMSGSIZE:
738  return RESULT_INVALID_DATA;
739  default:
740  return RESULT_OPERATION_FAIL;
741  }
742 
743  }
744 
745  }
746 
747 
748  virtual u_result recvFrom(void *buf, size_t len, size_t & recv_len, SocketAddress * sourceAddr)
749  {
750  struct sockaddr * addr = (sourceAddr?reinterpret_cast<struct sockaddr *>(const_cast<void *>(sourceAddr->getPlatformData())):NULL);
751  size_t source_addr_size = (sourceAddr?sizeof(sockaddr_storage):0);
752 
753  size_t ans = ::recvfrom( _socket_fd, buf, len, 0, addr, (socklen_t*)&source_addr_size);
754  if (ans == (size_t)-1) {
755  recv_len = 0;
756  switch (errno) {
757  case EAGAIN:
758 #if EWOULDBLOCK!=EAGAIN
759  case EWOULDBLOCK:
760 #endif
762  default:
763  return RESULT_OPERATION_FAIL;
764  }
765 
766  } else {
767  recv_len = ans;
768  return RESULT_OK;
769  }
770 
771  }
772 
773 #if 0
774  virtual u_result recvFromNoWait(void *buf, size_t len, size_t & recv_len, SocketAddress * sourceAddr)
775  {
776  struct sockaddr * addr = (sourceAddr?reinterpret_cast<struct sockaddr *>(const_cast<void *>(sourceAddr->getPlatformData())):NULL);
777  size_t source_addr_size = (sourceAddr?sizeof(sockaddr_storage):0);
778 
779 
780  size_t ans = ::recvfrom( _socket_fd, buf, len, MSG_DONTWAIT, addr, &source_addr_size);
781 
782  if (ans == (size_t)-1) {
783  recv_len = 0;
784  if (errno == EAGAIN || errno == EWOULDBLOCK) {
785  return RESULT_OK;
786  } else {
787  return RESULT_OPERATION_FAIL;
788  }
789 
790 
791  } else {
792  recv_len = ans;
793  return RESULT_OK;
794  }
795 
796  }
797 #endif
798 
799 protected:
800  int _socket_fd;
801 
802 };
803 
804 
805 }}}
806 
807 
808 namespace rp { namespace net{
809 
810 
812 {
813  switch (family) {
815  return AF_INET;
817  return AF_INET6;
819  assert(!"should not reach here, AF_PACKET is not supported on macOS");
820  return AF_INET;
821  default:
822  assert(!"should not reach here");
823  return AF_INET; // force treating as IPv4 in release mode
824  }
825 
826 }
827 
829 {
830  if (family == SOCKET_FAMILY_RAW) return NULL;
831 
832 
833  int socket_family = _socketHalFamilyToOSFamily(family);
834  int socket_fd = ::socket(socket_family, SOCK_STREAM, 0);
835  if (socket_fd == -1) return NULL;
836 
837  StreamSocket * newborn = static_cast<StreamSocket *>(new rp::arch::net::StreamSocketImpl(socket_fd));
838  return newborn;
839 
840 }
841 
842 
844 {
845  int socket_family = _socketHalFamilyToOSFamily(family);
846 
847 
848  int socket_fd = ::socket(socket_family, (family==SOCKET_FAMILY_RAW)?SOCK_RAW:SOCK_DGRAM, 0);
849  if (socket_fd == -1) return NULL;
850 
851  DGramSocket * newborn = static_cast<DGramSocket *>(new rp::arch::net::DGramSocketImpl(socket_fd));
852  return newborn;
853 
854 }
855 
856 
857 }}
858 
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 Mar 20 2019 07:54:15