@@ -37,13 +37,37 @@ SOFTWARE.
3737#include "esp32-js-log.h"
3838#include "esp32-javascript.h"
3939
40- #define BUFSIZE 1024
4140#define LISTEN_BACKLOG 50
4241
42+ int setNonBlocking (int sockfd )
43+ {
44+ int opt = 1 ;
45+ int ret = lwip_ioctl (sockfd , FIONBIO , & opt );
46+
47+ if (ret >= 0 )
48+ {
49+ struct timeval tv ;
50+ tv .tv_sec = 0 ;
51+ tv .tv_usec = 1 ;
52+
53+ if (lwip_setsockopt (sockfd ,
54+ SOL_SOCKET ,
55+ SO_RCVTIMEO ,
56+ & tv ,
57+ sizeof (struct timeval )) < 0 )
58+ {
59+ jslog (ERROR , "Cannot set timeout opt." );
60+ return -1 ;
61+ }
62+ }
63+
64+ return ret ;
65+ }
66+
4367int createNonBlockingSocket (int domain , int type , int protocol , bool nonblocking )
4468{
4569 int sockfd ;
46- int opt ;
70+
4771 int ret ;
4872
4973 /* socket: create the socket */
@@ -56,8 +80,7 @@ int createNonBlockingSocket(int domain, int type, int protocol, bool nonblocking
5680
5781 if (nonblocking )
5882 {
59- opt = 1 ;
60- ret = lwip_ioctl (sockfd , FIONBIO , & opt );
83+ int ret = setNonBlocking (sockfd );
6184 if (ret < 0 )
6285 {
6386 jslog (ERROR , "Cannot set non-blocking opt." );
@@ -78,7 +101,7 @@ int connectNonBlocking(int sockfd, const char *hostname, int portno)
78101 server = gethostbyname (hostname );
79102 if (server == NULL )
80103 {
81- jslog (ERROR , "ERROR, no such host as %s" , hostname );
104+ jslog (ERROR , "ERROR, no such host %s" , hostname );
82105 return -1 ;
83106 }
84107
@@ -107,8 +130,7 @@ int acceptIncoming(int sockfd)
107130 int one = 1 ;
108131 setsockopt (cfd , SOL_SOCKET , SO_REUSEADDR , & one , sizeof (one ));
109132
110- int opt = 1 ;
111- int ret = lwip_ioctl (cfd , FIONBIO , & opt );
133+ int ret = setNonBlocking (sockfd );
112134 if (ret < 0 )
113135 {
114136 jslog (ERROR , "ERROR while accepting and setting non blocking: %d" , errno );
@@ -183,34 +205,16 @@ int writeSocket(int sockfd, const char *msg, int len, SSL *ssl)
183205
184206int readSocket (int sockfd , char * msg , int len )
185207{
186- struct sockaddr_in remaddr ;
187- socklen_t addrlen = sizeof (remaddr );
188-
189208 int result = 0 ;
190209
191- int opt = 1 ;
192- int ret = lwip_ioctl (sockfd , FIONBIO , & opt );
210+ int ret = setNonBlocking (sockfd );
193211 if (ret < 0 )
194212 {
195213 jslog (ERROR , "Cannot set non-blocking opt." );
196214 return -1 ;
197215 }
198216
199- struct timeval tv ;
200- tv .tv_sec = 1 ;
201- tv .tv_usec = 0 ;
202-
203- if (lwip_setsockopt (sockfd ,
204- SOL_SOCKET ,
205- SO_RCVTIMEO ,
206- & tv ,
207- sizeof (struct timeval )) < 0 )
208- {
209- jslog (ERROR , "Cannot set timeout opt." );
210- return -1 ;
211- }
212-
213- result = recvfrom (sockfd , msg , len , MSG_DONTWAIT , (struct sockaddr * )& remaddr , & addrlen );
217+ result = recv (sockfd , msg , len , MSG_DONTWAIT );
214218 return result ;
215219}
216220
0 commit comments