Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
Network.h
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2016 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#pragma once
23
24#ifdef USE_SOCKETW
25
26#include "Application.h"
27#include "RoRnet.h"
28
29#include <SocketW.h>
30
31#include <atomic>
32#include <condition_variable>
33#include <deque>
34#include <list>
35#include <mutex>
36#include <queue>
37#include <string>
38#include <thread>
39#include <vector>
40
41namespace RoR {
42
45
47{
48 std::string title;
49 CURLcode curl_result = CURLE_OK;
50 long http_response = 0;
51};
52
53// ----------------------- Network messages (packed) -------------------------
54
55#pragma pack(push, 1)
56
64
66{
67 int32_t command;
68};
69
78
80{
81 int32_t command;
82 int32_t source_id;
83 int32_t stream_id;
84 int32_t position;
85};
86
92
98
99#pragma pack(pop)
100
101// ------------------------ End of network messages --------------------------
102
109
111{
112public:
113 bool StartConnecting();
114 void StopConnecting();
115 void Disconnect();
116
117 void AddPacket(int streamid, int type, int len, const char *content);
118 void AddLocalStream(RoRnet::StreamRegister *reg, int size);
119
120 std::vector<NetRecvPacket> GetIncomingStreamData();
121
122 int GetUID();
123 int GetNetQuality();
124
125 Ogre::String GetTerrainName();
126
127 int GetUserColor();
128 std::string GetUsername();
130 std::vector<RoRnet::UserInfo> GetUserInfos();
131 std::vector<BitMask_t> GetAllUsersPeerOpts();
132 bool GetUserInfo(int uid, RoRnet::UserInfo &result);
133 bool GetUserPeerOpts(int uid, BitMask_t& result);
134 bool GetDisconnectedUserInfo(int uid, RoRnet::UserInfo &result);
135 bool GetAnyUserInfo(int uid, RoRnet::UserInfo &result);
136 bool FindUserInfo(std::string const& username, RoRnet::UserInfo &result);
137 Ogre::ColourValue GetPlayerColor(int color_num);
140
141 void BroadcastChatMsg(const char* msg);
142 void WhisperChatMsg(RoRnet::UserInfo const& user, const char* msg);
143
144 std::string UserAuthToStringShort(RoRnet::UserInfo const &user);
145 std::string UserAuthToStringLong(RoRnet::UserInfo const &user);
146
149 void SetLocalUserData(RoRnet::UserInfo const& user);
150 void AddBotUserInfo(RoRnet::UserInfo const& user);
151 void RemoveBotUserInfo(int uid);
153
154private:
155 void PushNetMessage(MsgType type, std::string const & message);
156 void SetNetQuality(int quality);
157 bool SendMessageRaw(char *buffer, int msgsize);
158 bool SendNetMessage(int type, unsigned int streamid, int len, char* content);
159 void QueueStreamData(RoRnet::Header &header, char *buffer, size_t buffer_len);
160 int ReceiveMessage(RoRnet::Header *head, char* content, int bufferlen);
161 void CouldNotConnect(std::string const & msg, bool close_socket = true);
162
163 bool ConnectThread();
164 void SendThread();
165 void RecvThread();
166
167 // Variables
168
169 SWInetSocket m_socket;
170
173 std::vector<RoRnet::UserInfo> m_users;
174 std::vector<BitMask_t> m_users_peeropts;
175 std::vector<RoRnet::UserInfo> m_disconnected_users;
176
177 std::string m_username; // Shadows GVar 'mp_player_name' for multithreaded access.
178 std::string m_net_host; // Shadows GVar 'mp_server_host' for multithreaded access.
179 std::string m_password; // Shadows GVar 'mp_server_password' for multithreaded access.
180 std::string m_token; // Shadows GVar 'mp_player_token' for multithreaded access.
181 int m_net_port; // Shadows GVar 'mp_server_port' for multithreaded access.
182 int m_uid;
184
185 std::thread m_send_thread;
186 std::thread m_recv_thread;
187 std::thread m_connect_thread;
188
189 std::string m_status_message;
190 std::atomic<bool> m_shutdown;
191 std::atomic<int> m_net_quality;
192 int m_stream_id = 10;
193
194 std::mutex m_users_mutex;
198
199 std::condition_variable m_send_packet_available_cv;
200
201 std::vector<NetRecvPacket> m_recv_packet_buffer;
202 std::deque <NetSendPacket> m_send_packet_buffer;
203};
204
206
207} // namespace RoR
208
209#endif // USE_SOCKETW
Central state/object manager and communications hub.
#define CHARACTER_ANIM_NAME_LEN
Definition Application.h:42
uint32_t BitMask_t
Definition BitFlags.h:7
bool GetUserPeerOpts(int uid, BitMask_t &result)
Definition Network.cpp:740
std::deque< NetSendPacket > m_send_packet_buffer
Definition Network.h:202
Ogre::ColourValue GetPlayerColor(int color_num)
Definition Network.cpp:95
std::mutex m_users_mutex
Definition Network.h:194
int GetUserColor()
Definition Network.cpp:696
bool FindUserInfo(std::string const &username, RoRnet::UserInfo &result)
Definition Network.cpp:790
void AddLocalStream(RoRnet::StreamRegister *reg, int size)
Definition Network.cpp:671
void SendThread()
Definition Network.cpp:215
int ReceiveMessage(RoRnet::Header *head, char *content, int bufferlen)
Definition Network.cpp:174
std::string UserAuthToStringLong(RoRnet::UserInfo const &user)
Definition Network.cpp:879
std::string m_username
Definition Network.h:177
int m_authlevel
Definition Network.h:183
std::string GetUsername()
Definition Network.cpp:702
std::thread m_connect_thread
Definition Network.h:187
std::string m_password
Definition Network.h:179
int GetNetQuality()
Definition Network.cpp:118
void SetLocalUserData(RoRnet::UserInfo const &user)
Definition Network.cpp:892
void RemovePeerOptions(PeerOptionsRequest *rq)
Definition Network.cpp:822
SWInetSocket m_socket
Definition Network.h:169
void CouldNotConnect(std::string const &msg, bool close_socket=true)
Definition Network.cpp:383
bool GetAnyUserInfo(int uid, RoRnet::UserInfo &result)
Also considers local client.
Definition Network.cpp:768
bool SendMessageRaw(char *buffer, int msgsize)
Definition Network.cpp:128
std::vector< BitMask_t > GetAllUsersPeerOpts()
Definition Network.cpp:720
std::vector< RoRnet::UserInfo > m_disconnected_users
Definition Network.h:175
int m_net_port
Definition Network.h:181
std::atomic< int > m_net_quality
Definition Network.h:191
void BroadcastChatMsg(const char *msg)
Definition Network.cpp:839
std::vector< NetRecvPacket > GetIncomingStreamData()
Definition Network.cpp:683
std::mutex m_send_packetqueue_mutex
Definition Network.h:197
std::string m_net_host
Definition Network.h:178
bool GetDisconnectedUserInfo(int uid, RoRnet::UserInfo &result)
Definition Network.cpp:754
std::string UserAuthToStringShort(RoRnet::UserInfo const &user)
Definition Network.cpp:869
std::atomic< bool > m_shutdown
Definition Network.h:190
std::vector< NetRecvPacket > m_recv_packet_buffer
Definition Network.h:201
bool StartConnecting()
Launches connecting on background.
Definition Network.cpp:395
void SetNetQuality(int quality)
Definition Network.cpp:113
bool SendNetMessage(int type, unsigned int streamid, int len, char *content)
Definition Network.cpp:141
void RecvThread()
Definition Network.cpp:239
int m_stream_id
Counter.
Definition Network.h:192
std::thread m_send_thread
Definition Network.h:185
void AddPacket(int streamid, int type, int len, const char *content)
Definition Network.cpp:617
RoRnet::ServerInfo m_server_settings
Definition Network.h:171
void Disconnect()
Definition Network.cpp:573
std::thread m_recv_thread
Definition Network.h:186
void StopConnecting()
Definition Network.cpp:420
RoRnet::UserInfo GetLocalUserData()
Definition Network.cpp:708
void AddBotUserInfo(RoRnet::UserInfo const &user)
Definition Network.cpp:900
std::mutex m_recv_packetqueue_mutex
Definition Network.h:196
void PushNetMessage(MsgType type, std::string const &message)
Definition Network.cpp:88
std::vector< BitMask_t > m_users_peeropts
See RoRnet::PeerOptions.
Definition Network.h:174
RoRnet::UserInfo m_userdata
Definition Network.h:172
std::mutex m_userdata_mutex
Definition Network.h:195
Ogre::String GetTerrainName()
Definition Network.cpp:691
bool ConnectThread()
Definition Network.cpp:428
void QueueStreamData(RoRnet::Header &header, char *buffer, size_t buffer_len)
Definition Network.cpp:164
void RemoveBotUserInfo(int uid)
Definition Network.cpp:908
void WhisperChatMsg(RoRnet::UserInfo const &user, const char *msg)
Definition Network.cpp:851
bool GetUserInfo(int uid, RoRnet::UserInfo &result)
Definition Network.cpp:726
std::vector< RoRnet::UserInfo > GetUserInfos()
Definition Network.cpp:714
std::vector< RoRnet::UserInfo > m_users
Definition Network.h:173
std::string m_status_message
Definition Network.h:189
void AddPeerOptions(PeerOptionsRequest *rq)
Definition Network.cpp:805
std::condition_variable m_send_packet_available_cv
Definition Network.h:199
std::string m_token
Definition Network.h:180
MsgType
Global gameplay message loop, see struct Message in GameContext.h.
Definition Application.h:77
NetCharacterCmd
Definition Network.h:58
@ CHARACTER_CMD_INVALID
Definition Network.h:59
@ CHARACTER_CMD_ATTACH
Definition Network.h:61
@ CHARACTER_CMD_DETACH
Definition Network.h:62
@ CHARACTER_CMD_POSITION
Definition Network.h:60
#define RORNET_MAX_MESSAGE_LENGTH
maximum size of a RoR message. 8192 bytes = 8 kibibytes
Definition RoRnet.h:31
CURLcode curl_result
Definition Network.h:49
std::string title
Definition Network.h:48
char anim_name[CHARACTER_ANIM_NAME_LEN]
Definition Network.h:76
RoRnet::Header header
Definition Network.h:95
char buffer[RORNET_MAX_MESSAGE_LENGTH]
Definition Network.h:96
char buffer[RORNET_MAX_MESSAGE_LENGTH]
Definition Network.h:89
Payload of MSG_NET_{ADD/REMOVE}_PEEROPTIONS_REQUESTED.
Definition Network.h:105
int por_uid
RoRnet unique user ID.
Definition Network.h:106
BitMask_t por_peeropts
See RoRnet::PeerOptions.
Definition Network.h:107
< Common header for every packet
Definition RoRnet.h:152
< Sent from the client to server and vice versa, to broadcast a new stream
Definition RoRnet.h:160