]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socket.cpp
EXPERIMENTAL new socket engine code
[user/henk/code/inspircd.git] / src / socket.cpp
index 48a58db0bdaabf8307466316edd0b815e78bf403..36b6d1d1e52d1793e3e087e719f5bece70e31a44 100644 (file)
@@ -48,9 +48,42 @@ InspSocket::InspSocket()
        this->state = I_DISCONNECTED;
 }
 
+InspSocket::InspSocket(int newfd, char* ip)
+{
+       this->fd = newfd;
+       this->state = I_CONNECTED;
+       this->IP = ip;
+}
+
 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
 {
        if (listening) {
+               if ((this->fd = OpenTCPSocket()) == ERROR)
+               {
+                       this->fd = -1;
+                       this->state = I_ERROR;
+                       this->OnError(I_ERR_SOCKET);
+                       log(DEBUG,"OpenTCPSocket() error");
+                        return;
+               }
+               else
+               {
+                       if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
+                       {
+                               this->Close();
+                               this->fd = -1;
+                               this->state = I_ERROR;
+                               this->OnError(I_ERR_BIND);
+                               log(DEBUG,"BindSocket() error %s",strerror(errno));
+                               return;
+                       }
+                       else
+                       {
+                               this->state = I_LISTENING;
+                               log(DEBUG,"New socket now in I_LISTENING state");
+                               return;
+                       }
+               }                       
        } else {
                char* ip;
                this->host = host;
@@ -62,6 +95,8 @@ InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long
                        ip = inet_ntoa(*ia);
                }
 
+               this->IP = ip;
+
                 timeout_end = time(NULL)+maxtime;
                 timeout = false;
                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
@@ -106,11 +141,17 @@ void InspSocket::Close()
        }
 }
 
+std::string InspSocket::GetIP()
+{
+       return this->IP;
+}
+
 char* InspSocket::Read()
 {
        int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
        if (n > 0)
        {
+               ibuf[n] = 0;
                return ibuf;
        }
        else
@@ -149,10 +190,13 @@ int InspSocket::Write(std::string data)
 
 bool InspSocket::Poll()
 {
-       if (time(NULL) > timeout_end)
+       if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
        {
+               // for non-listening sockets, the timeout can occur
+               // which causes termination of the connection after
+               // the given number of seconds without a successful
+               // connection.
                this->OnTimeout();
-               this->Close();
                this->OnError(I_ERR_TIMEOUT);
                timeout = true;
                this->state = I_ERROR;
@@ -164,6 +208,8 @@ bool InspSocket::Poll()
 
         if (ret > 0)
        {
+               int incoming = -1;
+               
                switch (this->state)
                {
                        case I_CONNECTING:
@@ -171,7 +217,10 @@ bool InspSocket::Poll()
                                return this->OnConnected();
                        break;
                        case I_LISTENING:
-                               this->OnIncomingConnection();
+                               length = sizeof (client);
+                               incoming = accept (this->fd, (sockaddr*)&client,&length);
+                               this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
+                               return true;
                        break;
                        case I_CONNECTED:
                                return this->OnDataReady();
@@ -180,7 +229,6 @@ bool InspSocket::Poll()
                        break;
                }
        }
-
        return true;
 }
 
@@ -190,10 +238,15 @@ void InspSocket::SetState(InspSocketState s)
        this->state = s;
 }
 
+InspSocketState InspSocket::GetState()
+{
+       return this->state;
+}
+
 bool InspSocket::OnConnected() { return true; }
 void InspSocket::OnError(InspSocketError e) { return; }
 int InspSocket::OnDisconnect() { return 0; }
-int InspSocket::OnIncomingConnection() { return 0; }
+int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
 bool InspSocket::OnDataReady() { return true; }
 void InspSocket::OnTimeout() { return; }
 void InspSocket::OnClose() { return; }