Move files to subdirectories (#6599)
[oweals/minetest.git] / src / network / connectionthreads.h
1 /*
2 Minetest
3 Copyright (C) 2013-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 celeron55, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #pragma once
22
23 #include <cassert>
24 #include "threading/thread.h"
25 #include "connection.h"
26
27 namespace con
28 {
29
30 class Connection;
31
32 class ConnectionSendThread : public Thread
33 {
34
35 public:
36         friend class UDPPeer;
37
38         ConnectionSendThread(unsigned int max_packet_size, float timeout);
39
40         void *run();
41
42         void Trigger();
43
44         void setParent(Connection *parent)
45         {
46                 assert(parent != NULL); // Pre-condition
47                 m_connection = parent;
48         }
49
50         void setPeerTimeout(float peer_timeout) { m_timeout = peer_timeout; }
51
52 private:
53         void runTimeouts(float dtime);
54         void rawSend(const BufferedPacket &packet);
55         bool rawSendAsPacket(session_t peer_id, u8 channelnum, SharedBuffer<u8> data,
56                         bool reliable);
57
58         void processReliableCommand(ConnectionCommand &c);
59         void processNonReliableCommand(ConnectionCommand &c);
60         void serve(Address bind_address);
61         void connect(Address address);
62         void disconnect();
63         void disconnect_peer(session_t peer_id);
64         void send(session_t peer_id, u8 channelnum, SharedBuffer<u8> data);
65         void sendReliable(ConnectionCommand &c);
66         void sendToAll(u8 channelnum, SharedBuffer<u8> data);
67         void sendToAllReliable(ConnectionCommand &c);
68
69         void sendPackets(float dtime);
70
71         void sendAsPacket(session_t peer_id, u8 channelnum, SharedBuffer<u8> data,
72                         bool ack = false);
73
74         void sendAsPacketReliable(BufferedPacket &p, Channel *channel);
75
76         bool packetsQueued();
77
78         Connection *m_connection = nullptr;
79         unsigned int m_max_packet_size;
80         float m_timeout;
81         std::queue<OutgoingPacket> m_outgoing_queue;
82         Semaphore m_send_sleep_semaphore;
83
84         unsigned int m_iteration_packets_avaialble;
85         unsigned int m_max_commands_per_iteration = 1;
86         unsigned int m_max_data_packets_per_iteration;
87         unsigned int m_max_packets_requeued = 256;
88 };
89
90 class ConnectionReceiveThread : public Thread
91 {
92 public:
93         ConnectionReceiveThread(unsigned int max_packet_size);
94
95         void *run();
96
97         void setParent(Connection *parent)
98         {
99                 assert(parent); // Pre-condition
100                 m_connection = parent;
101         }
102
103 private:
104         void receive();
105
106         // Returns next data from a buffer if possible
107         // If found, returns true; if not, false.
108         // If found, sets peer_id and dst
109         bool getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst);
110
111         bool checkIncomingBuffers(
112                         Channel *channel, session_t &peer_id, SharedBuffer<u8> &dst);
113
114         /*
115                 Processes a packet with the basic header stripped out.
116                 Parameters:
117                         packetdata: Data in packet (with no base headers)
118                         peer_id: peer id of the sender of the packet in question
119                         channelnum: channel on which the packet was sent
120                         reliable: true if recursing into a reliable packet
121         */
122         SharedBuffer<u8> processPacket(Channel *channel, SharedBuffer<u8> packetdata,
123                         session_t peer_id, u8 channelnum, bool reliable);
124
125         SharedBuffer<u8> handlePacketType_Control(Channel *channel,
126                         SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
127                         bool reliable);
128         SharedBuffer<u8> handlePacketType_Original(Channel *channel,
129                         SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
130                         bool reliable);
131         SharedBuffer<u8> handlePacketType_Split(Channel *channel,
132                         SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
133                         bool reliable);
134         SharedBuffer<u8> handlePacketType_Reliable(Channel *channel,
135                         SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
136                         bool reliable);
137
138         struct PacketTypeHandler
139         {
140                 SharedBuffer<u8> (ConnectionReceiveThread::*handler)(Channel *channel,
141                                 SharedBuffer<u8> packet, Peer *peer, u8 channelnum,
142                                 bool reliable);
143         };
144
145         static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX];
146
147         Connection *m_connection = nullptr;
148 };
149 }