RigsofRods
Soft-body Physics Simulation
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 #include <OgreUTFString.h>
41 
42 namespace RoR {
43 
46 
48 {
49  std::string title;
50  CURLcode curl_result = CURLE_OK;
51  long http_response = 0;
52 };
53 
54 // ----------------------- Network messages (packed) -------------------------
55 
56 #pragma pack(push, 1)
57 
59 {
64 };
65 
67 {
68  int32_t command;
69 };
70 
72 {
73  int32_t command;
74  float pos_x, pos_y, pos_z;
75  float rot_angle;
76  float anim_time;
78 };
79 
81 {
82  int32_t command;
83  int32_t source_id;
84  int32_t stream_id;
85  int32_t position;
86 };
87 
89 {
91  int size;
92 };
93 
95 {
98 };
99 
100 #pragma pack(pop)
101 
102 // ------------------------ End of network messages --------------------------
103 
104 class Network
105 {
106 public:
107  bool StartConnecting();
108  void StopConnecting();
109  void Disconnect();
110 
111  void AddPacket(int streamid, int type, int len, const char *content);
112  void AddLocalStream(RoRnet::StreamRegister *reg, int size);
113 
114  std::vector<NetRecvPacket> GetIncomingStreamData();
115 
116  int GetUID();
117  int GetNetQuality();
118 
119  Ogre::String GetTerrainName();
120 
121  int GetUserColor();
122  Ogre::UTFString GetUsername();
124  std::vector<RoRnet::UserInfo> GetUserInfos();
125  bool GetUserInfo(int uid, RoRnet::UserInfo &result);
126  bool GetDisconnectedUserInfo(int uid, RoRnet::UserInfo &result);
127  bool GetAnyUserInfo(int uid, RoRnet::UserInfo &result);
128  bool FindUserInfo(std::string const& username, RoRnet::UserInfo &result);
129  Ogre::ColourValue GetPlayerColor(int color_num);
130 
131  void BroadcastChatMsg(const char* msg);
132  void WhisperChatMsg(RoRnet::UserInfo const& user, const char* msg);
133 
134  std::string UserAuthToStringShort(RoRnet::UserInfo const &user);
135  std::string UserAuthToStringLong(RoRnet::UserInfo const &user);
136 
137 private:
138  void PushNetMessage(MsgType type, std::string const & message);
139  void SetNetQuality(int quality);
140  bool SendMessageRaw(char *buffer, int msgsize);
141  bool SendNetMessage(int type, unsigned int streamid, int len, char* content);
142  void QueueStreamData(RoRnet::Header &header, char *buffer, size_t buffer_len);
143  int ReceiveMessage(RoRnet::Header *head, char* content, int bufferlen);
144  void CouldNotConnect(std::string const & msg, bool close_socket = true);
145 
146  bool ConnectThread();
147  void SendThread();
148  void RecvThread();
149 
150  // Variables
151 
152  SWInetSocket m_socket;
153 
156  std::vector<RoRnet::UserInfo> m_users;
157  std::vector<RoRnet::UserInfo> m_disconnected_users;
158 
159  Ogre::UTFString m_username; // Shadows GVar 'mp_player_name' for multithreaded access.
160  std::string m_net_host; // Shadows GVar 'mp_server_host' for multithreaded access.
161  std::string m_password; // Shadows GVar 'mp_server_password' for multithreaded access.
162  std::string m_token; // Shadows GVar 'mp_player_token' for multithreaded access.
163  int m_net_port; // Shadows GVar 'mp_server_port' for multithreaded access.
164  int m_uid;
166 
167  std::thread m_send_thread;
168  std::thread m_recv_thread;
169  std::thread m_connect_thread;
170 
171  std::string m_status_message;
172  std::atomic<bool> m_shutdown;
173  std::atomic<int> m_net_quality;
174  int m_stream_id = 10;
175 
176  std::mutex m_users_mutex;
177  std::mutex m_userdata_mutex;
180 
181  std::condition_variable m_send_packet_available_cv;
182 
183  std::vector<NetRecvPacket> m_recv_packet_buffer;
184  std::deque <NetSendPacket> m_send_packet_buffer;
185 };
186 
188 
189 } // namespace RoR
190 
191 #endif // USE_SOCKETW
RoR::NetCharacterMsgAttach
Definition: Network.h:80
RoR::Network::Disconnect
void Disconnect()
Definition: Network.cpp:562
RoR::Network::GetUserInfos
std::vector< RoRnet::UserInfo > GetUserInfos()
Definition: Network.cpp:703
RoR::Network::PushNetMessage
void PushNetMessage(MsgType type, std::string const &message)
Definition: Network.cpp:87
RoR::Network::m_server_settings
RoRnet::ServerInfo m_server_settings
Definition: Network.h:154
RoR::CHARACTER_CMD_DETACH
@ CHARACTER_CMD_DETACH
Definition: Network.h:63
RoR::Network::m_recv_packetqueue_mutex
std::mutex m_recv_packetqueue_mutex
Definition: Network.h:178
RoR::Network::StartConnecting
bool StartConnecting()
Launches connecting on background.
Definition: Network.cpp:388
RoR::Network::ConnectThread
bool ConnectThread()
Definition: Network.cpp:419
RoR::Network::UserAuthToStringShort
std::string UserAuthToStringShort(RoRnet::UserInfo const &user)
Definition: Network.cpp:796
RoR::CurlFailInfo::http_response
long http_response
Definition: Network.h:51
RoR::Network::GetUserColor
int GetUserColor()
Definition: Network.cpp:685
RoR::NetCharacterMsgAttach::source_id
int32_t source_id
Definition: Network.h:83
RoRnet::UserInfo
Definition: RoRnet.h:170
RoR::Network::GetUID
int GetUID()
Definition: Network.cpp:122
RoR::Network::m_status_message
std::string m_status_message
Definition: Network.h:171
RoR::Network::QueueStreamData
void QueueStreamData(RoRnet::Header &header, char *buffer, size_t buffer_len)
Definition: Network.cpp:163
RoRnet.h
RoR::Network::GetIncomingStreamData
std::vector< NetRecvPacket > GetIncomingStreamData()
Definition: Network.cpp:672
RoR::NetCharacterMsgPos::anim_name
char anim_name[CHARACTER_ANIM_NAME_LEN]
Definition: Network.h:77
RoR::Network::m_userdata_mutex
std::mutex m_userdata_mutex
Definition: Network.h:177
RoRnet::ServerInfo
Definition: RoRnet.h:202
RoR::Network::m_password
std::string m_password
Definition: Network.h:161
RoR::Network::GetAnyUserInfo
bool GetAnyUserInfo(int uid, RoRnet::UserInfo &result)
Also considers local client.
Definition: Network.cpp:737
RoR::CurlFailInfo
Definition: Network.h:47
RoR::NetRecvPacket::header
RoRnet::Header header
Definition: Network.h:96
RoR::NetCharacterMsgPos::rot_angle
float rot_angle
Definition: Network.h:75
RoR::Network::GetUserInfo
bool GetUserInfo(int uid, RoRnet::UserInfo &result)
Definition: Network.cpp:709
RoR::CurlFailInfo::title
std::string title
Definition: Network.h:49
RoR::NetSendPacket::size
int size
Definition: Network.h:91
RoR::NetSendPacket::buffer
char buffer[RORNET_MAX_MESSAGE_LENGTH]
Definition: Network.h:90
RoR::Network::GetUsername
Ogre::UTFString GetUsername()
Definition: Network.cpp:691
RoR::Network::GetTerrainName
Ogre::String GetTerrainName()
Definition: Network.cpp:680
RoR::NetCharacterMsgGeneric::command
int32_t command
Definition: Network.h:68
RoR::NetCharacterMsgGeneric
Definition: Network.h:66
RoR::Network::m_net_quality
std::atomic< int > m_net_quality
Definition: Network.h:173
RoR::CHARACTER_CMD_ATTACH
@ CHARACTER_CMD_ATTACH
Definition: Network.h:62
RoR::Network::m_username
Ogre::UTFString m_username
Definition: Network.h:159
RoR::NetCharacterMsgPos::anim_time
float anim_time
Definition: Network.h:76
RoR::NetCharacterMsgPos
Definition: Network.h:71
RoR::Network::BroadcastChatMsg
void BroadcastChatMsg(const char *msg)
Definition: Network.cpp:773
RoR::Network::GetDisconnectedUserInfo
bool GetDisconnectedUserInfo(int uid, RoRnet::UserInfo &result)
Definition: Network.cpp:723
RoR::Network::GetNetQuality
int GetNetQuality()
Definition: Network.cpp:117
RORNET_MAX_MESSAGE_LENGTH
#define RORNET_MAX_MESSAGE_LENGTH
maximum size of a RoR message. 8192 bytes = 8 kibibytes
Definition: RoRnet.h:31
RoR::Network::SendNetMessage
bool SendNetMessage(int type, unsigned int streamid, int len, char *content)
Definition: Network.cpp:140
RoR::Network::m_shutdown
std::atomic< bool > m_shutdown
Definition: Network.h:172
RoR::NetCharacterMsgAttach::command
int32_t command
Definition: Network.h:82
RoR::NetCharacterMsgAttach::position
int32_t position
Definition: Network.h:85
RoR::Network::CouldNotConnect
void CouldNotConnect(std::string const &msg, bool close_socket=true)
Definition: Network.cpp:376
RoR::MsgType
MsgType
Global gameplay message loop, see struct Message in GameContext.h.
Definition: Application.h:74
Application.h
Central state/object manager and communications hub.
RoR::Network::m_token
std::string m_token
Definition: Network.h:162
RoR::Network::m_send_packetqueue_mutex
std::mutex m_send_packetqueue_mutex
Definition: Network.h:179
RoRnet::Header
< Common header for every packet
Definition: RoRnet.h:132
RoR::Network::GetLocalUserData
RoRnet::UserInfo GetLocalUserData()
Definition: Network.cpp:697
RoR::CHARACTER_CMD_POSITION
@ CHARACTER_CMD_POSITION
Definition: Network.h:61
RoR::NetCharacterMsgPos::pos_x
float pos_x
Definition: Network.h:74
RoR::Network::ReceiveMessage
int ReceiveMessage(RoRnet::Header *head, char *content, int bufferlen)
Definition: Network.cpp:173
RoR::Network::m_uid
int m_uid
Definition: Network.h:164
RoR::Network::m_recv_thread
std::thread m_recv_thread
Definition: Network.h:168
RoR::Network::m_userdata
RoRnet::UserInfo m_userdata
Definition: Network.h:155
RoR::Network::SendMessageRaw
bool SendMessageRaw(char *buffer, int msgsize)
Definition: Network.cpp:127
RoR::Network::m_stream_id
int m_stream_id
Counter.
Definition: Network.h:174
RoR::Network::GetPlayerColor
Ogre::ColourValue GetPlayerColor(int color_num)
Definition: Network.cpp:94
RoR::NetSendPacket
Definition: Network.h:88
RoR::Network::m_users
std::vector< RoRnet::UserInfo > m_users
Definition: Network.h:156
RoR::Network
Definition: Network.h:104
RoR::Network::FindUserInfo
bool FindUserInfo(std::string const &username, RoRnet::UserInfo &result)
Definition: Network.cpp:759
RoR::Network::RecvThread
void RecvThread()
Definition: Network.cpp:238
RoR::NetCharacterMsgPos::command
int32_t command
Definition: Network.h:73
RoR::Network::SetNetQuality
void SetNetQuality(int quality)
Definition: Network.cpp:112
RoR::Network::m_send_thread
std::thread m_send_thread
Definition: Network.h:167
RoR::Network::m_disconnected_users
std::vector< RoRnet::UserInfo > m_disconnected_users
Definition: Network.h:157
RoR::NetCharacterMsgPos::pos_y
float pos_y
Definition: Network.h:74
RoR::Network::m_net_host
std::string m_net_host
Definition: Network.h:160
RoR::Network::m_recv_packet_buffer
std::vector< NetRecvPacket > m_recv_packet_buffer
Definition: Network.h:183
RoR::Network::m_socket
SWInetSocket m_socket
Definition: Network.h:152
RoR::NetRecvPacket::buffer
char buffer[RORNET_MAX_MESSAGE_LENGTH]
Definition: Network.h:97
RoR::Network::UserAuthToStringLong
std::string UserAuthToStringLong(RoRnet::UserInfo const &user)
Definition: Network.cpp:806
RoR::NetCharacterCmd
NetCharacterCmd
Definition: Network.h:58
RoR::CurlFailInfo::curl_result
CURLcode curl_result
Definition: Network.h:50
RoR::NetCharacterMsgAttach::stream_id
int32_t stream_id
Definition: Network.h:84
RoR::CHARACTER_CMD_INVALID
@ CHARACTER_CMD_INVALID
Definition: Network.h:60
RoR::Network::m_send_packet_available_cv
std::condition_variable m_send_packet_available_cv
Definition: Network.h:181
RoR::Network::AddLocalStream
void AddLocalStream(RoRnet::StreamRegister *reg, int size)
Definition: Network.cpp:660
RoR::Network::m_users_mutex
std::mutex m_users_mutex
Definition: Network.h:176
RoR::NetRecvPacket
Definition: Network.h:94
RoR::Network::m_net_port
int m_net_port
Definition: Network.h:163
CHARACTER_ANIM_NAME_LEN
#define CHARACTER_ANIM_NAME_LEN
Definition: Application.h:42
RoR::Network::AddPacket
void AddPacket(int streamid, int type, int len, const char *content)
Definition: Network.cpp:606
RoR
Definition: AppContext.h:36
RoR::Network::m_send_packet_buffer
std::deque< NetSendPacket > m_send_packet_buffer
Definition: Network.h:184
RoR::Network::m_authlevel
int m_authlevel
Definition: Network.h:165
RoR::NetCharacterMsgPos::pos_z
float pos_z
Definition: Network.h:74
RoR::Network::SendThread
void SendThread()
Definition: Network.cpp:214
RoR::Network::m_connect_thread
std::thread m_connect_thread
Definition: Network.h:169
RoR::Network::WhisperChatMsg
void WhisperChatMsg(RoRnet::UserInfo const &user, const char *msg)
Definition: Network.cpp:778
RoRnet::StreamRegister
< Sent from the client to server and vice versa, to broadcast a new stream
Definition: RoRnet.h:140
RoR::Network::StopConnecting
void StopConnecting()
Definition: Network.cpp:413