]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Merge branch 'master+foreachneighbor'
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index f27e21ad8cbf93b8e04571136c7ca1cd916f5462..ee5287e5f33888e0cd24b46f0ce68979cf2d33f9 100644 (file)
@@ -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)
@@ -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;
@@ -138,9 +135,7 @@ void StreamSocket::Close()
                        DelIOHook();
                }
                SocketEngine::Shutdown(this, 2);
-               ServerInstance->SE->DelFd(this);
                SocketEngine::Close(this);
-               fd = -1;
        }
 }
 
@@ -155,9 +150,8 @@ bool StreamSocket::GetNextLine(std::string& line, char delim)
        std::string::size_type i = recvq.find(delim);
        if (i == std::string::npos)
                return false;
-       line = recvq.substr(0, i);
-       // TODO is this the most efficient way to split?
-       recvq = recvq.substr(i + 1);
+       line.assign(recvq, 0, i);
+       recvq.erase(0, i + 1);
        return true;
 }
 
@@ -184,36 +178,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 = SocketEngine::LastError();
-                       ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
+                       SocketEngine::ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
                }
        }
 }
@@ -225,7 +219,7 @@ void StreamSocket::DoWrite()
 {
        if (sendq.empty())
                return;
-       if (!error.empty() || fd < 0 || fd == INT_MAX)
+       if (!error.empty() || fd < 0)
        {
                ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "DoWrite on errored or closed socket");
                return;
@@ -287,7 +281,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");
@@ -296,15 +290,15 @@ 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(SocketEngine::LastError());
                                                return;
                                        }
                                        else if (rv < itemlen)
                                        {
-                                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
-                                               front = front.substr(rv);
+                                               SocketEngine::ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
+                                               front.erase(0, rv);
                                                sendq_len -= rv;
                                                return;
                                        }
@@ -313,7 +307,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
@@ -345,15 +339,17 @@ void StreamSocket::DoWrite()
                        }
 
                        int rv_max = 0;
-                       iovec* iovecs = new iovec[bufcount];
-                       for(int i=0; i < bufcount; i++)
+                       int rv;
                        {
-                               iovecs[i].iov_base = const_cast<char*>(sendq[i].data());
-                               iovecs[i].iov_len = sendq[i].length();
-                               rv_max += sendq[i].length();
+                               iovec iovecs[MYIOV_MAX];
+                               for (int i = 0; i < bufcount; i++)
+                               {
+                                       iovecs[i].iov_base = const_cast<char*>(sendq[i].data());
+                                       iovecs[i].iov_len = sendq[i].length();
+                                       rv_max += sendq[i].length();
+                               }
+                               rv = writev(fd, iovecs, bufcount);
                        }
-                       int rv = writev(fd, iovecs, bufcount);
-                       delete[] iovecs;
 
                        if (rv == (int)sendq_len)
                        {
@@ -383,7 +379,7 @@ void StreamSocket::DoWrite()
                                        else
                                        {
                                                // stopped in the middle of this string
-                                               front = front.substr(rv);
+                                               front.erase(0, rv);
                                                rv = 0;
                                        }
                                }
@@ -409,11 +405,11 @@ void StreamSocket::DoWrite()
                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
@@ -432,14 +428,14 @@ 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;
@@ -473,7 +469,7 @@ void BufferedSocket::DoWrite()
                state = I_CONNECTED;
                this->OnConnected();
                if (!GetIOHook())
-                       ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
+                       SocketEngine::ChangeEventMask(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
        }
        this->StreamSocket::DoWrite();
 }
@@ -481,11 +477,8 @@ void BufferedSocket::DoWrite()
 BufferedSocket::~BufferedSocket()
 {
        this->Close();
-       if (Timeout)
-       {
-               // The timer is removed from the TimerManager in Timer::~Timer()
-               delete Timeout;
-       }
+       // The timer is removed from the TimerManager in Timer::~Timer()
+       delete Timeout;
 }
 
 void StreamSocket::HandleEvent(EventType et, int errornum)