]> git.tdb.fi Git - libs/net.git/blobdiff - source/streamsocket.cpp
Add function to check if handshake is done
[libs/net.git] / source / streamsocket.cpp
index cf1a7b535203a1b3b643923d0ca09443e063c5b7..0322f12f1c286b63977435e7bb64b151daf0af68 100644 (file)
@@ -1,14 +1,14 @@
 /* $Id$
 
 This file is part of libmspnet
-Copyright © 2008  Mikkosoft Productions, Mikko Rasa
+Copyright © 2008-2009, 2011  Mikkosoft Productions, Mikko Rasa
 Distributed under the LGPL
 */
 
 #ifndef WIN32
 #include <sys/socket.h>
 #endif
-#include <errno.h>
+#include <cerrno>
 #include <msp/io/poll.h>
 #include <msp/strings/formatter.h>
 #include "streamsocket.h"
@@ -16,6 +16,19 @@ Distributed under the LGPL
 namespace Msp {
 namespace Net {
 
+/**
+Used by StreamListenSocket to construct a new socket from accept.
+*/
+StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr):
+       Socket(h, paddr),
+       connecting(false)
+{
+#ifdef WIN32
+       WSAEventSelect(handle, event, FD_READ|FD_CLOSE);
+#endif
+       set_events(IO::P_INPUT);
+}
+
 /**
 Constructs a new StreamSocket.
 */
@@ -41,7 +54,7 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout)
 #ifdef WIN32
                throw Exception(format("Connection polling failed: %d", WSAGetLastError()));
 #else
-               throw Exception(format("Connection polling failed: %s", strerror(errno)));
+               throw SystemError("Connection polling failed", errno);
 #endif
        else if(res>0)
        {
@@ -57,7 +70,7 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout)
 #ifdef WIN32
                        throw Exception(format("Connection failed: %d", err));
 #else
-                       throw Exception(format("Connection failed: %s", strerror(err)));
+                       throw SystemError("Connection failed", err);
 #endif
                }
 
@@ -89,12 +102,12 @@ int StreamSocket::connect(const SockAddr &addr)
        if(connected)
                throw InvalidState("Socket is already connected");
 
-       sockaddr sa;
+       sockaddr_storage sa;
        socklen_t size=addr.fill_sockaddr(sa);
 
 #ifdef WIN32
-       int err=WSAConnect(handle, &sa, size, 0, 0, 0, 0);
-       if(err=SOCKET_ERROR)
+       int err=WSAConnect(handle, reinterpret_cast<sockaddr *>(&sa), size, 0, 0, 0, 0);
+       if(err==SOCKET_ERROR)
        {
                int err_code=WSAGetLastError();
                if(err_code==WSAEWOULDBLOCK)
@@ -107,7 +120,7 @@ int StreamSocket::connect(const SockAddr &addr)
                        throw Exception(format("Unable to connect: %d", err_code));
        }
 #else
-       int err=::connect(handle, &sa, size);
+       int err=::connect(handle, reinterpret_cast<sockaddr *>(&sa), size);
        if(err==-1)
        {
                if(errno==EINPROGRESS)
@@ -116,7 +129,7 @@ int StreamSocket::connect(const SockAddr &addr)
                        set_events(IO::P_OUTPUT);
                }
                else
-                       throw Exception(format("Unable to connect: %s", strerror(errno)));
+                       throw SystemError("Unable to connect", errno);
        }
 #endif
 
@@ -124,32 +137,20 @@ int StreamSocket::connect(const SockAddr &addr)
        peer_addr=addr.copy();
 
        delete local_addr;
-       size=sizeof(sockaddr);
-       getsockname(handle, &sa, &size);
+       size=sizeof(sockaddr_storage);
+       getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
        local_addr=SockAddr::create(sa);
 
        if(err==0)
        {
                connected=true;
+               set_events(IO::P_INPUT);
                signal_connect_finished.emit(0);
        }
 
        return (err==0)?0:1;
 }
 
-/**
-Used by StreamListenSocket to construct a new socket from accept.
-*/
-StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr):
-       Socket(h, paddr),
-       connecting(false)
-{
-#ifdef WIN32
-       WSAEventSelect(handle, event, FD_READ|FD_CLOSE);
-#endif
-       set_events(IO::P_INPUT);
-}
-
 void StreamSocket::on_event(IO::PollEvent ev)
 {
        //cout<<"StreamSocket::on_event "<<ev<<'\n';