]> git.localhorst.tv Git - blank.git/blob - src/net/Connection.hpp
ed7a946aab01d2296123f9f68229d46a2b9b4b48
[blank.git] / src / net / Connection.hpp
1 #ifndef BLANK_NET_CONNECTION_HPP_
2 #define BLANK_NET_CONNECTION_HPP_
3
4 #include "Packet.hpp"
5 #include "../app/IntervalTimer.hpp"
6
7 #include <cstdint>
8 #include <SDL_net.h>
9
10
11 namespace blank {
12
13 class ConnectionHandler;
14
15 class Connection {
16
17 public:
18         explicit Connection(const IPaddress &);
19
20         void SetHandler(ConnectionHandler *h) noexcept { handler = h; }
21         void RemoveHandler() noexcept { handler = nullptr; }
22         bool HasHandler() const noexcept { return handler; }
23         ConnectionHandler &Handler() noexcept { return *handler; }
24
25         const IPaddress &Address() const noexcept { return addr; }
26
27         bool Matches(const IPaddress &) const noexcept;
28
29         bool ShouldPing() const noexcept;
30         bool TimedOut() const noexcept;
31
32         void Close() noexcept { closed = true; }
33         bool Closed() const noexcept { return closed; }
34
35         void Update(int dt);
36
37         std::uint16_t SendPing(UDPpacket &, UDPsocket);
38
39         std::uint16_t Send(UDPpacket &, UDPsocket);
40         void Received(const UDPpacket &);
41
42 private:
43         void FlagSend() noexcept;
44         void FlagRecv() noexcept;
45
46 private:
47         ConnectionHandler *handler;
48         IPaddress addr;
49         IntervalTimer send_timer;
50         IntervalTimer recv_timer;
51
52         Packet::TControl ctrl_out;
53         Packet::TControl ctrl_in;
54
55         bool closed;
56
57 };
58
59 }
60
61 #endif