move too many connections error from authentication to connection handling
authorJack Allnutt <m2ys4u@Gmail.com>
Sun, 31 Jul 2011 05:33:32 +0000 (06:33 +0100)
committerJack Allnutt <m2ys4u@Gmail.com>
Sun, 31 Jul 2011 05:33:32 +0000 (06:33 +0100)
js/gateway.js
node/kiwi.js

index cdeac1bc1f84183bc35b7e3a389e9880c1ff5f61..9d406c5fed13c090c53ad642b0f662a8f03ef245 100644 (file)
@@ -24,7 +24,7 @@ var gateway = {
             gateway.socket = io.connect(kiwi_server, {'max reconnection attempts': 3});
             gateway.socket.of('/kiwi').on('connect_failed', function (reason) {
                 console.debug('Unable to connect Socket.IO', reason);
-                front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\nYour IP address has too many open connections to Kiwi', 'error');
+                front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error');
                 gateway.socket.disconnect();
                 $(gateway).trigger("ondisconnect", {});
                 gateway.sendData = function () {};
@@ -42,6 +42,9 @@ var gateway = {
                 });
                 gateway.socket.emit('irc connect', gateway.nick, host, port, ssl, callback);
             });
+            gateway.socket.on('too_many_connections', function () {
+                front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\nYour IP address has too many connections to Kiwi IRC', 'error');
+            });
         }
     },
 
