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"
36 SharedBuffer<u8> data;
40 OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer<u8> &data_,
41 bool reliable_,bool ack_=false):
43 channelnum(channelnum_),
51 class ConnectionSendThread : public Thread
57 ConnectionSendThread(unsigned int max_packet_size, float timeout);
63 void setParent(Connection *parent)
65 assert(parent != NULL); // Pre-condition
66 m_connection = parent;
69 void setPeerTimeout(float peer_timeout) { m_timeout = peer_timeout; }
72 void runTimeouts(float dtime);
73 void rawSend(const BufferedPacket *p);
74 bool rawSendAsPacket(session_t peer_id, u8 channelnum,
75 const SharedBuffer<u8> &data, bool reliable);
77 void processReliableCommand(ConnectionCommandPtr &c);
78 void processNonReliableCommand(ConnectionCommandPtr &c);
79 void serve(Address bind_address);
80 void connect(Address address);
82 void disconnect_peer(session_t peer_id);
83 void send(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data);
84 void sendReliable(ConnectionCommandPtr &c);
85 void sendToAll(u8 channelnum, const SharedBuffer<u8> &data);
86 void sendToAllReliable(ConnectionCommandPtr &c);
88 void sendPackets(float dtime);
90 void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data,
93 void sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel);
97 Connection *m_connection = nullptr;
98 unsigned int m_max_packet_size;
100 std::queue<OutgoingPacket> m_outgoing_queue;
101 Semaphore m_send_sleep_semaphore;
103 unsigned int m_iteration_packets_avaialble;
104 unsigned int m_max_commands_per_iteration = 1;
105 unsigned int m_max_data_packets_per_iteration;
106 unsigned int m_max_packets_requeued = 256;
109 class ConnectionReceiveThread : public Thread
112 ConnectionReceiveThread(unsigned int max_packet_size);
116 void setParent(Connection *parent)
118 assert(parent); // Pre-condition
119 m_connection = parent;
123 void receive(SharedBuffer<u8> &packetdata, bool &packet_queued);
125 // Returns next data from a buffer if possible
126 // If found, returns true; if not, false.
127 // If found, sets peer_id and dst
128 bool getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst);
130 bool checkIncomingBuffers(
131 Channel *channel, session_t &peer_id, SharedBuffer<u8> &dst);
134 Processes a packet with the basic header stripped out.
136 packetdata: Data in packet (with no base headers)
137 peer_id: peer id of the sender of the packet in question
138 channelnum: channel on which the packet was sent
139 reliable: true if recursing into a reliable packet
141 SharedBuffer<u8> processPacket(Channel *channel,
142 const SharedBuffer<u8> &packetdata, session_t peer_id,
143 u8 channelnum, bool reliable);
145 SharedBuffer<u8> handlePacketType_Control(Channel *channel,
146 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
148 SharedBuffer<u8> handlePacketType_Original(Channel *channel,
149 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
151 SharedBuffer<u8> handlePacketType_Split(Channel *channel,
152 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
154 SharedBuffer<u8> handlePacketType_Reliable(Channel *channel,
155 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
158 struct PacketTypeHandler
160 SharedBuffer<u8> (ConnectionReceiveThread::*handler)(Channel *channel,
161 const SharedBuffer<u8> &packet, Peer *peer, u8 channelnum,
165 static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX];
167 Connection *m_connection = nullptr;