]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Change allocation of UserManager::clientlist to be physically part of the object...
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index ec528571bba2f15028daafadc5362d13a8511949..d15a1b6a5953518ef1bff1cad757073ea93661bf 100644 (file)
@@ -48,7 +48,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 +57,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 +93,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,7 +107,7 @@ 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());
@@ -132,14 +132,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 +171,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 +182,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 +248,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 +285,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 +294,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 +311,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 +320,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 +401,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 +430,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 +457,7 @@ bool SocketTimeout::Tick(time_t)
        }
 
        this->sock->Timeout = NULL;
+       delete this;
        return false;
 }
 
@@ -465,10 +470,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 +499,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 +534,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 +543,3 @@ void StreamSocket::HandleEvent(EventType et, int errornum)
                OnError(errcode);
        }
 }
-