Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
threads.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
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#pragma once
7
8/********************************************/
9/* may only be included from in src/network */
10/********************************************/
11
12#include <cassert>
13#include "threading/thread.h"
15
16namespace con
17{
18
19class Connection;
20
22{
27 bool ack;
28
29 OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer<u8> &data_,
30 bool reliable_,bool ack_=false):
31 peer_id(peer_id_),
32 channelnum(channelnum_),
33 data(data_),
34 reliable(reliable_),
35 ack(ack_)
36 {
37 }
38};
39
41{
42
43public:
44 friend class UDPPeer;
45
46 ConnectionSendThread(unsigned int max_packet_size, float timeout);
47
48 void *run();
49
50 void Trigger();
51
52 void setParent(Connection *parent)
53 {
54 assert(parent != NULL); // Pre-condition
55 m_connection = parent;
56 }
57
58 void setPeerTimeout(float peer_timeout) { m_timeout = peer_timeout; }
59
60private:
61 void runTimeouts(float dtime, u32 peer_packet_quota);
62 void resendReliable(Channel &channel, const BufferedPacket *k, float resend_timeout);
63 void rawSend(const BufferedPacket *p);
64 bool rawSendAsPacket(session_t peer_id, u8 channelnum,
65 const SharedBuffer<u8> &data, bool reliable);
66
69 void serve(Address bind_address);
70 void connect(Address address);
71 void disconnect();
72 void disconnect_peer(session_t peer_id);
73 void send(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data);
75 void sendToAll(u8 channelnum, const SharedBuffer<u8> &data);
77
78 void sendPackets(float dtime, u32 peer_packet_quota);
79
80 void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data,
81 bool ack = false);
82
84
85 bool packetsQueued();
86
88 unsigned int m_max_packet_size;
89 float m_timeout;
90 std::queue<OutgoingPacket> m_outgoing_queue;
92
95 unsigned int m_max_packets_requeued = 256;
96};
97
99{
100public:
102
103 void *run();
104
105 void setParent(Connection *parent)
106 {
107 assert(parent); // Pre-condition
108 m_connection = parent;
109 }
110
111private:
112 void receive(SharedBuffer<u8> &packetdata, bool &packet_queued);
113
114 // Returns next data from a buffer if possible
115 // If found, returns true; if not, false.
116 // If found, sets peer_id and dst
117 bool getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst);
118
120 Channel *channel, session_t &peer_id, SharedBuffer<u8> &dst);
121
122 /*
123 Processes a packet with the basic header stripped out.
124 Parameters:
125 packetdata: Data in packet (with no base headers)
126 peer_id: peer id of the sender of the packet in question
127 channelnum: channel on which the packet was sent
128 reliable: true if recursing into a reliable packet
129 */
131 const SharedBuffer<u8> &packetdata, session_t peer_id,
132 u8 channelnum, bool reliable);
133
135 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
136 bool reliable);
138 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
139 bool reliable);
141 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
142 bool reliable);
144 const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
145 bool reliable);
146
148 {
150 const SharedBuffer<u8> &packet, Peer *peer, u8 channelnum,
151 bool reliable);
152 };
153
155 u64 time = 0;
156 int counter = 0;
157 bool logged = false;
158
159 void tick() {
160 u64 now = porting::getTimeS();
161 if (time != now) {
162 time = now;
163 counter = 0;
164 logged = false;
165 }
166 }
167 };
168
170
172
174};
175}
Definition address.h:28
Definition semaphore.h:18
Definition pointer.h:147
Definition thread.h:57
Definition internal.h:362
Definition threads.h:99
SharedBuffer< u8 > handlePacketType_Reliable(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1293
void receive(SharedBuffer< u8 > &packetdata, bool &packet_queued)
Definition threads.cpp:904
ConnectionReceiveThread()
Definition threads.cpp:802
void * run()
Definition threads.cpp:807
SharedBuffer< u8 > handlePacketType_Original(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1255
bool getFromBuffers(session_t &peer_id, SharedBuffer< u8 > &dst)
Definition threads.cpp:1059
SharedBuffer< u8 > processPacket(Channel *channel, const SharedBuffer< u8 > &packetdata, session_t peer_id, u8 channelnum, bool reliable)
Definition threads.cpp:1115
SharedBuffer< u8 > handlePacketType_Split(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1269
SharedBuffer< u8 > handlePacketType_Control(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1154
Connection * m_connection
Definition threads.h:171
static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX]
Definition threads.h:169
bool checkIncomingBuffers(Channel *channel, session_t &peer_id, SharedBuffer< u8 > &dst)
Definition threads.cpp:1081
RateLimitHelper m_new_peer_ratelimit
Definition threads.h:173
void setParent(Connection *parent)
Definition threads.h:105
Definition threads.h:41
unsigned int m_max_data_packets_per_iteration
Definition threads.h:94
Semaphore m_send_sleep_semaphore
Definition threads.h:91
void send(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data)
Definition threads.cpp:582
void * run()
Definition threads.cpp:66
void processReliableCommand(ConnectionCommandPtr &c)
Definition threads.cpp:388
std::queue< OutgoingPacket > m_outgoing_queue
Definition threads.h:90
unsigned int m_iteration_packets_avaialble
Definition threads.h:93
void disconnect_peer(session_t peer_id)
Definition threads.cpp:560
void connect(Address address)
Definition threads.cpp:517
void processNonReliableCommand(ConnectionCommandPtr &c)
Definition threads.cpp:448
void setParent(Connection *parent)
Definition threads.h:52
void sendToAll(u8 channelnum, const SharedBuffer< u8 > &data)
Definition threads.cpp:623
float m_timeout
Definition threads.h:89
void serve(Address bind_address)
Definition threads.cpp:503
void disconnect()
Definition threads.cpp:542
void setPeerTimeout(float peer_timeout)
Definition threads.h:58
bool packetsQueued()
Definition threads.cpp:138
void sendToAllReliable(ConnectionCommandPtr &c)
Definition threads.cpp:632
ConnectionSendThread(unsigned int max_packet_size, float timeout)
Definition threads.cpp:48
unsigned int m_max_packets_requeued
Definition threads.h:95
void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data, bool ack=false)
Definition threads.cpp:795
void runTimeouts(float dtime, u32 peer_packet_quota)
Definition threads.cpp:165
void Trigger()
Definition threads.cpp:133
bool rawSendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data, bool reliable)
Definition threads.cpp:334
void sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel)
Definition threads.cpp:314
void rawSend(const BufferedPacket *p)
Definition threads.cpp:299
void sendReliable(ConnectionCommandPtr &c)
Definition threads.cpp:614
void sendPackets(float dtime, u32 peer_packet_quota)
Definition threads.cpp:646
Connection * m_connection
Definition threads.h:87
void resendReliable(Channel &channel, const BufferedPacket *k, float resend_timeout)
Definition threads.cpp:272
unsigned int m_max_packet_size
Definition threads.h:88
Definition impl.h:223
Definition impl.h:95
Definition internal.h:463
Definition client.h:59
@ PACKET_TYPE_MAX
Definition internal.h:110
std::shared_ptr< BufferedPacket > BufferedPacketPtr
Definition impl.h:90
std::shared_ptr< ConnectionCommand > ConnectionCommandPtr
Definition impl.h:87
u64 getTimeS()
Definition porting.h:168
u16 session_t
Definition networkprotocol.h:22
Definition internal.h:180
SharedBuffer< u8 >(ConnectionReceiveThread::* handler)(Channel *channel, const SharedBuffer< u8 > &packet, Peer *peer, u8 channelnum, bool reliable)
Definition threads.h:149
void tick()
Definition threads.h:159
Definition threads.h:22
u8 channelnum
Definition threads.h:24
bool ack
Definition threads.h:27
session_t peer_id
Definition threads.h:23
bool reliable
Definition threads.h:26
SharedBuffer< u8 > data
Definition threads.h:25
OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer< u8 > &data_, bool reliable_, bool ack_=false)
Definition threads.h:29
static std::string p(std::string path)
Definition test_filesys.cpp:53