X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fsocket.cpp;h=a4675f593b6bc95ed9e0d4ad40f442e5bcf78ee1;hb=f9cc7ff2e1e5af865220e9fbe16673619ecb78bd;hp=49614df0c807bdf89f3d5772b3afb996b1bf0039;hpb=35009eb3f51a6fd0f9261f892a85d97d81e69886;p=libs%2Fnet.git diff --git a/source/socket.cpp b/source/socket.cpp index 49614df..a4675f5 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -1,17 +1,11 @@ -/* $Id$ - -This file is part of libmspnet -Copyright © 2008 Mikkosoft Productions, Mikko Rasa -Distributed under the LGPL -*/ - #ifndef WIN32 #include #include #include #endif #include -#include +#include +#include #include #include "socket.h" @@ -24,7 +18,7 @@ public: WinSockHelper() { WSADATA wsa_data; - int err=WSAStartup(0x0002, &wsa_data); + int err = WSAStartup(0x0002, &wsa_data); if(err) std::cerr<<"Failed to initialize WinSock: "<(&sa), &size); + local_addr = SockAddr::create(sa); #ifdef WIN32 - event=CreateEvent(0, false, false, 0); + event = CreateEvent(0, false, false, 0); #endif } @@ -64,10 +58,10 @@ Socket::Socket(Family af, int type, int proto): local_addr(0), peer_addr(0) { - handle=socket(af, type, proto); + handle = socket(af, type, proto); #ifdef WIN32 - event=CreateEvent(0, false, false, 0); + event = CreateEvent(0, false, false, 0); #endif } @@ -78,15 +72,15 @@ Socket::~Socket() void Socket::set_block(bool b) { - mode=(mode&~IO::M_NONBLOCK); + mode = (mode&~IO::M_NONBLOCK); if(b) - mode=(mode|IO::M_NONBLOCK); + mode = (mode|IO::M_NONBLOCK); #ifdef WIN32 - u_long flag=!b; + u_long flag = !b; ioctlsocket(handle, FIONBIO, &flag); #else - int flags=fcntl(handle, F_GETFL); + int flags = fcntl(handle, F_GETFL); fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); #endif } @@ -105,21 +99,17 @@ void Socket::bind(const SockAddr &addr) { check_state(false); - sockaddr sa; - unsigned size=addr.fill_sockaddr(sa); + sockaddr_storage sa; + unsigned size = addr.fill_sockaddr(sa); - int err=::bind(handle, &sa, size); + int err = ::bind(handle, reinterpret_cast(&sa), size); if(err==-1) throw SystemError("Unable to bind", errno); delete local_addr; - local_addr=addr.copy(); + local_addr = addr.copy(); } -/** -Closes the socket. Most operations on the socket will throw an exception after -this. -*/ void Socket::close() { if(handle==MSP_NET_INVALID_SOCKET_HANDLE) @@ -134,21 +124,20 @@ void Socket::close() #else ::close(handle); #endif - handle=MSP_NET_INVALID_SOCKET_HANDLE; - connected=false; + handle = MSP_NET_INVALID_SOCKET_HANDLE; + connected = false; signal_closed.emit(); delete local_addr; - local_addr=0; + local_addr = 0; delete peer_addr; - peer_addr=0; + peer_addr = 0; } void Socket::set_timeout(const Time::TimeDelta &timeout) { #ifndef WIN32 - timeval tv; - timeout.fill_timeval(tv); + timeval tv = Time::rawtime_to_timeval(timeout.raw()); set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval)); set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval)); #else @@ -205,7 +194,7 @@ unsigned Socket::do_write(const char *buf, unsigned size) if(size==0) return 0; - int ret=::send(handle, buf, size, 0); + int ret = ::send(handle, buf, size, 0); if(ret<0) { if(errno==EAGAIN) @@ -224,7 +213,7 @@ unsigned Socket::do_read(char *buf, unsigned size) if(size==0) return 0; - int ret=::recv(handle, buf, size, 0); + int ret = ::recv(handle, buf, size, 0); if(ret<0) { if(errno==EAGAIN) @@ -234,7 +223,7 @@ unsigned Socket::do_read(char *buf, unsigned size) } else if(ret==0 && !eof_flag) { - eof_flag=true; + eof_flag = true; signal_end_of_file.emit(); set_events(IO::P_NONE); }