1010
1111class ZephyrSocketWrapper {
1212protected:
13- int sock_fd;
13+ int * sock_fd = nullptr ;
1414 bool is_ssl = false ;
1515 int ssl_sock_temp_char = -1 ;
1616
1717public:
18- ZephyrSocketWrapper () : sock_fd(-1 ) {
18+ ZephyrSocketWrapper () {
19+ sock_fd = new int (-1 );
1920 }
2021
21- ZephyrSocketWrapper (int sock_fd) : sock_fd(sock_fd) {
22+ ZephyrSocketWrapper (int fd) {
23+ sock_fd = new int (fd);
2224 }
2325
2426 ~ZephyrSocketWrapper () {
25- if (sock_fd != -1 ) {
26- ::close (sock_fd);
27+ if (sock_fd && *sock_fd != -1 ) {
28+ ::close (* sock_fd);
2729 }
30+ delete sock_fd;
31+ sock_fd = nullptr ;
2832 }
2933
3034 bool connect (const char *host, uint16_t port) {
@@ -55,16 +59,16 @@ class ZephyrSocketWrapper {
5559 goto exit;
5660 }
5761
58- sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
59- if (sock_fd < 0 ) {
62+ * sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
63+ if (!sock_fd || * sock_fd < 0 ) {
6064 rv = false ;
6165
6266 goto exit;
6367 }
6468
65- if (::connect (sock_fd, res->ai_addr , res->ai_addrlen ) < 0 ) {
66- ::close (sock_fd);
67- sock_fd = -1 ;
69+ if (::connect (* sock_fd, res->ai_addr , res->ai_addrlen ) < 0 ) {
70+ ::close (* sock_fd);
71+ * sock_fd = -1 ;
6872 rv = false ;
6973 goto exit;
7074 }
@@ -87,14 +91,14 @@ class ZephyrSocketWrapper {
8791 addr.sin_port = htons (port);
8892 inet_pton (AF_INET, _host, &addr.sin_addr );
8993
90- sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
91- if (sock_fd < 0 ) {
94+ * sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
95+ if (!sock_fd || * sock_fd < 0 ) {
9296 return false ;
9397 }
9498
95- if (::connect (sock_fd, (struct sockaddr *)&addr, sizeof (addr)) < 0 ) {
96- ::close (sock_fd);
97- sock_fd = -1 ;
99+ if (::connect (* sock_fd, (struct sockaddr *)&addr, sizeof (addr)) < 0 ) {
100+ ::close (* sock_fd);
101+ * sock_fd = -1 ;
98102 return false ;
99103 }
100104
@@ -146,18 +150,18 @@ class ZephyrSocketWrapper {
146150 }
147151 }
148152
149- sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
150- if (sock_fd < 0 ) {
153+ * sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
154+ if (!sock_fd || * sock_fd < 0 ) {
151155 goto exit;
152156 }
153157
154- if (setsockopt (sock_fd, SOL_TLS, TLS_HOSTNAME, host, strlen (host)) ||
155- setsockopt (sock_fd, SOL_TLS, TLS_SEC_TAG_LIST, sec_tag_opt, sizeof (sec_tag_opt)) ||
156- setsockopt (sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout_opt, sizeof (timeout_opt))) {
158+ if (setsockopt (* sock_fd, SOL_TLS, TLS_HOSTNAME, host, strlen (host)) ||
159+ setsockopt (* sock_fd, SOL_TLS, TLS_SEC_TAG_LIST, sec_tag_opt, sizeof (sec_tag_opt)) ||
160+ setsockopt (* sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout_opt, sizeof (timeout_opt))) {
157161 goto exit;
158162 }
159163
160- if (::connect (sock_fd, res->ai_addr , res->ai_addrlen ) < 0 ) {
164+ if (::connect (* sock_fd, res->ai_addr , res->ai_addrlen ) < 0 ) {
161165 goto exit;
162166 }
163167
@@ -170,9 +174,9 @@ class ZephyrSocketWrapper {
170174 res = nullptr ;
171175 }
172176
173- if (!rv && sock_fd >= 0 ) {
174- ::close (sock_fd);
175- sock_fd = -1 ;
177+ if (!rv && * sock_fd >= 0 ) {
178+ ::close (* sock_fd);
179+ * sock_fd = -1 ;
176180 }
177181 return rv;
178182 }
@@ -191,9 +195,9 @@ class ZephyrSocketWrapper {
191195 if (ssl_sock_temp_char != -1 ) {
192196 return 1 ;
193197 }
194- count = ::recv (sock_fd, &ssl_sock_temp_char, 1 , MSG_DONTWAIT);
198+ count = ::recv (* sock_fd, &ssl_sock_temp_char, 1 , MSG_DONTWAIT);
195199 } else {
196- zsock_ioctl (sock_fd, ZFD_IOCTL_FIONREAD, &count);
200+ zsock_ioctl (* sock_fd, ZFD_IOCTL_FIONREAD, &count);
197201 }
198202 if (count <= 0 ) {
199203 delay (1 );
@@ -203,30 +207,32 @@ class ZephyrSocketWrapper {
203207 }
204208
205209 int recv (uint8_t *buffer, size_t size, int flags = MSG_DONTWAIT) {
206- if (sock_fd == -1 ) {
210+ if (sock_fd == nullptr || *sock_fd == -1 ) {
207211 return -1 ;
208212 }
213+
209214 // TODO: see available()
210215 if (ssl_sock_temp_char != -1 ) {
211- int ret = ::recv (sock_fd, &buffer[1 ], size - 1 , flags);
216+ int ret = ::recv (* sock_fd, &buffer[1 ], size - 1 , flags);
212217 buffer[0 ] = ssl_sock_temp_char;
213218 ssl_sock_temp_char = -1 ;
214219 return ret + 1 ;
215220 }
216- return ::recv (sock_fd, buffer, size, flags);
221+ return ::recv (* sock_fd, buffer, size, flags);
217222 }
218223
219224 int send (const uint8_t *buffer, size_t size) {
220- if (sock_fd == -1 ) {
225+ if (sock_fd == nullptr || *sock_fd == -1 ) {
221226 return -1 ;
222227 }
223- return ::send (sock_fd, buffer, size, 0 );
228+
229+ return ::send (*sock_fd, buffer, size, 0 );
224230 }
225231
226232 void close () {
227- if (sock_fd != -1 ) {
228- ::close (sock_fd);
229- sock_fd = -1 ;
233+ if (sock_fd && *sock_fd != -1 ) {
234+ ::close (* sock_fd);
235+ * sock_fd = -1 ;
230236 }
231237 }
232238
@@ -236,54 +242,54 @@ class ZephyrSocketWrapper {
236242 addr.sin_port = htons (port);
237243 addr.sin_addr .s_addr = INADDR_ANY;
238244
239- sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
240- if (sock_fd < 0 ) {
245+ * sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
246+ if (!sock_fd || * sock_fd < 0 ) {
241247 return false ;
242248 }
243249
244- zsock_ioctl (sock_fd, ZFD_IOCTL_FIONBIO);
250+ zsock_ioctl (* sock_fd, ZFD_IOCTL_FIONBIO);
245251
246- if (::bind (sock_fd, (struct sockaddr *)&addr, sizeof (addr)) < 0 ) {
247- ::close (sock_fd);
248- sock_fd = -1 ;
252+ if (::bind (* sock_fd, (struct sockaddr *)&addr, sizeof (addr)) < 0 ) {
253+ ::close (* sock_fd);
254+ * sock_fd = -1 ;
249255 return false ;
250256 }
251257
252258 return true ;
253259 }
254260
255261 bool listen (int backlog = 5 ) {
256- if (sock_fd == -1 ) {
262+ if (sock_fd == nullptr || *sock_fd == -1 ) {
257263 return false ;
258264 }
259265
260- if (::listen (sock_fd, backlog) < 0 ) {
261- ::close (sock_fd);
262- sock_fd = -1 ;
266+ if (::listen (* sock_fd, backlog) < 0 ) {
267+ ::close (* sock_fd);
268+ * sock_fd = -1 ;
263269 return false ;
264270 }
265271
266272 return true ;
267273 }
268274
269275 int accept () {
270- if (sock_fd == -1 ) {
276+ if (sock_fd == nullptr || *sock_fd == -1 ) {
271277 return -1 ;
272278 }
273279
274- return ::accept (sock_fd, nullptr , nullptr );
280+ return ::accept (* sock_fd, nullptr , nullptr );
275281 }
276282
277283 String remoteIP () {
278- if (sock_fd == -1 ) {
284+ if (sock_fd == nullptr || *sock_fd == -1 ) {
279285 return {};
280286 }
281287
282288 struct sockaddr_storage addr;
283289 socklen_t addr_len = sizeof (addr);
284290 char ip_str[INET6_ADDRSTRLEN] = {0 };
285291
286- if (::getpeername (sock_fd, (struct sockaddr *)&addr, &addr_len) == 0 ) {
292+ if (::getpeername (* sock_fd, (struct sockaddr *)&addr, &addr_len) == 0 ) {
287293 if (addr.ss_family == AF_INET) {
288294 struct sockaddr_in *s = (struct sockaddr_in *)&addr;
289295 ::inet_ntop (AF_INET, &s->sin_addr, ip_str, sizeof (ip_str));
0 commit comments