Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
threads.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"
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 internal.h:377
Definition threads.h:114
SharedBuffer< u8 > handlePacketType_Reliable(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1308
void receive(SharedBuffer< u8 > &packetdata, bool &packet_queued)
Definition threads.cpp:919
ConnectionReceiveThread()
Definition threads.cpp:817
void * run()
Definition threads.cpp:822
SharedBuffer< u8 > handlePacketType_Original(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1270
bool getFromBuffers(session_t &peer_id, SharedBuffer< u8 > &dst)
Definition threads.cpp:1074
SharedBuffer< u8 > processPacket(Channel *channel, const SharedBuffer< u8 > &packetdata, session_t peer_id, u8 channelnum, bool reliable)
Definition threads.cpp:1130
SharedBuffer< u8 > handlePacketType_Split(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1284
SharedBuffer< u8 > handlePacketType_Control(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1169
Connection * m_connection
Definition threads.h:186
static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX]
Definition threads.h:184
bool checkIncomingBuffers(Channel *channel, session_t &peer_id, SharedBuffer< u8 > &dst)
Definition threads.cpp:1096
RateLimitHelper m_new_peer_ratelimit
Definition threads.h:188
void setParent(Connection *parent)
Definition threads.h:120
Definition threads.h:56
unsigned int m_max_data_packets_per_iteration
Definition threads.h:109
Semaphore m_send_sleep_semaphore
Definition threads.h:106
void send(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data)
Definition threads.cpp:597
void * run()
Definition threads.cpp:81
void processReliableCommand(ConnectionCommandPtr &c)
Definition threads.cpp:403
std::queue< OutgoingPacket > m_outgoing_queue
Definition threads.h:105
unsigned int m_iteration_packets_avaialble
Definition threads.h:108
void disconnect_peer(session_t peer_id)
Definition threads.cpp:575
void connect(Address address)
Definition threads.cpp:532
void processNonReliableCommand(ConnectionCommandPtr &c)
Definition threads.cpp:463
void setParent(Connection *parent)
Definition threads.h:67
void sendToAll(u8 channelnum, const SharedBuffer< u8 > &data)
Definition threads.cpp:638
float m_timeout
Definition threads.h:104
void serve(Address bind_address)
Definition threads.cpp:518
void disconnect()
Definition threads.cpp:557
void setPeerTimeout(float peer_timeout)
Definition threads.h:73
bool packetsQueued()
Definition threads.cpp:153
void sendToAllReliable(ConnectionCommandPtr &c)
Definition threads.cpp:647
ConnectionSendThread(unsigned int max_packet_size, float timeout)
Definition threads.cpp:63
unsigned int m_max_packets_requeued
Definition threads.h:110
void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data, bool ack=false)
Definition threads.cpp:810
void runTimeouts(float dtime, u32 peer_packet_quota)
Definition threads.cpp:180
void Trigger()
Definition threads.cpp:148
bool rawSendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data, bool reliable)
Definition threads.cpp:349
void sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel)
Definition threads.cpp:329
void rawSend(const BufferedPacket *p)
Definition threads.cpp:314
void sendReliable(ConnectionCommandPtr &c)
Definition threads.cpp:629
void sendPackets(float dtime, u32 peer_packet_quota)
Definition threads.cpp:661
Connection * m_connection
Definition threads.h:102
void resendReliable(Channel &channel, const BufferedPacket *k, float resend_timeout)
Definition threads.cpp:287
unsigned int m_max_packet_size
Definition threads.h:103
Definition impl.h:238
Definition impl.h:110
Definition internal.h:478
Definition client.h:72
@ PACKET_TYPE_MAX
Definition internal.h:125
std::shared_ptr< BufferedPacket > BufferedPacketPtr
Definition impl.h:105
std::shared_ptr< ConnectionCommand > ConnectionCommandPtr
Definition impl.h:102
u64 getTimeS()
Definition porting.h:183
u16 session_t
Definition networkprotocol.h:259
Definition internal.h:195
SharedBuffer< u8 >(ConnectionReceiveThread::* handler)(Channel *channel, const SharedBuffer< u8 > &packet, Peer *peer, u8 channelnum, bool reliable)
Definition threads.h:164
void tick()
Definition threads.h:174
Definition threads.h:37
u8 channelnum
Definition threads.h:39
bool ack
Definition threads.h:42
session_t peer_id
Definition threads.h:38
bool reliable
Definition threads.h:41
SharedBuffer< u8 > data
Definition threads.h:40
OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer< u8 > &data_, bool reliable_, bool ack_=false)
Definition threads.h:44
static std::string p(std::string path)
Definition test_filesys.cpp:66