Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
connectionthreads.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2013-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4Copyright (C) 2017 celeron55, Loic Blot <loic.blot@unix-experience.fr>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU Lesser General Public License as published by
8the Free Software Foundation; either version 2.1 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Lesser General Public License for more details.
15
16You should have received a copy of the GNU Lesser General Public License along
17with this program; if not, write to the Free Software Foundation, Inc.,
1851 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21#pragma once
22
23/********************************************/
24/* may only be included from in src/network */
25/********************************************/
26
27#include <cassert>
28#include "threading/thread.h"
29#include "connection_internal.h"
30
31namespace con
32{
33
34class Connection;
35
37{
42 bool ack;
43
44 OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer<u8> &data_,
45 bool reliable_,bool ack_=false):
46 peer_id(peer_id_),
47 channelnum(channelnum_),
48 data(data_),
49 reliable(reliable_),
50 ack(ack_)
51 {
52 }
53};
54
56{
57
58public:
59 friend class UDPPeer;
60
61 ConnectionSendThread(unsigned int max_packet_size, float timeout);
62
63 void *run();
64
65 void Trigger();
66
67 void setParent(Connection *parent)
68 {
69 assert(parent != NULL); // Pre-condition
70 m_connection = parent;
71 }
72
73 void setPeerTimeout(float peer_timeout) { m_timeout = peer_timeout; }
74
75private:
76 void runTimeouts(float dtime, u32 peer_packet_quota);
77 void resendReliable(Channel &channel, const BufferedPacket *k, float resend_timeout);
78 void rawSend(const BufferedPacket *p);
79 bool rawSendAsPacket(session_t peer_id, u8 channelnum,
80 const SharedBuffer<u8> &data, bool reliable);
81
84 void serve(Address bind_address);
85 void connect(Address address);
86 void disconnect();
87 void disconnect_peer(session_t peer_id);
88 void send(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data);
90 void sendToAll(u8 channelnum, const SharedBuffer<u8> &data);
92
93 void sendPackets(float dtime, u32 peer_packet_quota);
94
95 void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data,
96 bool ack = false);
97
99
100 bool packetsQueued();
101
103 unsigned int m_max_packet_size;
105 std::queue<OutgoingPacket> m_outgoing_queue;
107
110 unsigned int m_max_packets_requeued = 256;
111};
112
114{
115public:
117
118 void *run();
119
120 void setParent(Connection *parent)
121 {
122 assert(parent); // Pre-condition
123 m_connection = parent;
124 }
125
126private:
127 void receive(SharedBuffer<u8> &packetdata, bool &packet_queued);
128
129 // Returns next data from a buffer if possible
130 // If found, returns true; if not, false.
131 // If found, sets peer_id and dst
132 bool getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst);
133
135 Channel *channel, session_t &peer_id, SharedBuffer<u8> &dst);
136
137 /*
138 Processes a packet with the basic header stripped out.
139 Parameters:
140 packetdata: Data in packet (with no base headers)
141 peer_id: peer id of the sender of the packet in question
142 channelnum: channel on which the packet was sent
143 reliable: true if recursing into a reliable packet
144 */
146 const SharedBuffer<u8> &packetdata, session_t peer_id,
147 u8 channelnum, bool reliable);
148
150 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
151 bool reliable);
153 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
154 bool reliable);
156 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
157 bool reliable);
159 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
160 bool reliable);
161
163 {
165 const SharedBuffer<u8> &packet, Peer *peer, u8 channelnum,
166 bool reliable);
167 };
168
170 u64 time = 0;
171 int counter = 0;
172 bool logged = false;
173
174 void tick() {
175 u64 now = porting::getTimeS();
176 if (time != now) {
177 time = now;
178 counter = 0;
179 logged = false;
180 }
181 }
182 };
183
185
187
189};
190}
Definition: address.h:43
Definition: semaphore.h:33
Definition: pointer.h:162
Definition: thread.h:57
Definition: connection_internal.h:343
Definition: connectionthreads.h:114
SharedBuffer< u8 > handlePacketType_Reliable(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition: connectionthreads.cpp:1290
void receive(SharedBuffer< u8 > &packetdata, bool &packet_queued)
Definition: connectionthreads.cpp:901
ConnectionReceiveThread()
Definition: connectionthreads.cpp:799
void * run()
Definition: connectionthreads.cpp:804
SharedBuffer< u8 > handlePacketType_Original(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition: connectionthreads.cpp:1252
bool getFromBuffers(session_t &peer_id, SharedBuffer< u8 > &dst)
Definition: connectionthreads.cpp:1056
SharedBuffer< u8 > processPacket(Channel *channel, const SharedBuffer< u8 > &packetdata, session_t peer_id, u8 channelnum, bool reliable)
Definition: connectionthreads.cpp:1112
SharedBuffer< u8 > handlePacketType_Split(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition: connectionthreads.cpp:1266
SharedBuffer< u8 > handlePacketType_Control(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition: connectionthreads.cpp:1151
Connection * m_connection
Definition: connectionthreads.h:186
static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX]
Definition: connectionthreads.h:184
bool checkIncomingBuffers(Channel *channel, session_t &peer_id, SharedBuffer< u8 > &dst)
Definition: connectionthreads.cpp:1078
RateLimitHelper m_new_peer_ratelimit
Definition: connectionthreads.h:188
void setParent(Connection *parent)
Definition: connectionthreads.h:120
Definition: connectionthreads.h:56
unsigned int m_max_data_packets_per_iteration
Definition: connectionthreads.h:109
Semaphore m_send_sleep_semaphore
Definition: connectionthreads.h:106
void send(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data)
Definition: connectionthreads.cpp:579
void * run()
Definition: connectionthreads.cpp:72
void processReliableCommand(ConnectionCommandPtr &c)
Definition: connectionthreads.cpp:385
std::queue< OutgoingPacket > m_outgoing_queue
Definition: connectionthreads.h:105
unsigned int m_iteration_packets_avaialble
Definition: connectionthreads.h:108
void disconnect_peer(session_t peer_id)
Definition: connectionthreads.cpp:557
void connect(Address address)
Definition: connectionthreads.cpp:514
void processNonReliableCommand(ConnectionCommandPtr &c)
Definition: connectionthreads.cpp:445
void setParent(Connection *parent)
Definition: connectionthreads.h:67
void sendToAll(u8 channelnum, const SharedBuffer< u8 > &data)
Definition: connectionthreads.cpp:620
float m_timeout
Definition: connectionthreads.h:104
void serve(Address bind_address)
Definition: connectionthreads.cpp:500
void disconnect()
Definition: connectionthreads.cpp:539
void setPeerTimeout(float peer_timeout)
Definition: connectionthreads.h:73
bool packetsQueued()
Definition: connectionthreads.cpp:144
void sendToAllReliable(ConnectionCommandPtr &c)
Definition: connectionthreads.cpp:629
unsigned int m_max_packets_requeued
Definition: connectionthreads.h:110
void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data, bool ack=false)
Definition: connectionthreads.cpp:792
void runTimeouts(float dtime, u32 peer_packet_quota)
Definition: connectionthreads.cpp:171
void Trigger()
Definition: connectionthreads.cpp:139
bool rawSendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data, bool reliable)
Definition: connectionthreads.cpp:331
void sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel)
Definition: connectionthreads.cpp:312
void rawSend(const BufferedPacket *p)
Definition: connectionthreads.cpp:297
void sendReliable(ConnectionCommandPtr &c)
Definition: connectionthreads.cpp:611
void sendPackets(float dtime, u32 peer_packet_quota)
Definition: connectionthreads.cpp:643
Connection * m_connection
Definition: connectionthreads.h:102
void resendReliable(Channel &channel, const BufferedPacket *k, float resend_timeout)
Definition: connectionthreads.cpp:270
unsigned int m_max_packet_size
Definition: connectionthreads.h:103
Definition: connection.h:255
Definition: connection.h:122
Definition: connection_internal.h:444
Definition: client.h:73
@ PACKET_TYPE_MAX
Definition: connection_internal.h:126
std::shared_ptr< BufferedPacket > BufferedPacketPtr
Definition: connection.h:117
std::shared_ptr< ConnectionCommand > ConnectionCommandPtr
Definition: connection.h:114
u64 getTimeS()
Definition: porting.h:183
u16 session_t
Definition: networkprotocol.h:251
Definition: connection_internal.h:170
Definition: connectionthreads.h:163
SharedBuffer< u8 >(ConnectionReceiveThread::* handler)(Channel *channel, const SharedBuffer< u8 > &packet, Peer *peer, u8 channelnum, bool reliable)
Definition: connectionthreads.h:164
Definition: connectionthreads.h:169
int counter
Definition: connectionthreads.h:171
u64 time
Definition: connectionthreads.h:170
void tick()
Definition: connectionthreads.h:174
bool logged
Definition: connectionthreads.h:172
Definition: connectionthreads.h:37
u8 channelnum
Definition: connectionthreads.h:39
bool ack
Definition: connectionthreads.h:42
session_t peer_id
Definition: connectionthreads.h:38
bool reliable
Definition: connectionthreads.h:41
SharedBuffer< u8 > data
Definition: connectionthreads.h:40
OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer< u8 > &data_, bool reliable_, bool ack_=false)
Definition: connectionthreads.h:44
static std::string p(std::string path)
Definition: test_filesys.cpp:64