Skip to content

Commit

Permalink
Allow different types of network to have different buffer sizes - enl…
Browse files Browse the repository at this point in the history
…arge JS, Linux and WIZnet buffers
  • Loading branch information
gfwilliams committed Jan 15, 2016
1 parent 6a27f69 commit 34326d0
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -16,6 +16,7 @@
Added Tab complete and Debug to low-end Nordic uCs
Limit the number of events processed each time around the idle loop to the number that were in it at the start
- helps to fix issues with out of memory when receiving big files over ESP8266 serial connection
Allow different types of network to have different buffer sizes - enlarge JS, Linux and WIZnet buffers

1v84 : Fix device initialisation flags not working when the device number is above 32 (fix #751, #748)
- this fixes broken SPI when not on standard pins
Expand Down
4 changes: 4 additions & 0 deletions libs/network/cc3000/network_cc3000.c
Expand Up @@ -222,4 +222,8 @@ void netSetCallbacks_cc3000(JsNetwork *net) {
net->gethostbyname = net_cc3000_gethostbyname;
net->recv = net_cc3000_recv;
net->send = net_cc3000_send;
/* we're limited by CC3k buffer sizes - see CC3000_RX_BUFFER_SIZE/CC3000_TX_BUFFER_SIZE
* We could however allocate RAM on the stack (since we now don't use IRQs)
* and could then alloc more, increasing this. */
net->chunkSize = 64;
}
2 changes: 2 additions & 0 deletions libs/network/esp8266/network_esp8266.c
Expand Up @@ -728,6 +728,8 @@ void netSetCallbacks_esp8266_board(
net->gethostbyname = net_ESP8266_BOARD_gethostbyname;
net->recv = net_ESP8266_BOARD_recv;
net->send = net_ESP8266_BOARD_send;
// The TCP MSS is 536, we use half that 'cause otherwise we easily run out of JSvars memory
net->chunkSize = 536/2;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion libs/network/js/network_js.c
Expand Up @@ -117,8 +117,9 @@ int net_js_recv(JsNetwork *net, int sckt, void *buf, size_t len) {
int r = -1; // fail
if (jsvIsString(res)) {
r = (int)jsvGetStringLength(res);
if (r>(int)len) r=(int)len;
if (r>(int)len) { r=(int)len; assert(0); }
jsvGetStringChars(res, 0, (char*)buf, (size_t)r);
// FIXME: jsvGetStringChars adds a 0 - does that actually write past the end of the array, or clip the data we get?
}
jsvUnLock(res);
return r;
Expand Down Expand Up @@ -148,5 +149,6 @@ void netSetCallbacks_js(JsNetwork *net) {
net->gethostbyname = net_js_gethostbyname;
net->recv = net_js_recv;
net->send = net_js_send;
net->chunkSize = 536;
}

1 change: 1 addition & 0 deletions libs/network/linux/network_linux.c
Expand Up @@ -225,4 +225,5 @@ void netSetCallbacks_linux(JsNetwork *net) {
net->gethostbyname = net_linux_gethostbyname;
net->recv = net_linux_recv;
net->send = net_linux_send;
net->chunkSize = 536;
}
2 changes: 2 additions & 0 deletions libs/network/network.h
Expand Up @@ -53,6 +53,8 @@ typedef struct JsNetwork {
JsNetworkData data;
unsigned char _blank; ///< this is needed as jsvGetString for 'data' wants to add a trailing zero

int chunkSize; ///< Amount of memory to allocate for chunks of data when using send/recv

/// Called on idle. Do any checks required for this device
void (*idle)(struct JsNetwork *net);
/// Call just before returning to idle loop. This checks for errors and tries to recover. Returns true if no errors.
Expand Down
18 changes: 7 additions & 11 deletions libs/network/socketserver.c
Expand Up @@ -43,16 +43,11 @@

// Define the size of buffers/chunks that are transmitted or received
#ifdef ESP8266
// The TCP MSS is 536, we use half that 'cause otherwise we easily run out of JSvars memory
#define CHUNK (536/2)
// esp8266 debugging, need to remove this eventually
extern int os_printf_plus(const char *format, ...) __attribute__((format(printf, 1, 2)));
#define printf os_printf_plus
#else
#define CHUNK 64
#endif


// -----------------------------

static void httpAppendHeaders(JsVar *string, JsVar *headerObject) {
Expand Down Expand Up @@ -230,11 +225,11 @@ NO_INLINE static void _socketCloseAllConnections(JsNetwork *net) {

// returns 0 on success and a (negative) error number on failure
int socketSendData(JsNetwork *net, JsVar *connection, int sckt, JsVar **sendData) {
char buf[CHUNK];
char *buf = alloca(net->chunkSize); // allocate on stack

assert(!jsvIsEmptyString(*sendData));

size_t bufLen = httpStringGet(*sendData, buf, sizeof(buf));
size_t bufLen = httpStringGet(*sendData, buf, net->chunkSize);
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
Expand Down Expand Up @@ -301,7 +296,7 @@ static bool fireErrorEvent(int error, JsVar *obj1, JsVar *obj2) {
// -----------------------------

bool socketServerConnectionsIdle(JsNetwork *net) {
char buf[CHUNK];
char *buf = alloca(net->chunkSize); // allocate on stack

JsVar *arr = socketGetArray(HTTP_ARRAY_HTTP_SERVER_CONNECTIONS,false);
if (!arr) return false;
Expand All @@ -322,7 +317,7 @@ bool socketServerConnectionsIdle(JsNetwork *net) {
int error = 0;

if (!closeConnectionNow) {
int num = netRecv(net, sckt, buf,sizeof(buf));
int num = netRecv(net, sckt, buf, net->chunkSize);
if (num<0) {
// we probably disconnected so just get rid of this
closeConnectionNow = true;
Expand Down Expand Up @@ -431,7 +426,7 @@ void socketClientPushReceiveData(JsVar *connection, JsVar *socket, JsVar **recei
}

bool socketClientConnectionsIdle(JsNetwork *net) {
char buf[CHUNK];
char *buf = alloca(net->chunkSize); // allocate on stack

JsVar *arr = socketGetArray(HTTP_ARRAY_HTTP_CLIENT_CONNECTIONS,false);
if (!arr) return false;
Expand Down Expand Up @@ -492,7 +487,7 @@ bool socketClientConnectionsIdle(JsNetwork *net) {
}
// Now read data if possible (and we have space for it)
if (!receiveData || !hadHeaders) {
int num = netRecv(net, sckt, buf, sizeof(buf));
int num = netRecv(net, sckt, buf, net->chunkSize);
//if (num != 0) printf("recv returned %d\r\n", num);
if (!alreadyConnected && num == SOCKET_ERR_NO_CONN) {
; // ignore... it's just telling us we're not connected yet
Expand Down Expand Up @@ -908,6 +903,7 @@ void serverResponseWrite(JsVar *httpServerResponseVar, JsVar *data) {

void serverResponseEnd(JsVar *httpServerResponseVar) {
serverResponseWrite(httpServerResponseVar, 0); // force connection->sendData to be created even if data not called
// TODO: This should only close the connection once the received data length == contentLength header
jsvObjectSetChildAndUnLock(httpServerResponseVar, HTTP_NAME_CLOSE, jsvNewFromBool(true));
}

1 change: 1 addition & 0 deletions libs/network/wiznet/network_wiznet.c
Expand Up @@ -181,5 +181,6 @@ void netSetCallbacks_wiznet(JsNetwork *net) {
net->gethostbyname = net_wiznet_gethostbyname;
net->recv = net_wiznet_recv;
net->send = net_wiznet_send;
net->chunkSize = 536;
}

0 comments on commit 34326d0

Please sign in to comment.