Skip to content

Commit 5ee3578

Browse files
committedSep 5, 2021
chore: some code cleanup
1 parent c34b7fc commit 5ee3578

File tree

10 files changed

+62
-58
lines changed

10 files changed

+62
-58
lines changed
 

‎components/esp32-javascript/modules/esp32-javascript/global.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
24+
// make `global` available for compatibility reasons. For new features use globalThis instead.
2425
globalThis.global = globalThis;

‎components/esp32-javascript/modules/esp32-javascript/global.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ SOFTWARE.
2424

2525
/* eslint-disable no-var */
2626
declare var global: any;
27-
(globalThis as any).global = globalThis;
27+
// make `global` available for compatibility reasons. For new features use globalThis instead.
28+
(globalThis as any).global = globalThis;

‎components/esp32-javascript/modules/esp32-js-eventloop/index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,16 @@ function el_select_next() {
125125
function start() {
126126
var nextfuncs = [main];
127127
for (;;) {
128-
if (Array.isArray(nextfuncs)) {
129-
nextfuncs.forEach(function (nf) {
130-
if (typeof nf === "function") {
131-
try {
132-
nf();
133-
}
134-
catch (error) {
135-
internalErrorHandler(error);
136-
}
128+
nextfuncs.forEach(function (nf) {
129+
if (typeof nf === "function") {
130+
try {
131+
nf();
137132
}
138-
});
139-
}
133+
catch (error) {
134+
internalErrorHandler(error);
135+
}
136+
}
137+
});
140138
nextfuncs = el_select_next();
141139
}
142140
}

‎components/esp32-javascript/modules/esp32-js-eventloop/index.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,15 @@ function el_select_next() {
175175
export function start(): void {
176176
let nextfuncs: (() => void)[] = [main];
177177
for (;;) {
178-
if (Array.isArray(nextfuncs)) {
179-
nextfuncs.forEach(function (nf) {
180-
if (typeof nf === "function") {
181-
try {
182-
nf();
183-
} catch (error) {
184-
internalErrorHandler(error);
185-
}
178+
nextfuncs.forEach(function (nf) {
179+
if (typeof nf === "function") {
180+
try {
181+
nf();
182+
} catch (error) {
183+
internalErrorHandler(error);
186184
}
187-
});
188-
}
185+
}
186+
});
189187
nextfuncs = el_select_next();
190188
}
191189
}

‎components/socket-events/modules/socket-events/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ var Socket = /** @class */ (function () {
158158
this.clearReadTimeoutTimer();
159159
if (this.readTimeout > 0) {
160160
this.readTimeoutHandle = setTimeout(function () {
161-
console.debug("Close socket because of read timeout.");
161+
if (console.isDebug) {
162+
console.debug("Close socket because of read timeout.");
163+
}
162164
closeSocket(_this);
163165
}, this.readTimeout);
164166
}

‎components/socket-events/modules/socket-events/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ class Socket implements Esp32JsSocket {
188188
this.clearReadTimeoutTimer();
189189
if (this.readTimeout > 0) {
190190
this.readTimeoutHandle = setTimeout(() => {
191-
console.debug("Close socket because of read timeout.");
191+
if (console.isDebug) {
192+
console.debug("Close socket because of read timeout.");
193+
}
192194
closeSocket(this);
193195
}, this.readTimeout);
194196
}

‎components/socket-events/socket-events.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ void select_task_it()
238238
if (dataAvailable > 0)
239239
{
240240
char msg[dataAvailable];
241-
struct sockaddr_in remaddr;
242-
socklen_t addrlen = sizeof(remaddr);
243-
if (recvfrom(selectServerSocket, msg, dataAvailable, 0, (struct sockaddr *)&remaddr, &addrlen) < 0)
241+
if (recv(selectServerSocket, msg, dataAvailable, 0) < 0)
244242
{
245243
jslog(ERROR, "READ self-socket FAILED: %d", errno);
246244
}

‎components/socket-events/tcp.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4367
int 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

184206
int 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

‎examples/repl.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* After that you can type every JS expression, which then gets
1313
* evaluated in esp32-javascript and the result is printed.
1414
*
15-
* This is only considered as demo. If you want to use it in production
16-
* please change the ssl flag to true, otherwise credentials
15+
* This is only considered as demo. If you want to use it in production
16+
* please change the ssl flag to true, otherwise credentials
1717
* are visible for "persons in the middle".
1818
*/
1919
var configManager = require("esp32-javascript/config");
@@ -78,7 +78,7 @@ require("socket-events").sockListen(
7878
logOutput.push("ERROR|" + msg + "\n");
7979
},
8080
};
81-
_ = eval(data);
81+
_ = eval.call(globalThis, data);
8282
} finally {
8383
console = _console;
8484
}

‎sdkconfig.defaults

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
665665
# CONFIG_LWIP_L2_TO_L3_COPY is not set
666666
CONFIG_LWIP_IRAM_OPTIMIZATION=y
667667
CONFIG_LWIP_TIMERS_ONDEMAND=y
668-
CONFIG_LWIP_MAX_SOCKETS=10
668+
CONFIG_LWIP_MAX_SOCKETS=12
669669
CONFIG_LWIP_USE_ONLY_LWIP_SELECT=y
670670
# CONFIG_LWIP_SO_LINGER is not set
671671
CONFIG_LWIP_SO_REUSE=y

0 commit comments

Comments
 (0)
Please sign in to comment.