Mercurial > code
annotate C++/SocketTcp.h @ 318:68ae6d7dea1f
Sockets: improve errors
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 06 Mar 2015 17:30:16 +0100 |
parents | 4c0af1143fc4 |
children | cba77da58496 |
rev | line source |
---|---|
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
2 * SocketTcp.h -- portable C++ socket wrappers |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
19 #ifndef _SOCKET_TCP_NG_3_H_ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
20 #define _SOCKET_TCP_NG_3_H_ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
21 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
22 #include "Socket.h" |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
23 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
24 /** |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
25 * @class SocketAbstractTcp |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
26 * @brief Base class for TCP sockets |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
27 */ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
28 class SocketAbstractTcp : public Socket { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
29 public: |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
30 using Socket::Socket; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
31 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
32 inline SocketAbstractTcp(int domain, int protocol) |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
33 : Socket(domain, SOCK_STREAM, protocol) |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
34 { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
35 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
36 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
37 void listen(int max = 128); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
38 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
39 inline std::string recv(unsigned count) |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
40 { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
41 std::string result; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
42 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
43 result.resize(count); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
44 auto n = recv(const_cast<char *>(result.data()), count); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
45 result.resize(n); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
46 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
47 return result; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
48 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
49 |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
50 inline std::string waitRecv(unsigned count, int timeout) |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
51 { |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
52 std::string result; |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
53 |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
54 result.resize(count); |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
55 auto n = waitRecv(const_cast<char *>(result.data()), count, timeout); |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
56 result.resize(n); |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
57 |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
58 return result; |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
59 } |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
60 |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
61 inline unsigned send(const std::string &data) |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
62 { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
63 return send(data.c_str(), data.size()); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
64 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
65 |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
66 inline unsigned waitSend(const std::string &data, int timeout) |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
67 { |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
68 return waitSend(data.c_str(), data.size(), timeout); |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
69 } |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
70 |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
71 virtual unsigned recv(void *data, unsigned length) = 0; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
72 |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
73 virtual unsigned waitRecv(void *data, unsigned length, int timeout) = 0; |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
74 |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
75 virtual unsigned send(const void *data, unsigned length) = 0; |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
76 |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
77 virtual unsigned waitSend(const void *data, unsigned length, int timeout) = 0; |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
78 }; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
79 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
80 /** |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
81 * @class SocketTcp |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
82 * @brief End-user class for TCP sockets |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
83 */ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
84 class SocketTcp final : public SocketAbstractTcp { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
85 public: |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
86 using SocketAbstractTcp::SocketAbstractTcp; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
87 using SocketAbstractTcp::recv; |
318
68ae6d7dea1f
Sockets: improve errors
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
88 using SocketAbstractTcp::waitRecv; |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
89 using SocketAbstractTcp::send; |
318
68ae6d7dea1f
Sockets: improve errors
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
90 using SocketAbstractTcp::waitSend; |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
91 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
92 SocketTcp accept(); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
93 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
94 SocketTcp accept(SocketAddress &info); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
95 |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
96 SocketTcp waitAccept(int timeout); |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
97 |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
98 SocketTcp waitAccept(SocketAddress &info, int timeout); |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
99 |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
100 void connect(const SocketAddress &address); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
101 |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
102 void waitConnect(const SocketAddress &address, int timeout); |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
103 |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
104 unsigned recv(void *data, unsigned length) override; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
105 |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
106 unsigned waitRecv(void *data, unsigned length, int timeout) override; |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
107 |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
108 unsigned send(const void *data, unsigned length) override; |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
109 |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
110 unsigned waitSend(const void *data, unsigned length, int timeout) override; |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
111 }; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
112 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
113 #endif // !_SOCKET_TCP_NG_3_H_ |