1 #ifndef NETWORK_SOCKET_HH |
1 #ifndef NETWORK_SOCKET_HH |
2 #define NETWORK_SOCKET_HH |
2 #define NETWORK_SOCKET_HH |
3 |
3 |
4 #include "../Error.hh" |
4 #include "../Error.hh" |
|
5 #include "Address.hh" |
5 |
6 |
6 #include <ClanLib/Network/Socket/socket.h> |
7 /* |
|
8 * Platform-specific includes |
|
9 */ |
|
10 #ifndef WIN32 |
|
11 // linux |
|
12 #include <sys/types.h> |
|
13 #include <sys/socket.h> |
|
14 #include <unistd.h> |
|
15 #include <fcntl.h> |
|
16 |
|
17 #define closesocket close |
|
18 #else |
|
19 #error "This network code won't compile on win32 :)" |
|
20 #endif |
|
21 |
7 #include <cerrno> |
22 #include <cerrno> |
8 #include <cstring> |
23 #include <cstring> |
9 |
24 |
10 /** |
25 /** |
11 * We use ClanLib's Socket API, but with our own name |
26 * We use ClanLib's Socket API, but with our own extensions... |
12 */ |
27 */ |
13 typedef CL_Socket NetworkSocket; |
28 class NetworkSocket { |
|
29 private: |
|
30 /** The file descriptor */ |
|
31 int fd; |
|
32 |
|
33 /** Socket domain */ |
|
34 int family; |
|
35 |
|
36 /** Socket type */ |
|
37 int socktype; |
|
38 |
|
39 /** Socket protocol */ |
|
40 int protocol; |
|
41 |
|
42 /** |
|
43 * Has the socket been explicitly bind()'d? If so, force ourselves to use this socket in connect(). |
|
44 */ |
|
45 bool bound; |
|
46 |
|
47 /** |
|
48 * Read/write signals |
|
49 */ |
|
50 CL_Signal_v0 _sig_read, _sig_write; |
|
51 |
|
52 public: |
|
53 /** |
|
54 * Construct a socket of the specific type. Family and protocol can be left as NULL, but type should usually |
|
55 * be specified. |
|
56 */ |
|
57 NetworkSocket (int family, int socktype, int protocol = 0); |
|
58 |
|
59 /** |
|
60 * Create a socket from the given pre-existing fd |
|
61 */ |
|
62 NetworkSocket (int fd); |
|
63 |
|
64 /** |
|
65 * Force-close the socket if it's still open |
|
66 */ |
|
67 ~NetworkSocket (void); |
|
68 |
|
69 private: |
|
70 // XXX: nocopy |
|
71 |
|
72 /** |
|
73 * Create a new socket of the given type, unless we already have one |
|
74 */ |
|
75 void lazy_socket (int family, int type, int protocol); |
|
76 |
|
77 /** |
|
78 * Close, ignoring errors |
|
79 */ |
|
80 void force_close (void); |
|
81 |
|
82 public: |
|
83 /** |
|
84 * Get the socket fd... promise not to break it |
|
85 */ |
|
86 int get_socket (void) const { return fd; } |
|
87 |
|
88 /** |
|
89 * Bind to a specific local address |
|
90 */ |
|
91 void bind (const NetworkAddress &addr); |
|
92 |
|
93 /** |
|
94 * Put socket into listen mode |
|
95 */ |
|
96 void listen (int backlog); |
|
97 |
|
98 /** |
|
99 * Get local address |
|
100 */ |
|
101 NetworkAddress get_local_address (void); |
|
102 |
|
103 /** |
|
104 * Get remote address |
|
105 */ |
|
106 NetworkAddress get_remote_address (void); |
|
107 |
|
108 /** |
|
109 * Make send/recv/connect non-blocking |
|
110 */ |
|
111 void set_nonblocking (bool nonblocking); |
|
112 |
|
113 /** |
|
114 * Accept a new connection, optionally giving the connection's source address |
|
115 */ |
|
116 NetworkSocket* accept (NetworkAddress *src); |
|
117 |
|
118 /** |
|
119 * Establish a new connection |
|
120 */ |
|
121 void connect (const NetworkAddress &addr); |
|
122 |
|
123 /** |
|
124 * Send, optionally using the specific destination |
|
125 * |
|
126 * @return number of bytes sent, zero if busy |
|
127 * @throw NetworkSocketError on error |
|
128 */ |
|
129 size_t send (const char *buf, size_t size, const NetworkAddress *dest = NULL); |
|
130 |
|
131 /** |
|
132 * Recv, optionally storing the source in src |
|
133 * |
|
134 * @return number of bytes received, zero if none available |
|
135 * @throw NetworkSocketEOFError if the connection was closed |
|
136 * @throw NetworkSocketError on error |
|
137 */ |
|
138 size_t recv (char *buf, size_t size, NetworkAddress *src = NULL); |
|
139 |
|
140 /** |
|
141 * Close the socket |
|
142 */ |
|
143 void close (void); |
|
144 |
|
145 /** |
|
146 * Triggered when socket becomes readable |
|
147 */ |
|
148 CL_Signal_v0& sig_read (void) { return _sig_read; } |
|
149 |
|
150 /** |
|
151 * Triggered when socket becomes writeable after a send that returned zero |
|
152 */ |
|
153 CL_Signal_v0& sig_write (void) { return _sig_write; } |
|
154 }; |
14 |
155 |
15 /** |
156 /** |
16 * Base class for expcetions thrown by socket methods |
157 * Base class for expcetions thrown by socket methods |
17 */ |
158 */ |
18 class NetworkSocketError : public Error { |
159 class NetworkSocketError : public Error { |
19 protected: |
160 protected: |
20 std::string build_str (const NetworkSocket &socket, const char *op, const char *err); |
161 static std::string build_str (const NetworkSocket &socket, const char *op, const char *err); |
21 |
162 |
|
163 public: |
22 NetworkSocketError (const NetworkSocket &socket, const char *op, const char *err); |
164 NetworkSocketError (const NetworkSocket &socket, const char *op, const char *err); |
23 }; |
165 }; |
24 |
166 |
25 /** |
167 /** |
26 * Errno-enabled exception, most common type of NetworkSocketError |
168 * Errno-enabled exception, most common type of NetworkSocketError |