socket.cc
Go to the documentation of this file.
1 /*
2 # Software License Agreement (MIT License)
3 #
4 # Copyright (c) 2020, UFACTORY, Inc.
5 # All rights reserved.
6 #
7 # Author: Vinman <vinman.wen@ufactory.cc> <vinman.cub@gmail.com>
8 */
9 #include <string.h>
10 #include <errno.h>
11 #include "xarm/core/port/socket.h"
12 #include "xarm/core/os/network.h"
13 
14 #ifdef _WIN32
15 #include <ws2tcpip.h>
16 static int close(int fd)
17 {
18  return closesocket(fd);
19 }
20 
21 static bool is_ignore_errno(int fp, int port)
22 {
23  if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK) {
24  printf("EINTR occured, port=%d, fp=%d, errno=%d\n", port, fp, WSAGetLastError());
25  return true;
26  }
27  printf("socket read failed, port=%d, fp=%d, errno=%d, exit\n", port, fp, WSAGetLastError());
28  return false;
29 }
30 #else
31 #include <sys/socket.h>
32 #include <unistd.h>
33 static bool is_ignore_errno(int fp, int port)
34 {
35  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
36  printf("EINTR occured, port=%d, fp=%d, errno=%d\n", port, fp, errno);
37  return true;
38  }
39  printf("socket read failed, port=%d, fp=%d, errno=%d, exit\n", port, fp, errno);
40  return false;
41 }
42 #endif
43 
44 
46  int ret;
47  int failed_cnt = 0;
48  int num;
49  unsigned char *recv_data = new unsigned char[que_maxlen_];
50  while (state_ == 0) {
51  memset(recv_data, 0, que_maxlen_);
52  num = recv(fp_, (char *)&recv_data[4], que_maxlen_ - 4, 0);
53  if (num <= 0) {
54  if (is_ignore_errno(fp_, port_)) {
55  continue;
56  }
57  else {
58  close_port();
59  break;
60  }
61  }
62  bin32_to_8(num, &recv_data[0]);
63  ret = rx_que_->push(recv_data);
64  failed_cnt = 0;
65  while (ret != 0 && state_ == 0 && failed_cnt < 1500)
66  {
67  std::this_thread::sleep_for(std::chrono::milliseconds(2));
68  ret = rx_que_->push(recv_data);
69  failed_cnt += 1;
70  }
71  if (ret != 0) {
72  if (state_ == 0)
73  printf("socket push data failed, exit, port=%d, fp=%d\n", port_, fp_);
74  close_port();
75  break;
76  };
77  }
78  delete[] recv_data;
79  delete rx_que_;
80 }
81 
82 static void *recv_proc_(void *arg) {
83  SocketPort *my_this = (SocketPort *)arg;
84 
85  my_this->recv_proc();
86  return (void *)0;
87 }
88 
89 SocketPort::SocketPort(char *server_ip, int server_port, int que_num,
90  int que_maxlen) {
91  que_num_ = que_num;
92  que_maxlen_ = que_maxlen;
93  state_ = -1;
95  fp_ = socket_init((char *)" ", 0, 0);
96  if (fp_ == -1) {
97  delete rx_que_;
98  return;
99  }
100 
101  int ret = socket_connect_server(&fp_, server_ip, server_port);
102  if (ret == -1) {
103  delete rx_que_;
104  return;
105  }
106  port_ = server_port;
107  state_ = 0;
108  flush();
109  std::thread th(recv_proc_, this);
110  th.detach();
111 }
112 
114  state_ = -1;
115  close_port();
116 }
117 
118 int SocketPort::is_ok(void) { return state_; }
119 
120 void SocketPort::flush(void) { rx_que_->flush(); }
121 
122 int SocketPort::read_frame(unsigned char *data) {
123  if (state_ != 0) { return -1; }
124 
125  if (rx_que_->size() == 0) { return -1; }
126 
127  rx_que_->pop(data);
128  return 0;
129 }
130 
131 int SocketPort::write_frame(unsigned char *data, int len) {
132  int ret = socket_send_data(fp_, data, len);
133  return ret;
134 }
135 
137  state_ = -1;
138  flush();
139  close(fp_);
140 }
void recv_proc(void)
Definition: socket.cc:45
QueueMemcpy * rx_que_
Definition: socket.h:32
int port_
Definition: socket.h:28
int fp_
Definition: socket.h:29
int write_frame(unsigned char *data, int len)
Definition: socket.cc:131
char pop(void *data)
Definition: queue_memcpy.cc:41
int read_frame(unsigned char *data)
Definition: socket.cc:122
void flush(void)
Definition: socket.cc:120
int que_maxlen_
Definition: socket.h:25
void close_port(void)
Definition: socket.cc:136
int is_ok(void)
Definition: socket.cc:118
static bool is_ignore_errno(int fp, int port)
Definition: socket.cc:33
SocketPort(char *server_ip, int server_port, int que_num, int que_maxlen)
Definition: socket.cc:89
int socket_init(char *local_ip, int port, int is_server)
Definition: network_new.cc:36
long size(void)
Definition: queue_memcpy.cc:30
int socket_connect_server(int *socket, char server_ip[], int server_port)
Definition: network_new.cc:80
static void * recv_proc_(void *arg)
Definition: socket.cc:82
char push(void *data)
Definition: queue_memcpy.cc:66
int socket_send_data(int client_fp, unsigned char *data, int len)
Definition: network_new.cc:91
int que_num_
Definition: socket.h:31
~SocketPort(void)
Definition: socket.cc:113
int state_
Definition: socket.h:30
char flush(void)
Definition: queue_memcpy.cc:21
void bin32_to_8(int a, unsigned char *b)
Definition: data_type.h:25


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23