aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Pesch <inolen@gmail.com>2013-08-31 14:42:18 -0700
committerAnthony Pesch <inolen@gmail.com>2013-08-31 14:42:18 -0700
commit51587f3c8800500483d8102d8a270a45e5dfc834 (patch)
treef1bbd1afdd260d371ce839146b4111d000c17c54
parent52dae3e9b26dd8b4c6ce7989505c41532c090051 (diff)
- refactored accept code to automatically setup peers for incoming connections immediately upon connect. previously, this wasn't done until the server called accept, creating a small window of time for peer message events to be lost
- renamed send_queue to dgram_send_queue just so its purpose is more clear
-rw-r--r--src/library.js9
-rw-r--r--src/library_sockfs.js52
2 files changed, 34 insertions, 27 deletions
diff --git a/src/library.js b/src/library.js
index 2c60c8ba..1d94d006 100644
--- a/src/library.js
+++ b/src/library.js
@@ -8129,23 +8129,20 @@ LibraryManager.library = {
}
},
- accept__deps: ['$FS', '$SOCKFS', '$DNS', '$ERRNO_CODES', '__setErrNo', '_write_sockaddr', 'socket'],
+ accept__deps: ['$FS', '$SOCKFS', '$DNS', '$ERRNO_CODES', '__setErrNo', '_write_sockaddr'],
accept: function(fd, addrp, addrlen) {
var sock = SOCKFS.getSocket(fd);
if (!sock) {
___setErrNo(ERRNO_CODES.EBADF);
return -1;
}
- var newfd = _socket(sock.family, sock.type, sock.protocol);
- var newsock = SOCKFS.getSocket(newfd);
- assert(newsock);
try {
- sock.sock_ops.accept(sock, newsock, sock.stream.flags);
+ var newsock = sock.sock_ops.accept(sock);
if (addrp) {
var res = __write_sockaddr(addr, newsock.family, DNS.lookup_name(newsock.daddr), newsock.dport);
assert(!res.errno);
}
- return newfd;
+ return newsock.stream.fd;
} catch (e) {
FS.handleFSError(e);
return -1;
diff --git a/src/library_sockfs.js b/src/library_sockfs.js
index 8d2fd9d0..b11c6495 100644
--- a/src/library_sockfs.js
+++ b/src/library_sockfs.js
@@ -154,7 +154,7 @@ mergeInto(LibraryManager.library, {
addr: addr,
port: port,
socket: ws,
- send_queue: []
+ dgram_send_queue: []
};
SOCKFS.websocket_sock_ops.addPeer(sock, peer);
@@ -167,7 +167,7 @@ mergeInto(LibraryManager.library, {
#if SOCKET_DEBUG
Module.print('websocket queuing port message (port ' + sock.sport + ')');
#endif
- peer.send_queue.push(new Uint8Array([
+ peer.dgram_send_queue.push(new Uint8Array([
255, 255, 255, 255,
'p'.charCodeAt(0), 'o'.charCodeAt(0), 'r'.charCodeAt(0), 't'.charCodeAt(0),
((sock.sport & 0xff00) >> 8) , (sock.sport & 0xff)
@@ -189,16 +189,21 @@ mergeInto(LibraryManager.library, {
var first = true;
var handleOpen = function () {
+#if SOCKET_DEBUG
+ Module.print('websocket handle open');
+#endif
try {
- var queued = peer.send_queue.shift();
+ var queued = peer.dgram_send_queue.shift();
while (queued) {
#if SOCKET_DEBUG
Module.print('websocket sending queued data (' + queued.byteLength + ' bytes): ' + [Array.prototype.slice.call(new Uint8Array(queued))]);
#endif
peer.socket.send(queued);
- queued = peer.send_queue.shift();
+ queued = peer.dgram_send_queue.shift();
}
} catch (e) {
+ // not much we can do here in the way of proper error handling as we've already
+ // lied and said this data was sent. shut it down.
peer.socket.close();
}
};
@@ -207,6 +212,10 @@ mergeInto(LibraryManager.library, {
assert(typeof data !== 'string' && data.byteLength !== undefined); // must receive an ArrayBuffer
data = new Uint8Array(data); // make a typed array view on the array buffer
+#if SOCKET_DEBUG
+ Module.print('websocket handle message (' + data.byteLength + ' bytes): ' + [Array.prototype.slice.call(data)]);
+#endif
+
// if this is the port message, override the peer's port with it
var wasfirst = first;
first = false;
@@ -359,7 +368,7 @@ mergeInto(LibraryManager.library, {
}
// add the socket to our peer list and set our
- // defination address / port to match
+ // destination address / port to match
var peer = SOCKFS.websocket_sock_ops.createPeer(sock, addr, port);
sock.daddr = peer.addr;
sock.dport = peer.port;
@@ -390,12 +399,19 @@ mergeInto(LibraryManager.library, {
console.log('received connection from: ' + ws._socket.remoteAddress + ':' + ws._socket.remotePort);
#endif
if (sock.type === {{{ cDefine('SOCK_STREAM') }}}) {
+ var newsock = SOCKFS.createSocket(sock.family, sock.type, sock.protocol);
+
+ // create a peer on the new socket
+ var peer = SOCKFS.websocket_sock_ops.createPeer(newsock, ws);
+ newsock.daddr = peer.addr;
+ newsock.dport = peer.port;
+
// push to queue for accept to pick up
- sock.pending.push(ws);
+ sock.pending.push(newsock);
} else {
- // auto-accept, adding the peer to the listen socket so
- // calling sendto with the listen socket and an address
- // will resolve to the correct client
+ // create a peer on the listen socket so calling sendto
+ // with the listen socket and an address will resolve
+ // to the correct client
SOCKFS.websocket_sock_ops.createPeer(sock, ws);
}
});
@@ -406,19 +422,13 @@ mergeInto(LibraryManager.library, {
// don't throw
});
},
- accept: function(listensock, newsock, flags) {
+ accept: function(listensock) {
if (!listensock.server) {
- ___setErrNo(ERRNO_CODES.EINVAL);
- return -1;
+ throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
}
-
- // create a peer on the server's client socket
- var peer = SOCKFS.websocket_sock_ops.createPeer(newsock, listensock.pending.shift());
- newsock.daddr = peer.addr;
- newsock.dport = peer.port;
- newsock.stream.flags = flags;
-
- return 0;
+ var newsock = listensock.pending.shift();
+ newsock.stream.flags = listensock.stream.flags;
+ return newsock;
},
getname: function(sock, peer) {
var addr, port;
@@ -488,7 +498,7 @@ mergeInto(LibraryManager.library, {
#if SOCKET_DEBUG
Module.print('websocket queuing (' + length + ' bytes): ' + [Array.prototype.slice.call(new Uint8Array(data))]);
#endif
- dest.send_queue.push(data);
+ dest.dgram_send_queue.push(data);
return length;
}
}