Bug 261696 - Use state variable for connection information, making /cancel work better.

ChatZilla only.
r=samuel@sieb.net
This commit is contained in:
silver%warwickcompsoc.co.uk 2004-09-28 21:27:13 +00:00
parent 6fbab3cc29
commit 045d7584fa
6 changed files with 92 additions and 39 deletions

View File

@ -39,6 +39,7 @@
const JSIRC_ERR_NO_SOCKET = "JSIRCE:NS";
const JSIRC_ERR_EXHAUSTED = "JSIRCE:E";
const JSIRC_ERR_CANCELLED = "JSIRCE:C";
function userIsMe (user)
{
@ -74,6 +75,17 @@ function decodeParam(number, charsetOrObject)
return rv;
}
var i = 1;
const NET_OFFLINE = i++; // Initial, disconected.
const NET_WAITING = i++; // Waiting before trying.
const NET_CONNECTING = i++; // Trying a connect...
const NET_CANCELLING = i++; // Cancelling connect.
const NET_ONLINE = i++; // Connected ok.
const NET_DISCONNECTING = i++; // Disconnecting.
delete i;
/*
* irc network
*/
@ -87,7 +99,7 @@ function CIRCNetwork (name, serverList, eventPump)
this.serverList = new Array();
this.ignoreList = new Object();
this.ignoreMaskCache = new Object();
this.connecting = false;
this.state = NET_OFFLINE;
for (var i = 0; i < serverList.length; ++i)
{
@ -174,6 +186,7 @@ function net_connect(requireSecurity)
if ("primServ" in this && this.primServ.isConnected)
return;
this.state = NET_CONNECTING;
this.connectAttempt = 0;
this.nextHost = 0;
var ev = new CEvent("network", "do-connect", this, "onDoConnect");
@ -189,6 +202,28 @@ function net_quit (reason)
this.primServ.logout(reason);
}
CIRCNetwork.prototype.cancel =
function net_cancel()
{
if (this.state == NET_ONLINE)
{
// Pull the plug on the current connection, or...
this.quit();
}
else if ((this.state == NET_CONNECTING) || (this.state == NET_WAITING))
{
this.state = NET_CANCELLING;
// ...try a reconnect (which will fail us).
var ev = new CEvent("network", "do-connect", this, "onDoConnect");
this.eventPump.addEvent(ev);
}
else
{
dd("Network cancel in odd state: " + this.state);
}
}
/*
* Handles a request to connect to a primary server.
*/
@ -197,32 +232,49 @@ function net_doconnect(e)
{
var c;
if ("primServ" in this && this.primServ.isConnected)
return true;
// Clear the timer, if there is one.
if ("reconnectTimer" in this)
{
clearTimeout(this.reconnectTimer);
delete this.reconnectTimer;
}
var ev;
if ((this.connectAttempt++ >= this.MAX_CONNECT_ATTEMPTS) ||
("cancelConnect" in this))
if (this.state == NET_CANCELLING)
{
if ("reconnectTimer" in this)
{
clearTimeout(this.reconnectTimer);
delete this.reconnectTimer;
}
delete this.cancelConnect;
if ("primServ" in this && this.primServ.connection)
this.primServ.connection.disconnect();
else
this.state = NET_OFFLINE;
ev = new CEvent ("network", "error", this, "onError");
ev.server = this;
ev.debug = "Connect sequence was cancelled.";
ev.errorCode = JSIRC_ERR_CANCELLED;
this.eventPump.addEvent(ev);
return false;
}
if ("primServ" in this && this.primServ.isConnected)
return true;
if (this.connectAttempt++ >= this.MAX_CONNECT_ATTEMPTS)
{
this.state = NET_OFFLINE;
ev = new CEvent ("network", "error", this, "onError");
ev.server = this;
ev.debug = "Connection attempts exhausted, giving up.";
ev.errorCode = JSIRC_ERR_EXHAUSTED;
this.eventPump.addEvent (ev);
this.eventPump.addEvent(ev);
return false;
}
this.connecting = true; /* connection is considered "made" when serve
* sends a 001 message (see server.on001) */
this.state = NET_CONNECTING; /* connection is considered "made" when server
* sends a 001 message (see server.on001) */
var host = this.nextHost++;
if (host >= this.serverList.length)
@ -784,9 +836,13 @@ function serv_whois (target)
CIRCServer.prototype.onDisconnect =
function serv_disconnect(e)
{
if ((this.parent.connecting) ||
function stateChangeFn(network, state) {
network.state = state;
};
if ((this.parent.state == NET_CONNECTING) ||
/* fell off while connecting, try again */
(this.parent.primServ == this) &&
(this.parent.primServ == this) && (this.parent.state == NET_ONLINE) &&
(!("quitting" in this) && this.parent.stayingPower))
{ /* fell off primary server, reconnect to any host in the serverList */
var reconnectFn = function(server) {
@ -795,8 +851,13 @@ function serv_disconnect(e)
"onDoConnect");
server.parent.eventPump.addEvent(ev);
};
setTimeout(stateChangeFn, 0, this.parent, NET_WAITING);
this.parent.reconnectTimer = setTimeout(reconnectFn, 15000, this);
}
else
{
setTimeout(stateChangeFn, 0, this.parent, NET_OFFLINE);
}
e.server = this;
e.set = "network";
@ -1165,7 +1226,7 @@ CIRCServer.prototype.on001 =
function serv_001 (e)
{
this.parent.connectAttempt = 0;
this.parent.connecting = false;
this.parent.state = NET_ONLINE;
/* servers won't send a nick change notification if user was forced
* to change nick while logging in (eg. nick already in use.) We need

View File

@ -588,25 +588,14 @@ function cmdCancel(e)
{
var network = e.network;
if (!network.connecting)
if ((network.state != NET_CONNECTING) && (network.state != NET_WAITING))
{
display(MSG_NOTHING_TO_CANCEL, MT_ERROR);
return;
}
network.cancelConnect = true;
display(getMsg(MSG_CANCELLING, network.unicodeName));
if (network.isConnected())
{
// Pull the plug on the current connection, or...
network.dispatch("disconnect");
}
else
{
// ...try a reconnect (which will fail us).
var ev = new CEvent("network", "do-connect", network, "onDoConnect");
network.eventPump.addEvent(ev);
}
network.cancel();
}
function cmdChanUserMode(e)

View File

@ -1323,7 +1323,7 @@ function my_invite (e)
CIRCNetwork.prototype.on433 = /* nickname in use */
function my_433 (e)
{
if (e.params[2] == this.INITIAL_NICK && this.connecting)
if ((e.params[2] == this.INITIAL_NICK) && (this.state == NET_CONNECTING))
{
var newnick = this.INITIAL_NICK + "_";
this.INITIAL_NICK = newnick;
@ -1352,6 +1352,7 @@ CIRCNetwork.prototype.onError =
function my_neterror (e)
{
var msg;
var type = "ERROR";
if (typeof e.errorCode != "undefined")
{
@ -1364,16 +1365,20 @@ function my_neterror (e)
case JSIRC_ERR_EXHAUSTED:
msg = MSG_ERR_EXHAUSTED;
break;
case JSIRC_ERR_CANCELLED:
msg = MSG_ERR_CANCELLED;
type = "INFO";
break;
}
}
else
msg = e.params[e.params.length - 1];
this.connecting = false;
dispatch("sync-header");
updateTitle();
this.display (msg, "ERROR");
this.display(msg, type);
}
@ -1381,7 +1386,6 @@ CIRCNetwork.prototype.onDisconnect =
function my_netdisconnect (e)
{
var msg;
var reconnect = false;
if (typeof e.disconnectStatus != "undefined")
{
@ -1417,7 +1421,6 @@ function my_netdisconnect (e)
msg = getMsg(MSG_CLOSE_STATUS,
[this.getURL(), e.server.getURL(),
e.disconnectStatus]);
reconnect = true;
break;
}
}
@ -1430,7 +1433,7 @@ function my_netdisconnect (e)
/* If we were only /trying/ to connect, and failed, just put an error on
* the network tab. If we were actually connected ok, put it on all tabs.
*/
if (this.connecting)
if (this.state != NET_ONLINE)
{
this.busy = false;
updateProgress();

View File

@ -347,7 +347,7 @@ function updateClient()
function updateNetwork()
{
if (view.connecting)
if (view.state == client.mainWindow.NET_CONNECTING)
{
setText("status", MSG_CONNECTING);
setAttribute("status","condition", "yellow");

View File

@ -2821,7 +2821,7 @@ function cli_connect(networkOrName, requireSecurity)
return network;
}
if (network.connecting)
if (network.state != NET_OFFLINE)
return network;
if (network.prefs["nickname"] == DEFAULT_NICK)
@ -2830,7 +2830,6 @@ function cli_connect(networkOrName, requireSecurity)
if (!("connecting" in network))
network.display(getMsg(MSG_NETWORK_CONNECTING, name));
network.connecting = true;
network.connect(requireSecurity);
network.updateHeader();

View File

@ -561,6 +561,7 @@ msg.err.no.default = No default action for objects of type ``%S''.
msg.err.no.match = No match for ``%S''.
msg.err.no.socket = Error creating socket.
msg.err.exhausted = Connection attempts exhausted, giving up.
msg.err.cancelled = Connection process cancelled.
msg.err.badalias = Malformed alias: %S"
msg.err.no.ctcp.cmd = %S is not a valid CTCP function for this client
msg.err.no.ctcp.help = %S does not have any help information