Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
147private:
148 void PushNetMessage(MsgType type, std::string const & message);
149 void SetNetQuality(int quality);
150 bool SendMessageRaw(char *buffer, int msgsize);
151 bool SendNetMessage(int type, unsigned int streamid, int len, char* content);
152 void QueueStreamData(RoRnet::Header &header, char *buffer, size_t buffer_len);
153 int ReceiveMessage(RoRnet::Header *head, char* content, int bufferlen);
154 void CouldNotConnect(std::string const & msg, bool close_socket = true);
155
156 bool ConnectThread();
157 void SendThread();
158 void RecvThread();
159
160 // Variables
161
162 SWInetSocket m_socket;
163
166 std::vector<RoRnet::UserInfo> m_users;
167 std::vector<BitMask_t> m_users_peeropts;
168 std::vector<RoRnet::UserInfo> m_disconnected_users;
169
170 std::string m_username; // Shadows GVar 'mp_player_name' for multithreaded access.
171 std::string m_net_host; // Shadows GVar 'mp_server_host' for multithreaded access.
172 std::string m_password; // Shadows GVar 'mp_server_password' for multithreaded access.
173 std::string m_token; // Shadows GVar 'mp_player_token' for multithreaded access.
174 int m_net_port; // Shadows GVar 'mp_server_port' for multithreaded access.
175 int m_uid;
177
178 std::thread m_send_thread;
179 std::thread m_recv_thread;
180 std::thread m_connect_thread;
181
182 std::string m_status_message;
183 std::atomic<bool> m_shutdown;
184 std::atomic<int> m_net_quality;
185 int m_stream_id = 10;
186
187 std::mutex m_users_mutex;
191
192 std::condition_variable m_send_packet_available_cv;
193
194 std::vector<NetRecvPacket> m_recv_packet_buffer;
195 std::deque <NetSendPacket> m_send_packet_buffer;
196};
197
199
200} // namespace RoR
201
202#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:739
std::deque< NetSendPacket > m_send_packet_buffer
Definition Network.h:195
Ogre::ColourValue GetPlayerColor(int color_num)
Definition Network.cpp:94
std::mutex m_users_mutex
Definition Network.h:187
int GetUserColor()
Definition Network.cpp:695
bool FindUserInfo(std::string const &username, RoRnet::UserInfo &result)
Definition Network.cpp:789
void AddLocalStream(RoRnet::StreamRegister *reg, int size)
Definition Network.cpp:670
void SendThread()
Definition Network.cpp:214
int ReceiveMessage(RoRnet::Header *head, char *content, int bufferlen)
Definition Network.cpp:173
std::string UserAuthToStringLong(RoRnet::UserInfo const &user)
Definition Network.cpp:871
std::string m_username
Definition Network.h:170
int m_authlevel
Definition Network.h:176
std::string GetUsername()
Definition Network.cpp:701
std::thread m_connect_thread
Definition Network.h:180
std::string m_password
Definition Network.h:172
int GetNetQuality()
Definition Network.cpp:117
void RemovePeerOptions(PeerOptionsRequest *rq)
Definition Network.cpp:821
SWInetSocket m_socket
Definition Network.h:162
void CouldNotConnect(std::string const &msg, bool close_socket=true)
Definition Network.cpp:382
bool GetAnyUserInfo(int uid, RoRnet::UserInfo &result)
Also considers local client.
Definition Network.cpp:767
bool SendMessageRaw(char *buffer, int msgsize)
Definition Network.cpp:127
std::vector< BitMask_t > GetAllUsersPeerOpts()
Definition Network.cpp:719
std::vector< RoRnet::UserInfo > m_disconnected_users
Definition Network.h:168
int m_net_port
Definition Network.h:174
std::atomic< int > m_net_quality
Definition Network.h:184
void BroadcastChatMsg(const char *msg)
Definition Network.cpp:838
std::vector< NetRecvPacket > GetIncomingStreamData()
Definition Network.cpp:682
std::mutex m_send_packetqueue_mutex
Definition Network.h:190
std::string m_net_host
Definition Network.h:171
bool GetDisconnectedUserInfo(int uid, RoRnet::UserInfo &result)
Definition Network.cpp:753
std::string UserAuthToStringShort(RoRnet::UserInfo const &user)
Definition Network.cpp:861
std::atomic< bool > m_shutdown
Definition Network.h:183
std::vector< NetRecvPacket > m_recv_packet_buffer
Definition Network.h:194
bool StartConnecting()
Launches connecting on background.
Definition Network.cpp:394
void SetNetQuality(int quality)
Definition Network.cpp:112
bool SendNetMessage(int type, unsigned int streamid, int len, char *content)
Definition Network.cpp:140
void RecvThread()
Definition Network.cpp:238
int m_stream_id
Counter.
Definition Network.h:185
std::thread m_send_thread
Definition Network.h:178
void AddPacket(int streamid, int type, int len, const char *content)
Definition Network.cpp:616
RoRnet::ServerInfo m_server_settings
Definition Network.h:164
void Disconnect()
Definition Network.cpp:572
std::thread m_recv_thread
Definition Network.h:179
void StopConnecting()
Definition Network.cpp:419
RoRnet::UserInfo GetLocalUserData()
Definition Network.cpp:707
std::mutex m_recv_packetqueue_mutex
Definition Network.h:189
void PushNetMessage(MsgType type, std::string const &message)
Definition Network.cpp:87
std::vector< BitMask_t > m_users_peeropts
See RoRnet::PeerOptions.
Definition Network.h:167
RoRnet::UserInfo m_userdata
Definition Network.h:165
std::mutex m_userdata_mutex
Definition Network.h:188
Ogre::String GetTerrainName()
Definition Network.cpp:690
bool ConnectThread()
Definition Network.cpp:427
void QueueStreamData(RoRnet::Header &header, char *buffer, size_t buffer_len)
Definition Network.cpp:163
void WhisperChatMsg(RoRnet::UserInfo const &user, const char *msg)
Definition Network.cpp:843
bool GetUserInfo(int uid, RoRnet::UserInfo &result)
Definition Network.cpp:725
std::vector< RoRnet::UserInfo > GetUserInfos()
Definition Network.cpp:713
std::vector< RoRnet::UserInfo > m_users
Definition Network.h:166
std::string m_status_message
Definition Network.h:182
void AddPeerOptions(PeerOptionsRequest *rq)
Definition Network.cpp:804
std::condition_variable m_send_packet_available_cv
Definition Network.h:192
std::string m_token
Definition Network.h:173
MsgType
Global gameplay message loop, see struct Message in GameContext.h.
Definition Application.h:76
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