]> git.tdb.fi Git - libs/net.git/blob - source/net/clientsocket.h
Add a dynamic receiver class for more flexible packet handling
[libs/net.git] / source / net / clientsocket.h
1 #ifndef MSP_NET_CLIENTSOCKET_H_
2 #define MSP_NET_CLIENTSOCKET_H_
3
4 #include "mspnet_api.h"
5 #include "socket.h"
6
7 namespace Msp {
8 namespace Net {
9
10 /**
11 ClientSockets are used for sending and receiving data over the network.
12 */
13 class MSPNET_API ClientSocket: public Socket
14 {
15 public:
16         /** Emitted when the socket finishes connecting. */
17         sigc::signal<void, const std::exception *> signal_connect_finished;
18
19 protected:
20         bool connecting = false;
21         bool connected = false;
22         std::unique_ptr<SockAddr> peer_addr;
23
24         ClientSocket(const Private &, const SockAddr &);
25         ClientSocket(Family, int, int);
26 public:
27         virtual ~ClientSocket();
28
29         /** Connects to a remote address.  Exact semantics depend on the socket
30         type.  Returns true if the connection was established, false if it's in
31         progress. */
32         virtual bool connect(const SockAddr &) = 0;
33
34         /** Checks the status of a connection being established.  Returns true if
35         the connection was established successfully, false if it's still in
36         progress.  If an error occurred, an exception is thrown. */
37         virtual bool poll_connect(const Time::TimeDelta &) = 0;
38
39         bool is_connecting() const { return connecting; }
40         bool is_connected() const { return connected; }
41
42         void shutdown(IO::Mode);
43
44         const SockAddr &get_peer_address() const;
45 protected:
46         std::size_t do_write(const char *, std::size_t) override;
47         std::size_t do_read(char *, std::size_t) override;
48 };
49
50 } // namespace Net
51 } // namespace Msp
52
53 #endif