Mercurial > code
annotate C++/SocketTcp.h @ 315:c9356cb38c86
Split sockets into SocketTcp and SocketUdp
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 02 Mar 2015 14:00:48 +0100 |
parents | |
children | 4c0af1143fc4 |
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 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
50 inline unsigned send(const std::string &data) |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
51 { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
52 return send(data.c_str(), data.size()); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
53 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
54 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
55 virtual unsigned recv(void *data, unsigned length) = 0; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
56 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
57 virtual unsigned send(const void *data, unsigned length) = 0; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
58 }; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
59 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
60 /** |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
61 * @class SocketTcp |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
62 * @brief End-user class for TCP sockets |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
63 */ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
64 class SocketTcp final : public SocketAbstractTcp { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
65 public: |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
66 using SocketAbstractTcp::SocketAbstractTcp; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
67 using SocketAbstractTcp::recv; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
68 using SocketAbstractTcp::send; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
69 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
70 SocketTcp accept(); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
71 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
72 SocketTcp accept(SocketAddress &info); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
73 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
74 void connect(const SocketAddress &address); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
75 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
76 unsigned recv(void *data, unsigned length) override; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
77 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
78 unsigned send(const void *data, unsigned length) override; |
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 #endif // !_SOCKET_TCP_NG_3_H_ |