OSDN Git Service

ネットワークコマンド関連を整理
authorh2so5 <h2so5@git.sourceforge.jp>
Tue, 4 Sep 2012 02:49:11 +0000 (11:49 +0900)
committerh2so5 <h2so5@git.sourceforge.jp>
Tue, 4 Sep 2012 02:49:11 +0000 (11:49 +0900)
client/Card.cpp
client/Client.cpp
common/network/Command.cpp
common/network/Command.hpp
common/network/CommandHeader.hpp
common/network/Utils.cpp
common/unicode.cpp
common/unicode.hpp
server/Account.cpp
server/Server.cpp
server/main.cpp

index 85a1550..acaa3b0 100644 (file)
Binary files a/client/Card.cpp and b/client/Card.cpp differ
index 285c1d3..dcd240d 100644 (file)
@@ -331,11 +331,11 @@ void Client::ClientSession::Connect(const boost::system::error_code& error)
                         boost::asio::placeholders::error));
 
         if (on_receive_) {
-            (*on_receive_)(ConnectionSucceeded());
+            // (*on_receive_)(ConnectionSucceeded());
         }
     } else {
         if (on_receive_) {
-            (*on_receive_)(ConnectionFailed());
+            // (*on_receive_)(ConnectionFailed());
         }
     }
 }
index 2f8b8ec..7c16dec 100644 (file)
@@ -23,19 +23,6 @@ SessionWeakPtr Command::session()
     return session_;
 }
 
-// コネクションの確立に成功
-ConnectionSucceeded::ConnectionSucceeded() :
-        Command(header::ConnectionSucceeded, "")
-{
-
-}
-
-ConnectionFailed::ConnectionFailed() :
-        Command(header::ConnectionFailed, "")
-{
-
-}
-
 FatalConnectionError::FatalConnectionError() :
         Command(header::FatalConnectionError, "")
 {
@@ -48,19 +35,6 @@ FatalConnectionError::FatalConnectionError(unsigned int user_id) :
     body_ = Utils::Serialize(user_id);
 }
 
