mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
b603c8aebc
@ -369,7 +369,7 @@ JsVar *jswrap_net_createServer(JsVar *callback) {
|
||||
"generate_full" : "jswrap_net_connect(options, callback, ST_NORMAL)",
|
||||
"params" : [
|
||||
["options","JsVar","An object containing host,port fields"],
|
||||
["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."]
|
||||
["callback","JsVar","A `function(socket)` that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."]
|
||||
],
|
||||
"return" : ["JsVar","Returns a new net.Socket object"],
|
||||
"return_object" : "Socket"
|
||||
@ -397,17 +397,11 @@ JsVar *jswrap_net_connect(JsVar *options, JsVar *callback, SocketType socketType
|
||||
#endif
|
||||
|
||||
// Make sure we have a function as callback, or nothing (which is OK too)
|
||||
JsVar *skippedCallback = jsvSkipName(callback);
|
||||
if (!jsvIsUndefined(skippedCallback)) {
|
||||
if (!jsvIsFunction(skippedCallback)) {
|
||||
jsError("Expecting Callback Function but got %t", skippedCallback);
|
||||
jsvUnLock(skippedCallback);
|
||||
return 0;
|
||||
}
|
||||
jsvUnLock(skippedCallback);
|
||||
} else {
|
||||
callback = NULL;
|
||||
if (!jsvIsUndefined(callback) && !jsvIsFunction(callback)) {
|
||||
jsError("Expecting Callback Function but got %t", callback);
|
||||
return 0;
|
||||
}
|
||||
|
||||
JsVar *rq = clientRequestNew(socketType, options, callback);
|
||||
if (unlockOptions) jsvUnLock(options);
|
||||
|
||||
|
||||
@ -228,24 +228,26 @@ NO_INLINE static void _socketCloseAllConnections(JsNetwork *net) {
|
||||
int socketSendData(JsNetwork *net, JsVar *connection, int sckt, JsVar **sendData) {
|
||||
char buf[CHUNK];
|
||||
|
||||
if (!jsvIsEmptyString(*sendData)) {
|
||||
size_t bufLen = httpStringGet(*sendData, buf, sizeof(buf));
|
||||
int num = netSend(net, sckt, buf, bufLen);
|
||||
if (num < 0) return num; // an error occurred
|
||||
// Now cut what we managed to send off the beginning of sendData
|
||||
if (num > 0) {
|
||||
JsVar *newSendData = 0;
|
||||
if (num < (int)jsvGetStringLength(*sendData)) {
|
||||
// we didn't send all of it... cut out what we did send
|
||||
newSendData = jsvNewFromStringVar(*sendData, (size_t)num, JSVAPPENDSTRINGVAR_MAXLENGTH);
|
||||
} else {
|
||||
// we sent all of it! Issue a drain event
|
||||
jsiQueueObjectCallbacks(connection, HTTP_NAME_ON_DRAIN, &connection, 1);
|
||||
}
|
||||
jsvUnLock(*sendData);
|
||||
*sendData = newSendData;
|
||||
assert(!jsvIsEmptyString(*sendData));
|
||||
|
||||
size_t bufLen = httpStringGet(*sendData, buf, sizeof(buf));
|
||||
int num = netSend(net, sckt, buf, bufLen);
|
||||
if (num < 0) return num; // an error occurred
|
||||
// Now cut what we managed to send off the beginning of sendData
|
||||
if (num > 0) {
|
||||
JsVar *newSendData = 0;
|
||||
if (num < (int)jsvGetStringLength(*sendData)) {
|
||||
// we didn't send all of it... cut out what we did send
|
||||
newSendData = jsvNewFromStringVar(*sendData, (size_t)num, JSVAPPENDSTRINGVAR_MAXLENGTH);
|
||||
} else {
|
||||
// we sent all of it! Issue a drain event
|
||||
jsiQueueObjectCallbacks(connection, HTTP_NAME_ON_DRAIN, &connection, 1);
|
||||
newSendData = jsvNewFromEmptyString();
|
||||
}
|
||||
jsvUnLock(*sendData);
|
||||
*sendData = newSendData;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -352,17 +354,17 @@ bool socketServerConnectionsIdle(JsNetwork *net) {
|
||||
|
||||
// send data if possible
|
||||
JsVar *sendData = jsvObjectGetChild(socket,HTTP_NAME_SEND_DATA,0);
|
||||
if (sendData) {
|
||||
int sent = socketSendData(net, socket, sckt, &sendData);
|
||||
// FIXME? checking for errors is a bit iffy. With the esp8266 network that returns
|
||||
// varied error codes we'd want to skip SOCKET_ERR_CLOSED and let the recv side deal
|
||||
// with normal closing so we don't miss the tail of what's received, but other drivers
|
||||
// return -1 (which is the same value) for all errors. So we rely on the check ~12 lines
|
||||
// down if(num>0)closeConnectionNow=false instead.
|
||||
if (sent < 0) {
|
||||
closeConnectionNow = true;
|
||||
error = sent;
|
||||
}
|
||||
if (sendData && !jsvIsEmptyString(sendData)) {
|
||||
int sent = socketSendData(net, socket, sckt, &sendData);
|
||||
// FIXME? checking for errors is a bit iffy. With the esp8266 network that returns
|
||||
// varied error codes we'd want to skip SOCKET_ERR_CLOSED and let the recv side deal
|
||||
// with normal closing so we don't miss the tail of what's received, but other drivers
|
||||
// return -1 (which is the same value) for all errors. So we rely on the check ~12 lines
|
||||
// down if(num>0)closeConnectionNow=false instead.
|
||||
if (sent < 0) {
|
||||
closeConnectionNow = true;
|
||||
error = sent;
|
||||
}
|
||||
jsvObjectSetChild(socket, HTTP_NAME_SEND_DATA, sendData); // socketSendData prob updated sendData
|
||||
}
|
||||
// only close if we want to close, have no data to send, and aren't receiving data
|
||||
@ -459,12 +461,12 @@ bool socketClientConnectionsIdle(JsNetwork *net) {
|
||||
JsVar *sendData = jsvObjectGetChild(connection,HTTP_NAME_SEND_DATA,0);
|
||||
if (!closeConnectionNow) {
|
||||
// send data if possible
|
||||
if (sendData) {
|
||||
if (sendData && !jsvIsEmptyString(sendData)) {
|
||||
// don't try to send if we're already in error state
|
||||
int num = 0;
|
||||
if (error == 0) num = socketSendData(net, connection, sckt, &sendData);
|
||||
if (num > 0 && !alreadyConnected && !isHttp) { // whoa, we sent something, must be connected!
|
||||
jsiQueueObjectCallbacks(connection, HTTP_NAME_ON_CONNECT, NULL, 0);
|
||||
jsiQueueObjectCallbacks(connection, HTTP_NAME_ON_CONNECT, &connection, 1);
|
||||
jsvObjectSetChildAndUnLock(connection, HTTP_NAME_CONNECTED, jsvNewFromBool(true));
|
||||
alreadyConnected = true;
|
||||
}
|
||||
@ -491,7 +493,7 @@ bool socketClientConnectionsIdle(JsNetwork *net) {
|
||||
} else {
|
||||
// did we just get connected?
|
||||
if (!alreadyConnected && !isHttp) {
|
||||
jsiQueueObjectCallbacks(connection, HTTP_NAME_ON_CONNECT, NULL, 0);
|
||||
jsiQueueObjectCallbacks(connection, HTTP_NAME_ON_CONNECT, &connection, 1);
|
||||
jsvObjectSetChildAndUnLock(connection, HTTP_NAME_CONNECTED, jsvNewFromBool(true));
|
||||
alreadyConnected = true;
|
||||
// if we do not have any data to send, issue a drain event
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user