3 Copyright (C) 2013-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 celeron55, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
24 #include "threading/thread.h"
25 #include "connection.h"
32 class ConnectionSendThread : public Thread
38 ConnectionSendThread(unsigned int max_packet_size, float timeout);
44 void setParent(Connection *parent)
46 assert(parent != NULL); // Pre-condition
47 m_connection = parent;
50 void setPeerTimeout(float peer_timeout) { m_timeout = peer_timeout; }
53 void runTimeouts(float dtime);
54 void rawSend(const BufferedPacket &packet);
55 bool rawSendAsPacket(session_t peer_id, u8 channelnum, SharedBuffer<u8> data,
58 void processReliableCommand(ConnectionCommand &c);
59 void processNonReliableCommand(ConnectionCommand &c);
60 void serve(Address bind_address);
61 void connect(Address address);
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);
69 void sendPackets(float dtime);
71 void sendAsPacket(session_t peer_id, u8 channelnum, SharedBuffer<u8> data,
74 void sendAsPacketReliable(BufferedPacket &p, Channel *channel);
78 Connection *m_connection = nullptr;
79 unsigned int m_max_packet_size;
81 std::queue<OutgoingPacket> m_outgoing_queue;
82 Semaphore m_send_sleep_semaphore;
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;
90 class ConnectionReceiveThread : public Thread
93 ConnectionReceiveThread(unsigned int max_packet_size);
97 void setParent(Connection *parent)
99 assert(parent); // Pre-condition
100 m_connection = parent;
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);
111 bool checkIncomingBuffers(
112 Channel *channel, session_t &peer_id, SharedBuffer<u8> &dst);
115 Processes a packet with the basic header stripped out.
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
122 SharedBuffer<u8> processPacket(Channel *channel, SharedBuffer<u8> packetdata,
123 session_t peer_id, u8 channelnum, bool reliable);
125 SharedBuffer<u8> handlePacketType_Control(Channel *channel,
126 SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
128 SharedBuffer<u8> handlePacketType_Original(Channel *channel,
129 SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
131 SharedBuffer<u8> handlePacketType_Split(Channel *channel,
132 SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
134 SharedBuffer<u8> handlePacketType_Reliable(Channel *channel,
135 SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
138 struct PacketTypeHandler
140 SharedBuffer<u8> (ConnectionReceiveThread::*handler)(Channel *channel,
141 SharedBuffer<u8> packet, Peer *peer, u8 channelnum,
145 static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX];
147 Connection *m_connection = nullptr;