-/* $Id$
-
-This file is part of libmspnet
-Copyright © 2008 Mikkosoft Productions, Mikko Rasa
-Distributed under the LGPL
-*/
-
#ifndef MSP_NET_SOCKET_H_
#define MSP_NET_SOCKET_H_
-#include <msp/io/base.h>
+#include <msp/io/eventobject.h>
+#include <msp/io/handle.h>
#include "constants.h"
#include "sockaddr.h"
#include "types.h"
namespace Msp {
namespace Net {
-class Socket: public IO::Base
+class bad_socket_state: public std::logic_error
+{
+public:
+ bad_socket_state(const std::string &w): std::logic_error(w) { }
+ virtual ~bad_socket_state() throw() { }
+};
+
+
+class Socket: public IO::EventObject
{
protected:
SocketHandle handle;
-#ifdef WIN32
IO::Handle event;
-#endif
- bool connected;
- SockAddr *local_addr;
- SockAddr *peer_addr;
+ bool connected;
+ SockAddr *local_addr;
+ SockAddr *peer_addr;
Socket(SocketHandle, const SockAddr &);
Socket(Family, int, int);
~Socket();
void set_block(bool);
- IO::Handle get_event_handle();
+ const IO::Handle &get_event_handle();
bool is_connected() const { return connected; }
+
+ /** Associates the socket with a local address. There must be no existing
+ users of the address. */
void bind(const SockAddr &);
- virtual int connect(const SockAddr &) =0;
- void close();
+
+ /** Connects to a remote address. Exact semantics depend on the socket
+ type. */
+ virtual int connect(const SockAddr &) = 0;
+
+ void set_timeout(const Time::TimeDelta &);
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 *);
+ int set_option(int, int, const void *, socklen_t);
+ int get_option(int, int, void *, socklen_t *) const;
unsigned do_write(const char *, unsigned);
unsigned do_read(char *, unsigned);
};