]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Add SocketEngine::WriteV()
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index 3a8a58b5fd4eba87015bb8582a22d7920fe9e81c..db005b1a128f2552433af1a9dd1bdc10ceb24210 100644 (file)
 
 
 #include "inspircd.h"
-#include "socket.h"
-#include "inspstring.h"
-#include "socketengine.h"
-
-#ifndef DISABLE_WRITEV
-#include <sys/uio.h>
-#endif
-
-#ifndef IOV_MAX
-#define IOV_MAX 1024
-#endif
+#include "iohook.h"
 
 BufferedSocket::BufferedSocket()
 {
@@ -47,7 +37,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)
@@ -56,7 +46,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);
        }
 }
@@ -92,13 +82,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;
@@ -106,11 +96,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;
@@ -122,23 +112,22 @@ void StreamSocket::Close()
        {
                // 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);
        }
 }
 
@@ -153,25 +142,24 @@ 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;
 }
 
 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)
@@ -182,36 +170,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);
                }
        }
 }
@@ -223,14 +211,14 @@ 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;
        }
 
 #ifndef DISABLE_WRITEV
-       if (IOHook)
+       if (GetIOHook())
 #endif
        {
                int rv = -1;
@@ -248,17 +236,19 @@ 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();
-                               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
@@ -283,7 +273,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");
@@ -292,15 +282,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(strerror(errno));
+                                                       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;
                                        }
@@ -309,7 +299,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
@@ -318,7 +308,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
@@ -341,15 +331,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 = SocketEngine::WriteV(this, iovecs, bufcount);
                        }
-                       int rv = writev(fd, iovecs, bufcount);
-                       delete[] iovecs;
 
                        if (rv == (int)sendq_len)
                        {
@@ -379,7 +371,7 @@ void StreamSocket::DoWrite()
                                        else
                                        {
                                                // stopped in the middle of this string
-                                               front = front.substr(rv);
+                                               front.erase(0, rv);
                                                rv = 0;
                                        }
                                }
@@ -399,17 +391,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
@@ -428,15 +420,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)
        {
@@ -452,6 +447,7 @@ bool SocketTimeout::Tick(time_t)
        }
 
        this->sock->Timeout = NULL;
+       delete this;
        return false;
 }
 
@@ -464,10 +460,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();
 }
@@ -475,11 +469,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)
@@ -495,7 +486,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:
@@ -530,7 +521,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())
@@ -539,4 +530,3 @@ void StreamSocket::HandleEvent(EventType et, int errornum)
                OnError(errcode);
        }
 }
-