#include "inspircd.h"
-#include "socket.h"
-#include "inspstring.h"
-#include "socketengine.h"
+#include "iohook.h"
#ifndef DISABLE_WRITEV
#include <sys/uio.h>
this->fd = newfd;
this->state = I_CONNECTED;
if (fd > -1)
- ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
+ SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
}
void BufferedSocket::DoConnect(const std::string &ipaddr, int aport, unsigned long maxtime, const std::string &connectbindip)
if (err != I_ERR_NONE)
{
state = I_ERROR;
- SetError(strerror(errno));
+ SetError(SocketEngine::LastError());
OnError(err);
}
}
if (bind.sa.sa_family != 0)
{
- if (ServerInstance->SE->Bind(fd, bind) < 0)
+ if (SocketEngine::Bind(fd, bind) < 0)
return I_ERR_BIND;
}
- ServerInstance->SE->NonBlocking(fd);
+ SocketEngine::NonBlocking(fd);
- if (ServerInstance->SE->Connect(this, &dest.sa, dest.sa_size()) == -1)
+ if (SocketEngine::Connect(this, &dest.sa, dest.sa_size()) == -1)
{
if (errno != EINPROGRESS)
return I_ERR_CONNECT;
this->state = I_CONNECTING;
- if (!ServerInstance->SE->AddFd(this, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE | FD_WRITE_WILL_BLOCK))
+ if (!SocketEngine::AddFd(this, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE | FD_WRITE_WILL_BLOCK))
return I_ERR_NOMOREFDS;
- this->Timeout = new SocketTimeout(this->GetFd(), this, timeout, ServerInstance->Time());
- ServerInstance->Timers->AddTimer(this->Timeout);
+ this->Timeout = new SocketTimeout(this->GetFd(), this, timeout);
+ ServerInstance->Timers.AddTimer(this->Timeout);
ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BufferedSocket::DoConnect success");
return I_ERR_NONE;
{
// final chance, dump as much of the sendq as we can
DoWrite();
- if (IOHook)
+ if (GetIOHook())
{
try
{
- IOHook->OnStreamSocketClose(this);
+ GetIOHook()->OnStreamSocketClose(this);
}
catch (CoreException& modexcept)
{
ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "%s threw an exception: %s",
- modexcept.GetSource(), modexcept.GetReason());
+ modexcept.GetSource().c_str(), modexcept.GetReason().c_str());
}
- IOHook = NULL;
+ delete iohook;
+ DelIOHook();
}
- ServerInstance->SE->Shutdown(this, 2);
- ServerInstance->SE->DelFd(this);
- ServerInstance->SE->Close(this);
- fd = -1;
+ SocketEngine::Shutdown(this, 2);
+ SocketEngine::Close(this);
}
}
void StreamSocket::DoRead()
{
- if (IOHook)
+ if (GetIOHook())
{
int rv = -1;
try
{
- rv = IOHook->OnStreamSocketRead(this, recvq);
+ rv = GetIOHook()->OnStreamSocketRead(this, recvq);
}
catch (CoreException& modexcept)
{
ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "%s threw an exception: %s",
- modexcept.GetSource(), modexcept.GetReason());
+ modexcept.GetSource().c_str(), modexcept.GetReason().c_str());
return;
}
if (rv > 0)
else
{
char* ReadBuffer = ServerInstance->GetReadBuffer();
- int n = ServerInstance->SE->Recv(this, ReadBuffer, ServerInstance->Config->NetBufferSize, 0);
+ int n = SocketEngine::Recv(this, ReadBuffer, ServerInstance->Config->NetBufferSize, 0);
if (n == ServerInstance->Config->NetBufferSize)
{
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_ADD_TRIAL_READ);
+ SocketEngine::ChangeEventMask(this, FD_WANT_FAST_READ | FD_ADD_TRIAL_READ);
recvq.append(ReadBuffer, n);
OnDataReady();
}
else if (n > 0)
{
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ);
+ SocketEngine::ChangeEventMask(this, FD_WANT_FAST_READ);
recvq.append(ReadBuffer, n);
OnDataReady();
}
else if (n == 0)
{
error = "Connection closed";
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
+ SocketEngine::ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
}
else if (SocketEngine::IgnoreError())
{
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_READ_WILL_BLOCK);
+ SocketEngine::ChangeEventMask(this, FD_WANT_FAST_READ | FD_READ_WILL_BLOCK);
}
else if (errno == EINTR)
{
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_ADD_TRIAL_READ);
+ SocketEngine::ChangeEventMask(this, FD_WANT_FAST_READ | FD_ADD_TRIAL_READ);
}
else
{
- error = strerror(errno);
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
+ error = SocketEngine::LastError();
+ SocketEngine::ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
}
}
}
}
#ifndef DISABLE_WRITEV
- if (IOHook)
+ if (GetIOHook())
#endif
{
int rv = -1;
// The length limit of 1024 is to prevent merging strings
// more than once when writes begin to block.
std::string tmp;
- tmp.reserve(sendq_len);
- for(unsigned int i=0; i < sendq.size(); i++)
- tmp.append(sendq[i]);
- sendq.clear();
- sendq.push_back(tmp);
+ tmp.reserve(1280);
+ while (!sendq.empty() && tmp.length() < 1024)
+ {
+ tmp.append(sendq.front());
+ sendq.pop_front();
+ }
+ sendq.push_front(tmp);
}
std::string& front = sendq.front();
int itemlen = front.length();
- if (IOHook)
+ if (GetIOHook())
{
- rv = IOHook->OnStreamSocketWrite(this, front);
+ rv = GetIOHook()->OnStreamSocketWrite(this, front);
if (rv > 0)
{
// consumed the entire string, and is ready for more
#ifdef DISABLE_WRITEV
else
{
- rv = ServerInstance->SE->Send(this, front.data(), itemlen, 0);
+ rv = SocketEngine::Send(this, front.data(), itemlen, 0);
if (rv == 0)
{
SetError("Connection closed");
else if (rv < 0)
{
if (errno == EINTR || SocketEngine::IgnoreError())
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
+ SocketEngine::ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
else
- SetError(strerror(errno));
+ SetError(SocketEngine::LastError());
return;
}
else if (rv < itemlen)
{
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
+ SocketEngine::ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
front = front.substr(rv);
sendq_len -= rv;
return;
sendq_len -= itemlen;
sendq.pop_front();
if (sendq.empty())
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_EDGE_WRITE);
+ SocketEngine::ChangeEventMask(this, FD_WANT_EDGE_WRITE);
}
}
#endif
catch (CoreException& modexcept)
{
ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "%s threw an exception: %s",
- modexcept.GetSource(), modexcept.GetReason());
+ modexcept.GetSource().c_str(), modexcept.GetReason().c_str());
}
}
#ifndef DISABLE_WRITEV
}
else
{
- error = strerror(errno);
+ error = SocketEngine::LastError();
}
}
if (!error.empty())
{
// error - kill all events
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
+ SocketEngine::ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
}
else
{
- ServerInstance->SE->ChangeEventMask(this, eventChange);
+ SocketEngine::ChangeEventMask(this, eventChange);
}
}
#endif
sendq.push_back(data);
sendq_len += data.length();
- ServerInstance->SE->ChangeEventMask(this, FD_ADD_TRIAL_WRITE);
+ SocketEngine::ChangeEventMask(this, FD_ADD_TRIAL_WRITE);
}
bool SocketTimeout::Tick(time_t)
{
ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "SocketTimeout::Tick");
- if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
+ if (SocketEngine::GetRef(this->sfd) != this->sock)
+ {
+ delete this;
return false;
+ }
if (this->sock->state == I_CONNECTING)
{
}
this->sock->Timeout = NULL;
+ delete this;
return false;
}
{
state = I_CONNECTED;
this->OnConnected();
- if (GetIOHook())
- GetIOHook()->OnStreamSocketConnect(this);
- else
- ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
+ if (!GetIOHook())
+ SocketEngine::ChangeEventMask(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
}
this->StreamSocket::DoWrite();
}
if (errornum == 0)
SetError("Connection closed");
else
- SetError(strerror(errornum));
+ SetError(SocketEngine::GetError(errornum));
switch (errornum)
{
case ETIMEDOUT:
catch (CoreException& ex)
{
ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Caught exception in socket processing on FD %d - '%s'",
- fd, ex.GetReason());
+ fd, ex.GetReason().c_str());
SetError(ex.GetReason());
}
if (!error.empty())
OnError(errcode);
}
}
-