-ClientReceiveChatMessage::ClientReceiveChatMessage(unsigned int user_id,
-        const std::string& time_string,
-        const std::string& msg) :
-        Command(header::ClientReceiveChatMessage, Utils::Serialize<unsigned int, std::string, std::string>(user_id, time_string, msg))
-{
-}
-
-ServerReceiveChatMessage::ServerReceiveChatMessage(const std::string& msg) :
-        Command(header::ServerReceiveChatMessage, msg)
-{
-
-}
-
 ServerStartEncryptedSession::ServerStartEncryptedSession() :
         Command(header::ServerStartEncryptedSession, "")
 {
@@ -86,24 +60,6 @@ ClientReceiveCommonKey::ClientReceiveCommonKey(const std::string& key,
 {
 }
 
-ServerRequestedChatLog::ServerRequestedChatLog() :
-        Command(header::ServerRequestedChatLog, "")
-{
-
-}
-
-ClientJoinPlayer::ClientJoinPlayer() :
-        Command(header::ClientJoinPlayer, "")
-{
-
-}
-
-ClientLeavePlayer::ClientLeavePlayer() :
-        Command(header::ClientLeavePlayer, "")
-{
-
-}
-
 ClientUpdatePlayerPosition::ClientUpdatePlayerPosition
 (unsigned int id, float x, float y, float z, float theta) :
 Command(header::ClientUpdatePlayerPosition, Utils::Serialize(id, x, y, z, theta))
@@ -134,48 +90,12 @@ ClientRequestedClientInfo::ClientRequestedClientInfo() :
 
 }
 
-ClientUpdateChannelUserList::ClientUpdateChannelUserList() :
-        Command(header::ClientUpdateChannelUserList, "")
-{
-
-}
-
-ServerCloseSession::ServerCloseSession() :
-        Command(header::ServerCloseSession, "")
-{
-
-}
-
-ServerReceiveNewCard::ServerReceiveNewCard(const std::string& name,
-                                           const std::string& note,
-                                           const std::string& url) :
-                                           Command(header::ServerReceiveNewCard, Utils::Serialize(name, note, url))
-{
-}
-
-ServerRequestedCardRevisionPatch::ServerRequestedCardRevisionPatch(int revision) :
-                                           Command(header::ServerRequestedCardRevisionPatch, Utils::Serialize(revision))
-{
-}
-
 ServerRequestedAccountRevisionPatch::ServerRequestedAccountRevisionPatch(unsigned int user_id, int revision) :
                                            Command(header::ServerRequestedAccountRevisionPatch,
                                                    Utils::Serialize(user_id, revision))
 {
 }
 
-
-ClientReceiveCardRevisionPatch::ClientReceiveCardRevisionPatch(const std::string& patch) :
-    Command(header::ClientReceiveCardRevisionPatch, patch)
-{
-
-}
-
-ClientReceiveCardRevisionUpdateNotify::ClientReceiveCardRevisionUpdateNotify(int revision) :
-        Command(header::ClientReceiveCardRevisionUpdateNotify, Utils::Serialize(revision))
-{
-}
-
 ClientReceiveAccountRevisionPatch::ClientReceiveAccountRevisionPatch(const std::string& patch) :
     Command(header::ClientReceiveAccountRevisionPatch, patch)
 {
@@ -199,33 +119,19 @@ ClientReceiveServerCrowdedError::ClientReceiveServerCrowdedError() :
 
 }
 
-PlayerLogoutNotify::PlayerLogoutNotify(unsigned int user_id) :
-        Command(header::PlayerLogoutNotify, Utils::Serialize(user_id))
-{
-}
-
-ServerUpdatePlayerName::ServerUpdatePlayerName(const std::string& name) :
-        Command(header::ServerUpdatePlayerName, name)
-{
-}
-
-ServerUpdatePlayerTrip::ServerUpdatePlayerTrip(const std::string& trip) :
-        Command(header::ServerUpdatePlayerTrip, trip)
+ClientReceiveUnsupportVersionError::ClientReceiveUnsupportVersionError(uint32_t require_version) :
+        Command(header::ClientReceiveUnsupportVersionError, Utils::Serialize(require_version))
 {
 }
 
-ServerUpdatePlayerModelName::ServerUpdatePlayerModelName(const std::string& name) :
-        Command(header::ServerUpdatePlayerModelName, name)
+ServerReceiveAccountInitializeData::ServerReceiveAccountInitializeData(const std::string& data):
+        Command(header::ServerReceiveAccountInitializeData, data)
 {
-}
 
-ClientReceiveUnsupportVersionError::ClientReceiveUnsupportVersionError(uint32_t require_version) :
-        Command(header::ClientReceiveUnsupportVersionError, Utils::Serialize(require_version))
-{
 }
 
-ServerReceiveAccountInitializeData::ServerReceiveAccountInitializeData(const std::string& data):
-                Command(header::ServerReceiveAccountInitializeData, data)
+ServerUpdateAccountProperty::ServerUpdateAccountProperty(AccountProperty property, const std::string& value) :
+Command(header::ServerUpdateAccountProperty, Utils::Serialize(property, value))
 {
 
 }
@@ -240,5 +146,4 @@ ClientReceiveJSON::ClientReceiveJSON(const std::string& info_json, const std::st
 {
 }
 
-
 }
index 3d312bf..459072c 100644 (file)
@@ -9,6 +9,7 @@
 #include <boost/weak_ptr.hpp>
 #include <stdint.h>
 #include "CommandHeader.hpp"
+#include "../database/AccountProperty.hpp"
 
 namespace network {
 
@@ -35,19 +36,6 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
             SessionWeakPtr session_;
     };
 
-
-    // コネクションの確立に成功
-    class ConnectionSucceeded : public Command {
-    public:
-        ConnectionSucceeded();
-    };
-
-    // コネクションの確立に失敗
-    class ConnectionFailed : public Command {
-    public:
-        ConnectionFailed();
-    };
-
     // コネクションの切断
     class FatalConnectionError : public Command {
     public:
@@ -55,18 +43,6 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
         FatalConnectionError(unsigned int user_id);
     };
 
-    // チャットメッセージを受信した
-    class ClientReceiveChatMessage : public Command {
-    public:
-        ClientReceiveChatMessage(unsigned int user_id, const std::string& time_string, const std::string& msg);
-    };
-
-    // チャットメッセージを受信した
-    class ServerReceiveChatMessage : public Command {
-    public:
-        ServerReceiveChatMessage(const std::string& msg);
-    };
-
     // 暗号化通信を開始
     class ServerStartEncryptedSession : public Command {
     public:
@@ -93,30 +69,6 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
                 unsigned int user_id);
     };
 
