49 Ogre::ColourValue(0.0, 0.8, 0.0),
50 Ogre::ColourValue(0.0, 0.4, 0.701960784314),
51 Ogre::ColourValue(1.0, 0.501960784314, 0.0),
52 Ogre::ColourValue(1.0, 0.8, 0.0),
55 Ogre::ColourValue(0.8, 1.0, 0.0),
56 Ogre::ColourValue(1.0, 0.0, 0.0),
57 Ogre::ColourValue(0.501960784314, 0.501960784314, 0.501960784314),
58 Ogre::ColourValue(0.0, 0.560784313725, 0.0),
60 Ogre::ColourValue(0.701960784314, 0.352941176471, 0.0),
61 Ogre::ColourValue(0.701960784314, 0.560784313725, 0.0),
63 Ogre::ColourValue(0.560784313725, 0.701960784314, 0.0),
64 Ogre::ColourValue(0.701960784314, 0.0, 0.0),
65 Ogre::ColourValue(0.745098039216, 0.745098039216, 0.745098039216),
66 Ogre::ColourValue(0.501960784314, 1.0, 0.501960784314),
67 Ogre::ColourValue(0.501960784314, 0.788235294118, 1.0),
68 Ogre::ColourValue(1.0, 0.752941176471, 0.501960784314),
69 Ogre::ColourValue(1.0, 0.901960784314, 0.501960784314),
70 Ogre::ColourValue(0.666666666667, 0.501960784314, 1.0),
71 Ogre::ColourValue(0.933333333333, 0.0, 0.8),
72 Ogre::ColourValue(1.0, 0.501960784314, 0.501960784314),
73 Ogre::ColourValue(0.4, 0.4, 0.0),
74 Ogre::ColourValue(1.0, 0.749019607843, 1.0),
75 Ogre::ColourValue(0.0, 1.0, 0.8),
76 Ogre::ColourValue(0.8, 0.4, 0.6),
77 Ogre::ColourValue(0.6, 0.6, 0.0),
84 #define LOG_THREAD(_MSG_) { std::stringstream s; s << _MSG_ << " (Thread ID: " << std::this_thread::get_id() << ")"; LOG(s.str()); }
85 #define LOGSTREAM Ogre::LogManager().getSingleton().stream()
96 int numColours =
sizeof(
MP_COLORS) /
sizeof(Ogre::ColourValue);
97 if (color_num < 0 || color_num >= numColours)
98 return Ogre::ColourValue::White;
106 std::stringstream msg;
107 msg <<
"++ " << name <<
": " << header->
source <<
", " << header->
streamid
114 m_net_quality = quality;
119 return m_net_quality;
129 SWBaseSocket::SWBaseError error;
131 if (m_socket.fsend(buffer, msgsize, &error) < msgsize)
133 LOG(
"NET send error: " + error.get_error());
160 return SendMessageRaw(buffer, msgsize);
169 std::lock_guard<std::mutex> lock(m_recv_packetqueue_mutex);
170 m_recv_packet_buffer.push_back(packet);
175 SWBaseSocket::SWBaseError error;
178 LOG_THREAD(
"[RoR|Networking] ReceiveMessage() waiting...");
183 LOG(
"NET receive error 1: " + error.get_error());
188 LOG_THREAD(
"[RoR|Networking] ReceiveMessage() header received");
191 if (head->size > uint32_t(bufferlen))
199 std::memset(content, 0, bufferlen);
200 if (m_socket.frecv(content, head->size, &error) <
static_cast<int>(head->size))
202 LOG_THREAD(
"NET receive error 2: "+ error.get_error());
208 LOG_THREAD(
"[RoR|Networking] ReceiveMessage() body received");
216 LOG(
"[RoR|Networking] SendThread started");
221 std::unique_lock<std::mutex> queue_lock(m_send_packetqueue_mutex);
222 while (m_send_packet_buffer.empty() && !m_shutdown)
224 m_send_packet_available_cv.wait(queue_lock);
230 packet = m_send_packet_buffer.front();
231 m_send_packet_buffer.pop_front();
235 LOG(
"[RoR|Networking] SendThread stopped");
240 LOG_THREAD(
"[RoR|Networking] RecvThread starting...");
252 LOG_THREAD(
"[RoR|Networking] RecvThread: Error while receiving data: " +
TOSTRING(err));
260 if (header.
source == m_uid)
282 if (header.
size !=
sizeof(
int))
286 int quality = *(
int *)buffer;
287 SetNetQuality(quality);
292 if (header.
source == m_uid)
296 std::stringstream msg;
297 msg <<
_L(
"disconnected: remote side closed the connection");
301 bool was_kick = (std::strstr(buffer,
"disconnected on request") ==
nullptr);
309 std::lock_guard<std::mutex> lock(m_users_mutex);
310 auto user = std::find_if(m_users.begin(), m_users.end(), [header](
const RoRnet::UserInfo& u) { return static_cast<int>(u.uniqueid) == header.source; });
311 if (user != m_users.end())
315 text <<
_L(
"left the game");
320 m_disconnected_users.push_back(*user);
327 if (header.
source == m_uid)
329 std::lock_guard<std::mutex> lock(m_userdata_mutex);
331 m_authlevel = m_userdata.authstatus;
332 m_username = Ogre::UTFString(m_userdata.username);
339 if (!GetUserInfo(header.
source, user_info))
345 text <<
"(" << UserAuthToStringShort(user_info) <<
") ";
347 text <<
_L(
"joined the game");
354 std::lock_guard<std::mutex> lock(m_users_mutex);
355 m_users.push_back(user_info);
362 #ifdef USE_ANGELSCRIPT
364 #endif // USE_ANGELSCRIPT
372 LOG_THREAD(
"[RoR|Networking] RecvThread stopped");
378 RoR::LogFormat(
"[RoR|Networking] Failed to connect to server [%s:%d], message: %s", m_net_host.c_str(), m_net_port, msg.c_str());
383 m_socket.set_timeout(1, 0);
384 m_socket.disconnect();
404 catch (std::exception& e)
408 RoR::LogFormat(
"[RoR|Networking] Failed to launch connection thread, message: %s", e.what());
415 if (m_connect_thread.joinable())
416 m_connect_thread.join();
421 RoR::LogFormat(
"[RoR|Networking] Trying to join server '%s' on port '%d' ...", m_net_host.c_str(), m_net_port);
423 SWBaseSocket::SWBaseError error;
426 m_socket = SWInetSocket();
427 m_socket.set_timeout(10, 0);
428 m_socket.connect(m_net_port, m_net_host, &error);
429 if (error != SWBaseSocket::ok)
431 CouldNotConnect(
_L(
"Could not create connection"),
false);
438 CouldNotConnect(
_L(
"Establishing network session: error sending hello"));
448 CouldNotConnect(
_L(
"Establishing network session: error getting server version"));
455 Ogre::UTFString format_wstr =
_L(
"Establishing network session: wrong server version, you are using version '%s' and the server is using '%s'");
458 snprintf(msg_buf, 500, format_wstr.asUTF8_c_str(),
RORNET_VERSION, server_ver);
459 CouldNotConnect(msg_buf);
464 CouldNotConnect(
_L(
"server uses a different protocol version"));
469 CouldNotConnect(
_L(
"Establishing network session: error getting server hello"));
478 wchar_t tmp[512] = L
"";
479 Ogre::UTFString tmp2 =
_L(
"Establishing network session: wrong server version, you are using version '%s' and the server is using '%s'");
480 swprintf(tmp, 512, tmp2.asWStr_c_str(),
RORNET_VERSION, m_server_settings.protocolversion);
481 CouldNotConnect(MyGUI::UString(tmp).asUTF8_c_str());
488 m_socket.set_timeout(0, 0);
502 strncpy(c.
language, (language + std::string(
"_") + country).c_str(), 5);
506 CouldNotConnect(
_L(
"Establishing network session: error sending user info"));
513 CouldNotConnect(
_L(
"Establishing network session: error getting server authorization"));
519 CouldNotConnect(
_L(
"Establishing network session: sorry, server has too many players"));
525 CouldNotConnect(
_L(
"Establishing network session: sorry, you are banned!"),
false);
530 CouldNotConnect(
_L(
"Establishing network session: sorry, wrong password!"));
535 CouldNotConnect(
_L(
"Establishing network session: sorry, wrong protocol version!"));
541 CouldNotConnect(
_L(
"Establishing network session: sorry, unknown server response"));
554 LOG(
"[RoR|Networking] Connect(): Creating Send/Recv threads");
564 LOG(
"[RoR|Networking] Disconnect() disconnecting...");
565 bool is_clean_disconnect = !m_shutdown;
569 m_send_packet_available_cv.notify_one();
571 m_send_thread.join();
572 LOG(
"[RoR|Networking] Disconnect() sender thread cleaned up");
574 m_socket.set_timeout(1, 0);
576 if (is_clean_disconnect)
581 m_recv_thread.join();
582 LOG(
"[RoR|Networking] Disconnect() receiver thread cleaned up");
584 if (is_clean_disconnect)
586 m_socket.disconnect();
595 m_disconnected_users.clear();
596 m_recv_packet_buffer.clear();
597 m_send_packet_buffer.clear();
603 LOG(
"[RoR|Networking] Disconnect() done");
611 LOGSTREAM <<
"[RoR|Networking] Discarding network packet (StreamID: "
612 <<streamid<<
", Type: "<<type<<
"), length is " << len <<
", max is " << max_len;
619 char *buffer = (
char*)(packet.
buffer);
629 memcpy(bufferContent, content, len);
635 std::lock_guard<std::mutex> lock(m_send_packetqueue_mutex);
643 auto search = std::find_if(m_send_packet_buffer.begin(), m_send_packet_buffer.end(),
644 [&](
const NetSendPacket& p) { return !memcmp(packet.buffer, p.buffer, sizeof(RoRnet::Header)); });
645 if (search != m_send_packet_buffer.end())
649 m_send_packet_available_cv.notify_one();
654 m_send_packet_buffer.push_back(packet);
657 m_send_packet_available_cv.notify_one();
674 std::lock_guard<std::mutex> lock(m_recv_packetqueue_mutex);
675 std::vector<NetRecvPacket> buf_copy = m_recv_packet_buffer;
676 m_recv_packet_buffer.clear();
682 return m_server_settings.terrain;
687 std::lock_guard<std::mutex> lock(m_userdata_mutex);
688 return m_userdata.colournum;
693 std::lock_guard<std::mutex> lock(m_userdata_mutex);
699 std::lock_guard<std::mutex> lock(m_userdata_mutex);
705 std::lock_guard<std::mutex> lock(m_users_mutex);
711 std::lock_guard<std::mutex> lock(m_users_mutex);
714 if ((
int)user.uniqueid == uid)
725 std::lock_guard<std::mutex> lock(m_users_mutex);
728 if ((
int)user.uniqueid == uid)
742 if (GetUserInfo(uid, tmp))
749 tmp = GetLocalUserData();
761 std::lock_guard<std::mutex> lock(m_users_mutex);
764 if (user.username == username)
782 size_t payload_len = 0;
786 payload_len +=
sizeof(user.
uniqueid);
789 std::strncpy(payload + payload_len, msg,
sizeof(payload) - payload_len);
790 payload_len += std::strlen(msg);
803 else {
return _LC(
"NetUserAuth",
"Guest"); }
813 else {
return _LC(
"NetUserAuth",
"Guest"); }
816 #endif // USE_SOCKETW