]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socket.cpp
Inspire -> InspIRCd
[user/henk/code/inspircd.git] / src / socket.cpp
index 7b85f7f6b69cf077bb408c957b7345b492cf16af..550783aed5a7154a580ff8a0e5744c5ce351ce1e 100644 (file)
@@ -114,6 +114,15 @@ InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long
        }
 }
 
+void InspSocket::SetQueues(int nfd)
+{
+        // attempt to increase socket sendq and recvq as high as its possible
+       int sendbuf = 32768;
+       int recvbuf = 32768;
+       setsockopt(nfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
+       setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
+}
+
 bool InspSocket::DoResolve()
 {
        log(DEBUG,"In DoResolve(), trying to resolve IP");
@@ -176,6 +185,7 @@ bool InspSocket::DoConnect()
        this->state = I_CONNECTING;
        ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
        socket_ref[this->fd] = this;
+       this->SetQueues(this->fd);
        return true;
 }
 
@@ -225,8 +235,7 @@ char* InspSocket::Read()
 // and should be aborted.
 int InspSocket::Write(std::string data)
 {
-       this->Buffer = this->Buffer + data;
-       this->FlushWriteBuffer();
+       this->Buffer.append(data);
        return data.length();
 }
 
@@ -238,10 +247,17 @@ void InspSocket::FlushWriteBuffer()
                result = send(this->fd,this->Buffer.c_str(),this->Buffer.length(),0);
                if (result > 0)
                {
-                       /* If we wrote some, advance the buffer forwards */
-                       char* n = (char*)this->Buffer.c_str();
-                       n += result;
-                       this->Buffer = n;
+                       if (result == (int)this->Buffer.length())
+                       {
+                               this->Buffer = "";
+                       }
+                       else
+                       {
+                               /* If we wrote some, advance the buffer forwards */
+                               char* n = (char*)this->Buffer.c_str();
+                               n += result;
+                               this->Buffer = n;
+                       }
                }
        }
 }
@@ -260,14 +276,14 @@ bool InspSocket::Timeout(time_t current)
                this->state = I_ERROR;
                return true;
        }
-       if (this->Buffer.length())
-               this->FlushWriteBuffer();
+       this->FlushWriteBuffer();
        return false;
 }
 
 bool InspSocket::Poll()
 {
        int incoming = -1;
+       bool n = true;
        
        switch (this->state)
        {
@@ -288,19 +304,20 @@ bool InspSocket::Poll()
                case I_LISTENING:
                        length = sizeof (client);
                        incoming = accept (this->fd, (sockaddr*)&client,&length);
+                       this->SetQueues(incoming);
                        this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
                        return true;
                break;
                case I_CONNECTED:
-                       return this->OnDataReady();
+                       n = this->OnDataReady();
+                       /* Flush any pending, but not till after theyre done with the event
+                        * so there are less write calls involved. */
+                       this->FlushWriteBuffer();
+                       return n;
                break;
                default:
                break;
        }
-
-       if (this->Buffer.length())
-               this->FlushWriteBuffer();
-
        return true;
 }