Connect timeouts now work again, using InspSocket

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@5041 e03df62e-2008-0410-955e-edbf42e46eb7
This commit is contained in:
brain 2006-08-28 13:32:20 +00:00
parent 104cf9cbee
commit 82e7e21e15
2 changed files with 35 additions and 34 deletions

View File

@ -24,6 +24,7 @@
#include "inspircd_config.h"
#include "socket.h"
#include "inspsocket.h"
#include "timer.h"
/**
* States which a socket may be in
@ -43,6 +44,17 @@ using irc::sockets::insp_inaddr;
using irc::sockets::insp_ntoa;
using irc::sockets::insp_aton;
class SocketTimeout : public InspTimer
{
private:
InspSocket* sock;
InspIRCd* ServerInstance;
int sfd;
public:
SocketTimeout(int fd, InspIRCd* Instance, InspSocket* thesock, long secs_from_now, time_t now) : InspTimer(secs_from_now, now), sock(thesock), ServerInstance(Instance), sfd(fd) { };
virtual void Tick(time_t now);
};
/**
* InspSocket is an extendable socket class which modules
* can use for TCP socket support. It is fully integrated
@ -59,6 +71,10 @@ class InspSocket : public EventHandler
public:
InspIRCd* Instance;
SocketTimeout* Timeout;
unsigned long timeout_val;
std::deque<std::string> outbuffer;
/**
@ -91,13 +107,6 @@ class InspSocket : public EventHandler
*/
insp_inaddr addy;
/**
* When this time is reached,
* the socket times out if it is
* in the CONNECTING state
*/
time_t timeout_end;
/**
* This value is true if the
* socket has timed out.
@ -267,14 +276,6 @@ class InspSocket : public EventHandler
*/
std::string GetIP();
/**
* This function checks if the socket has
* timed out yet, given the current time
* in the parameter.
* @return true if timed out, false if not timed out
*/
bool Timeout(time_t current);
/**
* Writes a std::string to the socket. No carriage
* returns or linefeeds are appended to the string.

View File

@ -117,7 +117,7 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool
{
this->Instance->Log(DEBUG,"No need to resolve %s",this->host);
strlcpy(this->IP,host,MAXBUF);
timeout_end = time(NULL) + maxtime;
timeout_val = maxtime;
this->DoConnect();
}
}
@ -254,6 +254,9 @@ bool InspSocket::DoConnect()
this->state = I_ERROR;
return false;
}
this->Timeout = new SocketTimeout(this->GetFd(), this->Instance, this, timeout_val, this->Instance->Time());
this->Instance->Timers->AddTimer(this->Timeout);
}
this->state = I_CONNECTING;
if (this->fd > -1)
@ -366,34 +369,31 @@ bool InspSocket::FlushWriteBuffer()
return (fd < 0);
}
bool InspSocket::Timeout(time_t current)
void SocketTimeout::Tick(time_t now)
{
if (this->Instance->SE->GetRef(this->fd) != this)
if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
{
this->Instance->Log(DEBUG,"No FD or socket ref");
return false;
ServerInstance->Log(DEBUG,"No FD or socket ref");
return;
}
if (this->ClosePending)
if (this->sock->state == I_CONNECTING)
{
this->Instance->Log(DEBUG,"Close is pending");
return true;
}
if ((this->state == I_CONNECTING) && (current > timeout_end))
{
this->Instance->Log(DEBUG,"Timed out, current=%lu timeout_end=%lu");
ServerInstance->Log(DEBUG,"Timed out, current=%lu",now);
// for non-listening sockets, the timeout can occur
// which causes termination of the connection after
// the given number of seconds without a successful
// connection.
this->OnTimeout();
this->OnError(I_ERR_TIMEOUT);
timeout = true;
this->state = I_ERROR;
return true;
this->sock->OnTimeout();
this->sock->OnError(I_ERR_TIMEOUT);
this->sock->timeout = true;
this->sock->state = I_ERROR;
ServerInstance->SE->DelFd(this->sock);
this->sock->Close();
delete this->sock;
return;
}
return this->FlushWriteBuffer();
this->sock->FlushWriteBuffer();
}
bool InspSocket::Poll()