OSDN Git Service

0.1.5
authorh2so5 <h2so5@git.sourceforge.jp>
Sat, 15 Sep 2012 13:58:16 +0000 (22:58 +0900)
committerh2so5 <h2so5@git.sourceforge.jp>
Sat, 15 Sep 2012 13:58:16 +0000 (22:58 +0900)
サーバーにステー宅確認コマンドを追加

common/network/Command.cpp
common/network/Command.hpp
common/network/CommandHeader.hpp
server/Server.cpp
server/Server.hpp
server/main.cpp
server/version.hpp

index 752177a..1d7922b 100644 (file)
@@ -23,6 +23,11 @@ SessionWeakPtr Command::session()
     return session_;
 }
 
+boost::asio::ip::udp::endpoint Command::udp_endpoint() const
+{
+       return udp_endpoint_;
+}
+
 FatalConnectionError::FatalConnectionError() :
         Command(header::FatalConnectionError, "")
 {
index 1af76db..37fc8a8 100644 (file)
@@ -7,6 +7,7 @@
 #include <string>
 #include <boost/shared_ptr.hpp>
 #include <boost/weak_ptr.hpp>
+#include <boost/asio.hpp>
 #include <stdint.h>
 #include "CommandHeader.hpp"
 #include "../database/AccountProperty.hpp"
@@ -24,9 +25,14 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
             Command(header::CommandHeader header, const std::string body, const SessionWeakPtr& session) :
                 header_(header), body_(body), session_(session) {}
 
+            Command(header::CommandHeader header, const std::string body,
+                               const boost::asio::ip::udp::endpoint& udp_endpoint) :
+                header_(header), body_(body), udp_endpoint_(udp_endpoint) {}
+
             header::CommandHeader header() const;
             const std::string& body() const;
             SessionWeakPtr session();
+                       boost::asio::ip::udp::endpoint udp_endpoint() const;
 
         private:
             header::CommandHeader header_;
@@ -34,6 +40,7 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
         protected:
             std::string body_;
             SessionWeakPtr session_;
+                       boost::asio::ip::udp::endpoint udp_endpoint_;
     };
 
     // コネクションの切断
index 1cf975a..d7085e6 100644 (file)
@@ -29,6 +29,8 @@ namespace header {
         ServerReceiveJSON =                         0x14,
         ClientReceiveJSON =                         0x15,
 
+               ServerRequstedStatus =                                          0xE0,
+
         LZ4_COMPRESS_HEADER =                       0xF0,
         ENCRYPT_HEADER =                            0xF1
     };
index 422c4a8..4f252b8 100644 (file)
@@ -3,6 +3,7 @@
 //
 
 #include "Server.hpp"
+#include "version.hpp"
 #include <algorithm>
 #include <boost/make_shared.hpp>
 #include <boost/foreach.hpp>
@@ -83,10 +84,28 @@ namespace network {
        Logger::Info(_T("stop server innterrupt_type=%d"),innterrupt_type);
     }
 
+       int Server::GetUserCount() const
+       {
+               auto count = std::count_if(sessions_.begin(), sessions_.end(),
+                       [](const SessionWeakPtr& s){ return !s.expired() && s.lock()->online(); });
+
+               return count;
+       }
+
+       std::string Server::GetStatusJSON() const
+       {
+               auto msg = (
+                                       boost::format("{\"ver\":%d.%d.%d,\"cnt\":%d}")
+                                               % MMO_VERSION_MAJOR % MMO_VERSION_MINOR % MMO_VERSION_REVISION %
+                                               GetUserCount()
+                                       ).str();
+
+               return msg;
+       }
 
     bool Server::Empty() const
     {
-        return sessions_.size() == 0;
+        return GetUserCount() == 0;
     }
 
     void Server::ReceiveSession(const SessionPtr& session, const boost::system::error_code& error)
