*/
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
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);
}
/**
-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()
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)
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
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);
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.
*/
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';
class StreamSocket: public Socket
{
friend class StreamListenSocket;
+private:
+ bool connecting;
+
public:
/**
Emitted when the socket finishes connecting. The argument is a standard
*/
sigc::signal<void, int> 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);
};