X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Finspsocket.cpp;h=494422075b2b8736e4edc4a1d1c7a3025cbccd73;hb=ac7e37c8542159fa0efa48b43c9ab346762f1638;hp=ec528571bba2f15028daafadc5362d13a8511949;hpb=79db1cf848c64ba50bebadef4c683ae4237080b7;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/inspsocket.cpp b/src/inspsocket.cpp index ec528571b..494422075 100644 --- a/src/inspsocket.cpp +++ b/src/inspsocket.cpp @@ -23,9 +23,6 @@ #include "inspircd.h" -#include "socket.h" -#include "inspstring.h" -#include "socketengine.h" #include "iohook.h" #ifndef DISABLE_WRITEV @@ -48,7 +45,7 @@ BufferedSocket::BufferedSocket(int newfd) 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) @@ -57,7 +54,7 @@ void BufferedSocket::DoConnect(const std::string &ipaddr, int aport, unsigned lo if (err != I_ERR_NONE) { state = I_ERROR; - SetError(strerror(errno)); + SetError(SocketEngine::LastError()); OnError(err); } } @@ -93,13 +90,13 @@ BufferedSocketError BufferedSocket::BeginConnect(const irc::sockets::sockaddrs& 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; @@ -107,11 +104,11 @@ BufferedSocketError BufferedSocket::BeginConnect(const irc::sockets::sockaddrs& 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; @@ -132,14 +129,13 @@ void StreamSocket::Close() 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()); } + 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); } } @@ -172,7 +168,7 @@ void StreamSocket::DoRead() 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) @@ -183,36 +179,36 @@ void StreamSocket::DoRead() 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); } } } @@ -249,11 +245,13 @@ void StreamSocket::DoWrite() // 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(); @@ -284,7 +282,7 @@ void StreamSocket::DoWrite() #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"); @@ -293,14 +291,14 @@ void StreamSocket::DoWrite() 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; @@ -310,7 +308,7 @@ void StreamSocket::DoWrite() 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 @@ -319,7 +317,7 @@ void StreamSocket::DoWrite() 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 @@ -400,17 +398,17 @@ void StreamSocket::DoWrite() } 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 @@ -429,15 +427,18 @@ void StreamSocket::WriteData(const std::string &data) 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) { @@ -453,6 +454,7 @@ bool SocketTimeout::Tick(time_t) } this->sock->Timeout = NULL; + delete this; return false; } @@ -465,10 +467,8 @@ void BufferedSocket::DoWrite() { 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(); } @@ -496,7 +496,7 @@ void StreamSocket::HandleEvent(EventType et, int errornum) if (errornum == 0) SetError("Connection closed"); else - SetError(strerror(errornum)); + SetError(SocketEngine::GetError(errornum)); switch (errornum) { case ETIMEDOUT: @@ -531,7 +531,7 @@ void StreamSocket::HandleEvent(EventType et, int errornum) 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()) @@ -540,4 +540,3 @@ void StreamSocket::HandleEvent(EventType et, int errornum) OnError(errcode); } } -