@@ -119,15 +138,19 @@ namespace network {
          acceptor_.async_accept(new_session->tcp_socket(),
                  boost::bind(&Server::ReceiveSession, this, new_session, boost::asio::placeholders::error));
 
-        // 使用済のセッションのポインタを破棄
+               RefreshSession();
+    }
+
+       void Server::RefreshSession()
+       {
+               // 使用済のセッションのポインタを破棄
         auto it = std::remove_if(sessions_.begin(), sessions_.end(),
                 [](const SessionWeakPtr& ptr){
             return ptr.expired();
         });
         sessions_.erase(it, sessions_.end());
-
-               Logger::Info("Active connection: %d", sessions_.size() - 1);
-    }
+               Logger::Info("Active connection: %d", GetUserCount());
+       }
 
     void Server::SendAll(const Command& command)
     {
@@ -169,11 +192,16 @@ namespace network {
         }
     }
 
+    void Server::SendUDP(const std::string& message, const boost::asio::ip::udp::endpoint endpoint)
+    {
+               io_service_.post(boost::bind(&Server::DoWriteUDP, this, message, endpoint));
+    }
+
     void Server::ReceiveUDP(const boost::system::error_code& error, size_t bytes_recvd)
     {
         if (bytes_recvd > 0) {
             std::string buffer(receive_buf_udp_, bytes_recvd);
-            FetchUDP(buffer);
+            FetchUDP(buffer, sender_endpoint_);
         }
         if (!error) {
           socket_udp_.async_receive_from(
@@ -216,20 +244,26 @@ namespace network {
 //        }
     }
 
-    Command Server::FetchUDP(const std::string& buffer)
+    void Server::FetchUDP(const std::string& buffer, const boost::asio::ip::udp::endpoint endpoint)
     {
-        uint32_t user_id;
+        unsigned char header;
+               uint32_t user_id;
         unsigned char count;
-        header::CommandHeader header;
         std::string body;
         SessionPtr session;
 
-        size_t readed = network::Utils::Deserialize(buffer, &user_id, &count, &header);
+               size_t readed = 0;
+        if (buffer.size() > network::Utils::Deserialize(buffer, &header)) {
+                       readed = network::Utils::Deserialize(buffer, &user_id, &count);
+               }
+
         if (readed < buffer.size()) {
             body = buffer.substr(readed);
         }
 
-        return Command(header, body, session);
+        if (callback_) {
+                       (*callback_)(Command(static_cast<network::header::CommandHeader>(header), body, endpoint));
+        }
     }
 
     void Server::ServerSession::Start()
index d589a4a..7d7db5e 100644 (file)
@@ -33,10 +33,14 @@ class Server {
         void SendOthers(const Command&, SessionWeakPtr);
 
         bool Empty() const;
+               std::string GetStatusJSON() const;
 
         int GetSessionReadAverageLimit();
+               int GetUserCount() const;
+               void RefreshSession();
 
         void SendUDPTestPacket(const std::string& ip_address, uint16_t port);
+               void SendUDP(const std::string& message, const boost::asio::ip::udp::endpoint endpoint);
 
         int max_total_read_average() const;
         int max_session_read_average() const;
@@ -52,7 +56,7 @@ class Server {
         void DoWriteUDP(const std::string& msg, const udp::endpoint& endpoint);
         void WriteUDP(const boost::system::error_code& error, boost::shared_ptr<std::string> holder);
 
-        Command FetchUDP(const std::string& buffer);
+        void FetchUDP(const std::string& buffer, const boost::asio::ip::udp::endpoint endpoint);
 
     private:
        boost::asio::io_service io_service_;
index c915fd0..3258fc1 100644 (file)
@@ -66,6 +66,14 @@ int main(int argc, char* argv[])
 
         switch (c.header()) {
 
+               // ステータス要求
+               case network::header::ServerRequstedStatus:
+               {
+                       // ステータスを送り返す
+                       server.SendUDP(server.GetStatusJSON(), c.udp_endpoint());
+               }
+               break;
+
         // JSONメッセージ受信
         case network::header::ServerReceiveJSON:
         {
index fb60c50..63555de 100644 (file)
@@ -9,7 +9,7 @@
 
 #define MMO_VERSION_MAJOR 0
 #define MMO_VERSION_MINOR 1
-#define MMO_VERSION_REVISION 4
+#define MMO_VERSION_REVISION 5
 
 #ifdef MMO_VERSION_BUILD
 #define MMO_VERSION_BUILD_TEXT " Build " MMO_VERSION_TOSTRING(MMO_VERSION_BUILD)