-    // チャットログを受信した
-    class ClientReceiveChatLog : public Command {
-    public:
-        ClientReceiveChatLog();
-    };
-
-    // チャットログを要求された
-    class ServerRequestedChatLog : public Command {
-    public:
-        ServerRequestedChatLog();
-    };
-
-    // ユーザーが入室した
-    class ClientJoinPlayer : public Command {
-    public:
-        ClientJoinPlayer();
-    };
-
-    // ユーザーが退室した
-    class ClientLeavePlayer : public Command {
-    public:
-        ClientLeavePlayer();
-    };
-
     // 他のプレイヤーの位置情報が更新された
     class ClientUpdatePlayerPosition : public Command {
     public:
@@ -147,49 +99,11 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
         ClientRequestedClientInfo();
     };
 
-    // チャンネルのプレイヤーリストが更新された
-    class ClientUpdateChannelUserList : public Command {
-    public:
-        ClientUpdateChannelUserList();
-    };
-
-    // セッションを切断された
-    class ServerCloseSession : public Command {
-    public:
-        ServerCloseSession();
-    };
-
-    // カードの登録要求
-    class ServerReceiveNewCard : public Command {
-    public:
-        ServerReceiveNewCard(const std::string& name,
-                             const std::string& note,
-                             const std::string& url);
-    };
-
-    //
-    class ServerRequestedCardRevisionPatch : public Command {
-    public:
-        ServerRequestedCardRevisionPatch(int revision);
-    };
-
     class ServerRequestedAccountRevisionPatch : public Command {
     public:
         ServerRequestedAccountRevisionPatch(unsigned int user_id, int revision);
     };
 
-    // カードデータベースの更新データ
-    class ClientReceiveCardRevisionPatch : public Command {
-    public:
-        ClientReceiveCardRevisionPatch(const std::string& patch);
-    };
-
-    // カードデータベースの更新通知
-    class ClientReceiveCardRevisionUpdateNotify : public Command {
-    public:
-        ClientReceiveCardRevisionUpdateNotify(int revision);
-    };
-
     // アカウントデータベースの更新データ
     class ClientReceiveAccountRevisionPatch : public Command {
     public:
@@ -212,21 +126,6 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
         ClientReceiveServerCrowdedError();
     };
 
-    class PlayerLogoutNotify : public Command {
-    public:
-        PlayerLogoutNotify(unsigned int user_id);
-    };
-
-    class ServerUpdatePlayerName : public Command {
-    public:
-        ServerUpdatePlayerName(const std::string& name);
-    };
-
-    class ServerUpdatePlayerTrip : public Command {
-    public:
-        ServerUpdatePlayerTrip(const std::string& trip);
-    };
-
     class ClientReceiveUnsupportVersionError : public Command {
     public:
         ClientReceiveUnsupportVersionError(uint32_t require_version);
@@ -237,9 +136,9 @@ typedef boost::weak_ptr<Session> SessionWeakPtr;
         ServerReceiveAccountInitializeData(const std::string& data);
     };
 
