Luanti 5.18.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 connect(Address address);
70 void disconnect();
71 void disconnect_peer(session_t peer_id);
72 void fix_peer_id(session_t own_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:27
Definition semaphore.h:18
Definition pointer.h:132
Thread(const std::string &name="")
Definition thread.cpp:68
Definition internal.h:380
SharedBuffer< u8 > handlePacketType_Reliable(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1303
void receive(SharedBuffer< u8 > &packetdata, bool &packet_queued)
Definition threads.cpp:914
ConnectionReceiveThread()
Definition threads.cpp:812
void * run()
Definition threads.cpp:817
SharedBuffer< u8 > handlePacketType_Original(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1265
bool getFromBuffers(session_t &peer_id, SharedBuffer< u8 > &dst)
Definition threads.cpp:1069
SharedBuffer< u8 > processPacket(Channel *channel, const SharedBuffer< u8 > &packetdata, session_t peer_id, u8 channelnum, bool reliable)
Definition threads.cpp:1125
SharedBuffer< u8 > handlePacketType_Split(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1279
SharedBuffer< u8 > handlePacketType_Control(Channel *channel, const SharedBuffer< u8 > &packetdata, Peer *peer, u8 channelnum, bool reliable)
Definition threads.cpp:1164
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:1091
RateLimitHelper m_new_peer_ratelimit
Definition threads.h:173
void setParent(Connection *parent)
Definition threads.h:105
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:592
void * run()
Definition threads.cpp:70
void fix_peer_id(session_t own_peer_id)
Definition threads.cpp:572
void processReliableCommand(ConnectionCommandPtr &c)
Definition threads.cpp:393
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:550
void connect(Address address)
Definition threads.cpp:515
void processNonReliableCommand(ConnectionCommandPtr &c)
Definition threads.cpp:461
void setParent(Connection *parent)
Definition threads.h:52
void sendToAll(u8 channelnum, const SharedBuffer< u8 > &data)
Definition threads.cpp:633
float m_timeout
Definition threads.h:89
void disconnect()
Definition threads.cpp:532
void setPeerTimeout(float peer_timeout)
Definition threads.h:58
bool packetsQueued()
Definition threads.cpp:142
void sendToAllReliable(ConnectionCommandPtr &c)
Definition threads.cpp:642
ConnectionSendThread(unsigned int max_packet_size, float timeout)
Definition threads.cpp:52
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:805
void runTimeouts(float dtime, u32 peer_packet_quota)
Definition threads.cpp:169
void Trigger()
Definition threads.cpp:137
bool rawSendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer< u8 > &data, bool reliable)
Definition threads.cpp:339
void sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel)
Definition threads.cpp:319
friend class UDPPeer
Definition threads.h:44
void rawSend(const BufferedPacket *p)
Definition threads.cpp:304
void sendReliable(ConnectionCommandPtr &c)
Definition threads.cpp:624
void sendPackets(float dtime, u32 peer_packet_quota)
Definition threads.cpp:656
Connection * m_connection
Definition threads.h:87
void resendReliable(Channel &channel, const BufferedPacket *k, float resend_timeout)
Definition threads.cpp:277
unsigned int m_max_packet_size
Definition threads.h:88
Definition impl.h:222
Definition impl.h:94
Definition client.h:64
@ PACKET_TYPE_MAX
Definition internal.h:118
std::shared_ptr< BufferedPacket > BufferedPacketPtr
Definition impl.h:89
std::shared_ptr< ConnectionCommand > ConnectionCommandPtr
Definition impl.h:86
u64 getTimeS()
Definition porting.h:174
u16 session_t
Definition networkprotocol.h:27
Definition internal.h:195
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
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:73