From: Mikko Rasa Date: Wed, 20 Aug 2008 20:17:13 +0000 (+0000) Subject: Reorder class members X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3103b110c863c19a56cc176a173cc30ddf13afec;p=libs%2Fnet.git Reorder class members Update svn:ignore --- diff --git a/source/inet.h b/source/inet.h index 1092bc1..56b9f4d 100644 --- a/source/inet.h +++ b/source/inet.h @@ -29,19 +29,21 @@ Address class for IPv4 sockets. */ class InetAddr: public SockAddr { +private: + in_addr_t addr; + in_port_t port; + public: InetAddr(); InetAddr(sockaddr_in &); InetAddr(in_addr_t, in_port_t); + Family get_family() const { return INET; } in_addr_t get_addr() const { return ntohl(addr); } in_port_t get_port() const { return ntohs(port); } std::string str() const; - unsigned fill_sockaddr(sockaddr &) const; - InetAddr *copy() const { return new InetAddr(*this); } -private: - in_addr_t addr; - in_port_t port; + virtual unsigned fill_sockaddr(sockaddr &) const; + virtual InetAddr *copy() const { return new InetAddr(*this); } }; } // namespace Net diff --git a/source/socket.cpp b/source/socket.cpp index fcd686f..f24bcbe 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -43,6 +43,39 @@ WinSockHelper wsh; namespace Msp { namespace Net { +Socket::Socket(SocketHandle h, const SockAddr &paddr): + handle(h), + connected(true), + local_addr(0), + peer_addr(paddr.copy()) +{ + sockaddr sa; + socklen_t size=sizeof(sockaddr); + getsockname(handle, &sa, &size); + local_addr=SockAddr::create(sa); + +#ifdef WIN32 + event=CreateEvent(0, false, false, 0); +#endif +} + +Socket::Socket(Family af, int type, int proto): + connected(false), + local_addr(0), + peer_addr(0) +{ + handle=socket(af, type, proto); + +#ifdef WIN32 + event=CreateEvent(0, false, false, 0); +#endif +} + +Socket::~Socket() +{ + close(); +} + void Socket::set_block(bool b) { mode=(mode&~IO::M_NONBLOCK); @@ -84,7 +117,7 @@ void Socket::bind(const SockAddr &addr) } /** -Closes the socket. Most operations on the socket will return an error after +Closes the socket. Most operations on the socket will throw an exception after this. */ void Socket::close() @@ -124,39 +157,6 @@ const SockAddr &Socket::get_peer_address() const return *peer_addr; } -Socket::~Socket() -{ - close(); -} - -Socket::Socket(SocketHandle h, const SockAddr &paddr): - handle(h), - connected(true), - local_addr(0), - peer_addr(paddr.copy()) -{ - sockaddr sa; - socklen_t size=sizeof(sockaddr); - getsockname(handle, &sa, &size); - local_addr=SockAddr::create(sa); - -#ifdef WIN32 - event=CreateEvent(0, false, false, 0); -#endif -} - -Socket::Socket(Family af, int type, int proto): - connected(false), - local_addr(0), - peer_addr(0) -{ - handle=socket(af, type, proto); - -#ifdef WIN32 - event=CreateEvent(0, false, false, 0); -#endif -} - void Socket::check_state(bool conn) const { if(handle==MSP_NET_INVALID_SOCKET_HANDLE) diff --git a/source/socket.h b/source/socket.h index f275a7e..d807b7d 100644 --- a/source/socket.h +++ b/source/socket.h @@ -18,17 +18,6 @@ namespace Net { class Socket: public IO::Base { -public: - void set_block(bool); - IO::Handle get_event_handle(); - - bool get_connected() const { return connected; } - void bind(const SockAddr &); - virtual int connect(const SockAddr &) =0; - void close(); - const SockAddr &get_local_address() const; - const SockAddr &get_peer_address() const; - ~Socket(); protected: SocketHandle handle; #ifdef WIN32 @@ -40,6 +29,19 @@ protected: Socket(SocketHandle, const SockAddr &); Socket(Family, int, int); +public: + ~Socket(); + + void set_block(bool); + IO::Handle get_event_handle(); + + bool is_connected() const { return connected; } + void bind(const SockAddr &); + virtual int connect(const SockAddr &) =0; + void close(); + const SockAddr &get_local_address() const; + const SockAddr &get_peer_address() const; +protected: void check_state(bool) const; int get_option(int, int, void *, socklen_t *); unsigned do_write(const char *, unsigned); diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index 0f69111..30c7efd 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -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. */ @@ -137,19 +150,6 @@ int StreamSocket::connect(const SockAddr &addr) 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 "< signal_connect_finished; +private: + StreamSocket(SocketHandle, const SockAddr &); +public: StreamSocket(Family, int =0); - bool get_connecting() const { return connecting; } + + bool is_connecting() const { return connecting; } int poll_connect(const Time::TimeDelta &); int connect(const SockAddr &); private: - bool connecting; - - StreamSocket(SocketHandle, const SockAddr &); void on_event(IO::PollEvent); };