index c5f4ce3904e7aa286dd0d7e6403233bd4627a2ba..9cd38c13fab546235e850c40d43ba19c7dbd805e 100644 (file)
@@ -478,104 +478,104 @@ io.of('/kiwi').authorization(function (handshakeData, callback) {
     if (typeof connections[address] === 'undefined') {
         connections[address] = {count: 0, sockets: []};
     }
-    connection = connections[address];
-    if (connection.count >= config.max_client_conns) {
-        callback(null, false);
-        return;
-    }
-    connection.count += 1;
     callback(null, true);
 }).on('connection', function (websocket) {
     var con;
     websocket.kiwi = {address: websocket.handshake.address.address};
     con = connections[websocket.kiwi.address];
-    con.sockets.push(websocket);
-    websocket.on('irc connect', function (nick, host, port, ssl, callback) {
-        var ircSocket;
-        //setup IRC connection
-        if (!ssl) {
-            ircSocket = net.createConnection(port, host);
-        } else {
-            ircSocket = tls.connect(port, host);
-        }
-        ircSocket.setEncoding('ascii');
-        ircSocket.IRC = {options: {}, CAP: {negotiating: true, requested: [], enabled: []}};
-        websocket.ircSocket = ircSocket;
-        ircSocket.holdLast = false;
-        ircSocket.held = '';
-        
-        ircSocket.on('data', function (data) {
-            ircSocketDataHandler(data, websocket, ircSocket);
+    if (con.count >= config.max_client_conns) {
+        websocket.emit('too_many_connections');
+        websocket.disconnect();
+    } else {
+        con.count += 1
+        con.sockets.push(websocket);
+        websocket.on('irc connect', function (nick, host, port, ssl, callback) {
+            var ircSocket;
+            //setup IRC connection
+            if (!ssl) {
+                ircSocket = net.createConnection(port, host);
+            } else {
+                ircSocket = tls.connect(port, host);
+            }
+            ircSocket.setEncoding('ascii');
+            ircSocket.IRC = {options: {}, CAP: {negotiating: true, requested: [], enabled: []}};
+            websocket.ircSocket = ircSocket;
+            ircSocket.holdLast = false;
+            ircSocket.held = '';
+            
+            ircSocket.on('data', function (data) {
+                ircSocketDataHandler(data, websocket, ircSocket);
+            });
+            
+            ircSocket.IRC.nick = nick;
+            // Send the login data
+            ircSocket.write('CAP LS\r\n');
+            ircSocket.write('NICK ' + nick + '\r\n');
+            ircSocket.write('USER ' + nick + '_kiwi 0 0 :' + nick + '\r\n');
+            
+            if ((callback) && (typeof (callback) === 'function')) {
+                callback();
+            }
         });
-        
-        ircSocket.IRC.nick = nick;
-        // Send the login data
-        ircSocket.write('CAP LS\r\n');
-        ircSocket.write('NICK ' + nick + '\r\n');
-        ircSocket.write('USER ' + nick + '_kiwi 0 0 :' + nick + '\r\n');
-        
-        if ((callback) && (typeof (callback) === 'function')) {
-            callback();
-        }
-    });
-    websocket.on('message', function (msg, callback) {
-        var args;
-        try {
-            msg.data = JSON.parse(msg.data);
-            args = msg.data.args;
-            switch (msg.data.method) {
-            case 'msg':
-                if ((args.target) && (args.msg)) {
-                    websocket.ircSocket.write('PRIVMSG ' + args.target + ' :' + args.msg + '\r\n');
-                }
-                break;
-               case 'action':
-                if ((args.target) && (args.msg)) {
-                    websocket.ircSocket.write('PRIVMSG ' + args.target + ' :\ 1ACTION ' + args.msg + '\ 1\r\n');
+        websocket.on('message', function (msg, callback) {
+            var args;
+            try {
+                msg.data = JSON.parse(msg.data);
+                args = msg.data.args;
+                switch (msg.data.method) {
+                case 'msg':
+                    if ((args.target) && (args.msg)) {
+                        websocket.ircSocket.write('PRIVMSG ' + args.target + ' :' + args.msg + '\r\n');
+                    }
+                    break;
+                   case 'action':
+                    if ((args.target) && (args.msg)) {
+                        websocket.ircSocket.write('PRIVMSG ' + args.target + ' :\ 1ACTION ' + args.msg + '\ 1\r\n');
+                    }
+                    break;
+                   case 'raw':
+                    websocket.ircSocket.write(args.data + '\r\n');
+                    break;
+                   case 'join':
+                    if (args.channel) {
+                        _.each(args.channel.split(","), function (chan) {
+                            websocket.ircSocket.write('JOIN ' + chan + '\r\n');
+                        });
+                    }
+                    break;
+                   case 'quit':
+                    websocket.ircSocket.end('QUIT :' + args.message + '\r\n');
+                    websocket.sentQUIT = true;
+                    websocket.ircSocket.destroySoon();
+                    websocket.disconnect();
+                    break;
+                case 'notice':
+                    if ((args.target) && (args.msg)) {
+                        websocket.ircSocket.write('NOTICE ' + args.target + ' :' + args.msg + '\r\n');
+                    }
+                    break;
+                default:
                 }
-                break;
-               case 'raw':
-                websocket.ircSocket.write(args.data + '\r\n');
-                break;
-               case 'join':
-                if (args.channel) {
-                    _.each(args.channel.split(","), function (chan) {
-                        websocket.ircSocket.write('JOIN ' + chan + '\r\n');
-                    });
+                if ((callback) && (typeof (callback) === 'function')) {
+                    callback();
                 }
-                break;
-               case 'quit':
-                websocket.ircSocket.end('QUIT :' + args.message + '\r\n');
+            } catch (e) {
+                console.log("Caught error: " + e);
+            }
+        });
+        websocket.on('disconnect', function () {
+            var con;
+            if ((!websocket.sentQUIT) && (websocket.ircSocket)) {
+                websocket.ircSocket.end('QUIT :' + config.quit_message + '\r\n');
                 websocket.sentQUIT = true;
                 websocket.ircSocket.destroySoon();
-                websocket.disconnect();
-                break;
-            case 'notice':
-                if ((args.target) && (args.msg)) {
-                    websocket.ircSocket.write('NOTICE ' + args.target + ' :' + args.msg + '\r\n');
-                }
-                break;
-            default:
-            }
-            if ((callback) && (typeof (callback) === 'function')) {
-                callback();
             }
-        } catch (e) {
-            console.log("Caught error: " + e);
-        }
-    });
-    websocket.on('disconnect', function () {
-        var con;
-        if ((!websocket.sentQUIT) && (websocket.ircSocket)) {
-            websocket.ircSocket.end('QUIT :' + config.quit_message + '\r\n');
-            websocket.sentQUIT = true;
-            websocket.ircSocket.destroySoon();
-        }
-        con = connections[websocket.kiwi.address];
-        con.count -=1;
-        con.sockets = _.reject(con.sockets, function (sock) {
-            return sock === websocket;
+            con = connections[websocket.kiwi.address];
+            con.count -=1;
+            con.sockets = _.reject(con.sockets, function (sock) {
+                return sock === websocket;
+            });
         });
-    });
+    }
 });