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");
321 int peeropt_offset = (int)std::distance(m_users.begin(), user);
322 auto peeropt_itor = m_users_peeropts.begin() + peeropt_offset;
323 m_users_peeropts.erase(peeropt_itor);
325 m_disconnected_users.push_back(*user);
332 if (header.
source == m_uid)
334 std::lock_guard<std::mutex> lock(m_userdata_mutex);
336 m_authlevel = m_userdata.authstatus;
337 m_username = std::string(m_userdata.username);
344 if (!GetUserInfo(header.
source, user_info))
350 text <<
"(" << UserAuthToStringShort(user_info) <<
") ";
352 text <<
_L(
"joined the game");
359 std::lock_guard<std::mutex> lock(m_users_mutex);
360 m_users.push_back(user_info);
361 m_users_peeropts.push_back(
BitMask_t(0));
368 #ifdef USE_ANGELSCRIPT
370 #endif // USE_ANGELSCRIPT
378 LOG_THREAD(
"[RoR|Networking] RecvThread stopped");
384 RoR::LogFormat(
"[RoR|Networking] Failed to connect to server [%s:%d], message: %s", m_net_host.c_str(), m_net_port, msg.c_str());
389 m_socket.set_timeout(1, 0);
390 m_socket.disconnect();
410 catch (std::exception& e)
414 RoR::LogFormat(
"[RoR|Networking] Failed to launch connection thread, message: %s", e.what());
423 if (m_connect_thread.joinable())
424 m_connect_thread.join();
429 RoR::LogFormat(
"[RoR|Networking] Trying to join server '%s' on port '%d' ...", m_net_host.c_str(), m_net_port);
431 SWBaseSocket::SWBaseError error;
434 m_socket = SWInetSocket();
435 m_socket.set_timeout(10, 0);
436 m_socket.connect(m_net_port, m_net_host, &error);
437 if (error != SWBaseSocket::ok)
439 CouldNotConnect(
_L(
"Could not create connection"),
false);
446 CouldNotConnect(
_L(
"Establishing network session: error sending hello"));
456 CouldNotConnect(
_L(
"Establishing network session: error getting server version"));
463 std::string format_str =
_L(
"Establishing network session: wrong server version, you are using version '%s' and the server is using '%s'");
470 CouldNotConnect(
_L(
"server uses a different protocol version"));
475 CouldNotConnect(
_L(
"Establishing network session: error getting server hello"));
484 std::string formatstr =
_L(
"Establishing network session: wrong server version, you are using version '%s' and the server is using '%s'");
492 m_socket.set_timeout(0, 0);
504 strncpy(c.
language, (language + std::string(
"_") + country).c_str(), 5);
508 CouldNotConnect(
_L(
"Establishing network session: error sending user info"));
515 CouldNotConnect(
_L(
"Establishing network session: error getting server authorization"));
521 CouldNotConnect(
_L(
"Establishing network session: sorry, server has too many players"));
527 CouldNotConnect(
_L(
"Establishing network session: sorry, you are banned!"),
false);
532 CouldNotConnect(
_L(
"Establishing network session: sorry, wrong password!"));
537 CouldNotConnect(
_L(
"Establishing network session: sorry, wrong protocol version!"));
543 CouldNotConnect(
_L(
"Establishing network session: sorry, unknown server response"));
556 LOG(
"[RoR|Networking] Connect(): Creating Send/Recv threads");
566 LOG(
"[RoR|Networking] Disconnect() disconnecting...");
567 bool is_clean_disconnect = !m_shutdown;
571 m_send_packet_available_cv.notify_one();
573 m_send_thread.join();
574 LOG(
"[RoR|Networking] Disconnect() sender thread cleaned up");
576 m_socket.set_timeout(1, 0);
578 if (is_clean_disconnect)
583 m_recv_thread.join();
584 LOG(
"[RoR|Networking] Disconnect() receiver thread cleaned up");
586 if (is_clean_disconnect)
588 m_socket.disconnect();
597 m_disconnected_users.clear();
598 m_recv_packet_buffer.clear();
599 m_send_packet_buffer.clear();
605 LOG(
"[RoR|Networking] Disconnect() done");
613 LOGSTREAM <<
"[RoR|Networking] Discarding network packet (StreamID: "
614 <<streamid<<
", Type: "<<type<<
"), length is " << len <<
", max is " << max_len;
621 char *buffer = (
char*)(packet.
buffer);
631 memcpy(bufferContent, content, len);
637 std::lock_guard<std::mutex> lock(m_send_packetqueue_mutex);
645 auto search = std::find_if(m_send_packet_buffer.begin(), m_send_packet_buffer.end(),
646 [&](
const NetSendPacket& p) { return !memcmp(packet.buffer, p.buffer, sizeof(RoRnet::Header)); });
647 if (search != m_send_packet_buffer.end())
651 m_send_packet_available_cv.notify_one();
656 m_send_packet_buffer.push_back(packet);
659 m_send_packet_available_cv.notify_one();
676 std::lock_guard<std::mutex> lock(m_recv_packetqueue_mutex);
677 std::vector<NetRecvPacket> buf_copy = m_recv_packet_buffer;
678 m_recv_packet_buffer.clear();
684 return m_server_settings.terrain;
689 std::lock_guard<std::mutex> lock(m_userdata_mutex);
690 return m_userdata.colournum;
695 std::lock_guard<std::mutex> lock(m_userdata_mutex);
701 std::lock_guard<std::mutex> lock(m_userdata_mutex);
707 std::lock_guard<std::mutex> lock(m_users_mutex);
713 std::lock_guard<std::mutex> lock(m_users_mutex);
714 return m_users_peeropts;
719 std::lock_guard<std::mutex> lock(m_users_mutex);
722 if ((
int)user.uniqueid == uid)
733 std::lock_guard<std::mutex> lock(m_users_mutex);
734 for (
size_t i = 0; i < m_users.size(); i++)
736 if (
static_cast<int>(m_users[i].uniqueid) == uid)
738 result = m_users_peeropts[i];
747 std::lock_guard<std::mutex> lock(m_users_mutex);
750 if ((
int)user.uniqueid == uid)
764 if (GetUserInfo(uid, tmp))
771 tmp = GetLocalUserData();
783 std::lock_guard<std::mutex> lock(m_users_mutex);
786 if (user.username == username)
798 std::lock_guard<std::mutex> lock(m_users_mutex);
800 const bool peeropts_sane = m_users.size() == m_users_peeropts.size();
802 if (!peeropts_sane)
return;
804 for (
size_t i = 0; i < m_users.size(); i++)
806 if (
static_cast<int>(m_users[i].uniqueid) == rq->
por_uid)
815 std::lock_guard<std::mutex> lock(m_users_mutex);
817 const bool peeropts_sane = m_users.size() == m_users_peeropts.size();
819 if (!peeropts_sane)
return;
821 for (
size_t i = 0; i < m_users.size(); i++)
823 if (
static_cast<int>(m_users[i].uniqueid) == rq->
por_uid)
839 size_t payload_len = 0;
843 payload_len +=
sizeof(user.
uniqueid);
846 std::strncpy(payload + payload_len, msg,
sizeof(payload) - payload_len);
847 payload_len += std::strlen(msg);
860 else {
return _LC(
"NetUserAuth",
"Guest"); }
870 else {
return _LC(
"NetUserAuth",
"Guest"); }
873 #endif // USE_SOCKETW