-    class ServerUpdatePlayerModelName : public Command {
+    class ServerUpdateAccountProperty : public Command {
     public:
-        ServerUpdatePlayerModelName(const std::string& name);
+        ServerUpdateAccountProperty(AccountProperty propery, const std::string& value);
     };
 
     class ServerReceiveJSON : public Command {
index 905f926..cd4e8fa 100644 (file)
@@ -7,44 +7,25 @@
 namespace network {
 namespace header {
     enum CommandHeader {
-        ConnectionSucceeded =                       0x90000001,
-        ConnectionFailed =                          0xD0000002,
         FatalConnectionError =                      0xD0000003,
-        ClientReceiveChatMessage =                  0x80000004,
-        ServerReceiveChatMessage =                  0x00000005,
         ServerStartEncryptedSession =               0x00000006,
         ClientStartEncryptedSession =               0x80000007,
         ServerReceivePublicKey =                    0x00000008,
         ClientReceiveCommonKey =                    0x80000009,
-        ClientReceiveChatLog =                      0x8000000A,
-        ServerRequestedChatLog =                    0x2000000B,
-        ClientJoinPlayer =                          0x8000000C,
-        ClientLeavePlayer =                         0x8000000D,
         ClientUpdatePlayerPosition =                0x8000000E,
         ServerUpdatePlayerPosition =                0x0000000F,
         ServerReceiveClientInfo =                   0x00000010,
         ClientRequestedPublicKey =                  0xA0000011,
         ClientRequestedClientInfo =                 0xA0000012,
-        ClientUpdateChannelUserList =               0x80000013,
-        ServerCloseSession =                        0x00000014,
-        ServerReceiveNewCard =                      0x00000015,
-        ClientReceiveSystemMessage =                0x80000016,
-        ServerRequestedRemoveCard =                 0x20000017,
-        ServerRequestedCardRevisionPatch =          0x20000018,
-        ClientReceiveCardRevisionPatch =            0x80000019,
-        ClientReceiveCardRevisionUpdateNotify =     0x8000001A,
         ClientReceiveAccountRevisionPatch =         0x8000001B,
         ServerRequestedAccountRevisionPatch =       0x2000001C,
         ClientReceiveAccountRevisionUpdateNotify =  0x8000001D,
         ClientReceiveWriteAverageLimitUpdate =      0x8000001E,
         ClientReceiveServerCrowdedError =           0xC000001F,
-        PlayerLogoutNotify =                        0x10000020,
-        ServerUpdatePlayerName =                    0x00000021,
-        ServerUpdatePlayerTrip =                    0x00000022,
         ClientReceiveUnsupportVersionError =        0xC0000023,
         ServerReceiveAccountInitializeData =        0x00000024,
-        ServerUpdatePlayerModelName =               0x00000025,
         ClientReceiveServerInfo =                   0x80000026,
+        ServerUpdateAccountProperty =               0x00000027,
 
         ServerReceiveJSON =                         0x00000040,
         ClientReceiveJSON =                         0x80000080
index 22e7694..cd2ca4a 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdexcept>
 #include <cctype>
 #include <boost/asio/ip/address.hpp>
+#include <boost/foreach.hpp>
 
 namespace network {
     namespace Utils {
@@ -27,8 +28,7 @@ namespace network {
         {
             std::string out;
 
-                       for (auto it = in.begin(); it != in.end(); ++it) {
-                               const char& c = *it;
+                       BOOST_FOREACH(const char& c, in) {
                                if (c == 0x7e || c == 0x7d) {
                     out += 0x7d;
                     out += c ^ 0x20;
@@ -46,8 +46,7 @@ namespace network {
 
             bool escape = false;
 
-                       for (auto it = in.begin(); it != in.end(); ++it) {
-                               const char& c = *it;
+                       BOOST_FOREACH(const char& c, in) {
                                if (escape) {
                     out += c ^ 0x20;
                     escape = false;
@@ -63,8 +62,7 @@ namespace network {
         {
             std::string out;
 
-                       for (auto it = in.begin(); it != in.end(); ++it) {
-                               const char& c = *it;
+                       BOOST_FOREACH(const char& c, in) {
                                out += (boost::format("%X ") % (int)c).str();
                        }
 
@@ -148,8 +146,7 @@ namespace network {
            int bits_collected = 0;
            unsigned int accumulator = 0;
 
-           for (string::const_iterator i = ascdata.begin(); i != last; ++i) {
-              const int c = *i;
+           BOOST_FOREACH(const int c, ascdata) {
               if (::std::isspace(c) || c == '=') {
                  // Skip whitespace and padding. Be liberal in what you accept.
                  continue;
index ebd5a5f..83c5da3 100644 (file)
@@ -8,7 +8,7 @@ namespace unicode {
 
 #ifdef _WIN32
 
-    std::string sjis2utf8(std::string in) {
+    std::string sjis2utf8(const std::string& in) {
 
         int wchar_size = ::MultiByteToWideChar(CP_ACP,0,in.data(),in.size(),nullptr,0);
                auto wchar_buf = new wchar_t [wchar_size];
@@ -25,7 +25,7 @@ namespace unicode {
         return out;
     }
 
-    std::string utf82sjis(std::string in) {
+    std::string utf82sjis(const std::string& in) {
 
         int wchar_size = ::MultiByteToWideChar(CP_UTF8,0,in.data(),in.size(),nullptr,0);
                auto wchar_buf = new wchar_t[wchar_size];
index a42510e..3160585 100644 (file)
@@ -28,8 +28,8 @@ namespace unicode {
 
 #ifdef _WIN32
 
-std::string sjis2utf8(std::string);
-std::string utf82sjis(std::string);
+std::string sjis2utf8(const std::string&);
+std::string utf82sjis(const std::string&);
 
 std::string ToString(const std::wstring& s);
 std::wstring ToWString(const std::string& s);
index c226fa8..ab36e31 100644 (file)
@@ -180,7 +180,9 @@ std::string Account::GetUserName(UserID user_id) const
 
 void Account::SetUserName(UserID user_id, const std::string& name)
 {
-    Set(user_id, NAME, name);
+    if (name.size() > 0 && name.size() <= 32) {
+        Set(user_id, NAME, name);
+    }
 }
 
 std::string Account::GetUserTrip(UserID user_id) const
@@ -192,8 +194,10 @@ std::string Account::GetUserTrip(UserID user_id) const
 
 void Account::SetUserTrip(UserID user_id, const std::string& trip)
 {
-    std::string crypted_trip = network::Encrypter::GetTrip(trip);
-    Set(user_id, TRIP, crypted_trip);
+    if (trip.size() > 0 && trip.size() <= 64) {
+        std::string crypted_trip = network::Encrypter::GetTrip(trip);
+        Set(user_id, TRIP, crypted_trip);
+    }
 }
 
 std::string Account::GetUserModelName(UserID user_id) const
@@ -205,7 +209,9 @@ std::string Account::GetUserModelName(UserID user_id) const
 
 void Account::SetUserModelName(UserID user_id, const std::string& name)
 {
-    Set(user_id, MODEL_NAME, name);
+    if (name.size() > 0 && name.size() <= 64) {
+        Set(user_id, MODEL_NAME, name);
+    }
 }
 
 std::string Account::GetUserIPAddress(UserID user_id) const
index f4047ad..b633f22 100644 (file)
@@ -43,12 +43,6 @@ namespace network {
             // 通信量制限を越えていた場合、強制的に切断
             else if (auto session = c.session().lock()) {
                 if (session->GetReadByteAverage() > session_read_average_) {
-                    if (auto session = c.session().lock()) {
-                        if (callback) {
-                            (*callback)(network::PlayerLogoutNotify(session->id()));
-                        }
-                        return;
-                    }
                     session->Close();
                 }
             }
@@ -127,8 +121,7 @@ namespace network {
 
     void Server::SendAll(const Command& command)
     {
-        for (auto it = sessions_.begin(); it != sessions_.end(); ++it) {
-            SessionWeakPtr& ptr = *it;
+        BOOST_FOREACH(SessionWeakPtr& ptr, sessions_) {
             if (auto session = ptr.lock()) {
                 session->Send(command);
             }
@@ -137,8 +130,7 @@ namespace network {
 
     void Server::SendOthers(const Command& command, SessionWeakPtr self_ptr)
     {
-        for (auto it = sessions_.begin(); it != sessions_.end(); ++it) {
-            SessionWeakPtr& ptr = *it;
+        BOOST_FOREACH(SessionWeakPtr& ptr, sessions_) {
             if (auto session = ptr.lock()) {
                 if (auto self = self_ptr.lock()) {
                     if (*session != *self) {
index a2774cd..20a19e8 100644 (file)
@@ -12,6 +12,7 @@
 #include "Server.hpp"
 #include "../common/network/Encrypter.hpp"
 #include "../common/network/Signature.hpp"
+#include "../common/database/AccountProperty.hpp"
 #include "../common/Logger.hpp"
 #include "Config.hpp"
 #include "Account.hpp"
@@ -214,79 +215,66 @@ int main(int argc, char* argv[])
         }
         break;
 
-        // ユーザー名の更新
-        case network::header::ServerUpdatePlayerName:
+        case network::header::ServerUpdateAccountProperty:
         {
             if (auto session = c.session().lock()) {
-                std::string name = c.body();
-                if (name.size() > 0 && name.size() <= 64) {
-                    account.SetUserName(session->id(), name);
-                    server.SendAll(
-                            network::ClientReceiveAccountRevisionUpdateNotify(session->id(),
-                                    account.GetUserRevision(session->id())));
+                AccountProperty property;
+                std::string value;
+                network::Utils::Deserialize(c.body(), &property, &value);
+
+                auto old_revision = account.GetUserRevision(session->id());
+
+                switch (property) {
+
+                case NAME:
+                    {
+                        account.SetUserName(session->id(), value);
+                    }
+                    break;
+                case TRIP:
+                    {
+                        account.SetUserTrip(session->id(), value);
+                    }
+                    break;
+                case MODEL_NAME:
+                    {
+                        account.SetUserModelName(session->id(), value);
+                    }
+                    break;
+                default:
+                    ;
                 }
-                Logger::Info(msg);
-            }
-        }
-        break;
 
-        // トリップの更新
-        case network::header::ServerUpdatePlayerTrip:
-        {
-            if (auto session = c.session().lock()) {
-                std::string trip = c.body();
-                if (trip.size() > 0 && trip.size() <= 64) {
-                    account.SetUserTrip(session->id(), trip);
+                auto new_revison = account.GetUserRevision(session->id());
+                if (new_revison > old_revision) {
                     server.SendAll(
-                            network::ClientReceiveAccountRevisionUpdateNotify(session->id(),
-                                    account.GetUserRevision(session->id())));
+                            network::ClientReceiveAccountRevisionUpdateNotify(
+                            session->id(),new_revison));
                 }
+
                 Logger::Info(msg);
             }
         }
         break;
 
-        // ã\83¢ã\83\87ã\83«å\90\8dã\81®æ\9b´æ\96°
-        case network::header::ServerUpdatePlayerModelName:
+        // ã\82¨ã\83©ã\83¼
+        case network::header::FatalConnectionError:
         {
-            if (auto session = c.session().lock()) {
-                std::string name = c.body();
-                if (name.size() > 0 && name.size() <= 64) {
-                    account.SetUserModelName(session->id(), name);
-                    server.SendAll(
-                            network::ClientReceiveAccountRevisionUpdateNotify(session->id(),
-                                    account.GetUserRevision(session->id())));
-                }
-                Logger::Info(msg);
+            if (c.body().size() > 0) {
+                int user_id;
+                network::Utils::Deserialize(c.body(), &user_id);
+                account.LogOut(user_id);
+
+                server.SendAll(
+                        network::ClientReceiveAccountRevisionUpdateNotify(user_id,
+                                account.GetUserRevision(user_id)));
+
+                Logger::Info("Logout User: %d", user_id);
             }
         }
+        Logger::Info(msg);
         break;
 
-       // ログアウト
-       case network::header::PlayerLogoutNotify:
-       {
-           Logger::Info(msg);
-       }
-       break;
-
-       // エラー
-       case network::header::FatalConnectionError:
-       {
-           if (c.body().size() > 0) {
-               int user_id;
-               network::Utils::Deserialize(c.body(), &user_id);
-               account.LogOut(user_id);
-
-               server.SendAll(
-                       network::ClientReceiveAccountRevisionUpdateNotify(user_id,
-                               account.GetUserRevision(user_id)));
-
-               Logger::Info("Logout User: %d", user_id);
-           }
-       }
-       Logger::Info(msg);
-       break;
-
         default:
             break;
         }