]> git.tdb.fi Git - libs/net.git/blob - source/socket.h
Update handle types
[libs/net.git] / source / socket.h
1 #ifndef MSP_NET_SOCKET_H_
2 #define MSP_NET_SOCKET_H_
3
4 #include <msp/io/eventobject.h>
5 #include <msp/io/handle.h>
6 #include "constants.h"
7 #include "sockaddr.h"
8 #include "types.h"
9
10 namespace Msp {
11 namespace Net {
12
13 class Socket: public IO::EventObject
14 {
15 protected:
16         SocketHandle handle;
17         IO::Handle event;
18         bool connected;
19         SockAddr *local_addr;
20         SockAddr *peer_addr;
21
22         Socket(SocketHandle, const SockAddr &);
23         Socket(Family, int, int);
24 public:
25         ~Socket();
26
27         void set_block(bool);
28         const IO::Handle &get_event_handle();
29
30         bool is_connected() const { return connected; }
31         
32         /** Associates the socket with a local address.  There must be no existing
33         users of the address. */
34         void bind(const SockAddr &);
35
36         /** Connects to a remote address.  Exact semantics depend on the socket
37         type. */
38         virtual int connect(const SockAddr &) = 0;
39
40         /// Closes the socket.  Most operations will throw an exception after this.
41         void close();
42
43         void set_timeout(const Time::TimeDelta &);
44         const SockAddr &get_local_address() const;
45         const SockAddr &get_peer_address() const;
46 protected:
47         void check_state(bool) const;
48         int set_option(int, int, const void *, socklen_t);
49         int get_option(int, int, void *, socklen_t *) const;
50         unsigned do_write(const char *, unsigned);
51         unsigned do_read(char *, unsigned);
52 };
53
54 } // namespace Net
55 } // namespace Msp
56
57 #endif