diff --git a/lib/libmodels.so b/lib/libmodels.so index 911ebb3..239bdff 100755 Binary files a/lib/libmodels.so and b/lib/libmodels.so differ diff --git a/modules/TcpProxySession/include/TcpProxySession.hpp b/modules/TcpProxySession/include/TcpProxySession.hpp index b2cb536..b3548f8 100644 --- a/modules/TcpProxySession/include/TcpProxySession.hpp +++ b/modules/TcpProxySession/include/TcpProxySession.hpp @@ -4,12 +4,11 @@ * @Autor: lishengyin * @Date: 2022-04-11 17:30:58 * @LastEditors: Please set LastEditors - * @LastEditTime: 2022-04-11 23:14:30 + * @LastEditTime: 2022-04-12 00:07:32 */ #ifndef __TCPPROXYSESSION_HPP_ #define __TCPPROXYSESSION_HPP_ - #include #include #include "Util/logger.h" @@ -25,15 +24,6 @@ using namespace toolkit; class TcpProxySession; -class ProxySessions -{ -public: - std::string NameId; - std::shared_ptr session = nullptr; -public: - ProxySessions(){} - ~ProxySessions(){} -}; class TcpProxySession: public TcpSession { @@ -45,21 +35,16 @@ public: ~TcpProxySession() { DebugL; } - virtual void onRecv(const Buffer::Ptr &buf) override{ - //处理客户端发送过来的数据 - TraceL << buf->data() << " from port:" << getIdentifier(); - } - virtual void onError(const SockException &err) override{ - //客户端断开连接或其他原因导致该对象脱离TCPServer管理 - WarnL << err.what(); - } - virtual void onManager() override{ - //定时管理该对象,譬如会话超时检查 - // DebugL; - } + virtual void onRecv(const Buffer::Ptr &buf) override; + + virtual void onError(const SockException &err) override; + + virtual void onManager() override; private: Ticker _ticker; + bool alive = false; + std::string usr = ""; }; diff --git a/modules/TcpProxySession/src/TcpProxySession.cpp b/modules/TcpProxySession/src/TcpProxySession.cpp index 5a500da..dbc17ff 100644 --- a/modules/TcpProxySession/src/TcpProxySession.cpp +++ b/modules/TcpProxySession/src/TcpProxySession.cpp @@ -4,10 +4,54 @@ * @Autor: lishengyin * @Date: 2022-04-11 17:31:02 * @LastEditors: Please set LastEditors - * @LastEditTime: 2022-04-11 22:58:38 + * @LastEditTime: 2022-04-12 00:25:04 */ #include "TcpProxySession.hpp" +#include "login.hpp" +#include +static map> m_ProxySessions; +void TcpProxySession::onRecv(const Buffer::Ptr &buf) { + //处理客户端发送过来的数据 + TraceL << buf->data() << " from port:" << getIdentifier(); + if(this->alive == false){ + Login login; + std::string json = buf->data(); + if(buf->size() > 10 && this->alive == false){ + auto sendBuf = BufferRaw::create(); + if(login.jsonToObject(json)){ + sendBuf->assign("login successfully"); + send((Buffer::Ptr &)sendBuf); + this->alive = true; + this->usr = login.usr; + m_ProxySessions[this->usr].push_back(this); + }else { + sendBuf->assign("Not logged in. Please log in"); + send((Buffer::Ptr &)sendBuf); + } + } + }else{ + auto iter = m_ProxySessions.find(this->usr); + if(iter != m_ProxySessions.end()){ + for(auto iter1 = m_ProxySessions[this->usr].begin(); iter1 != m_ProxySessions[this->usr].end(); iter1++){ + if(*iter1 != this) (*iter1)->send(buf); + } + } + } +} +void TcpProxySession::onError(const SockException &err) { + //客户端断开连接或其他原因导致该对象脱离TCPServer管理 + WarnL << err.what(); + if(!m_ProxySessions[this->usr].empty()){ + vector::iterator iter = find(m_ProxySessions[this->usr].begin(),m_ProxySessions[this->usr].end(), this); + if(iter != m_ProxySessions[this->usr].end()) m_ProxySessions[this->usr].erase(iter); + } +} + +void TcpProxySession::onManager() { + //定时管理该对象,譬如会话超时检查 + // DebugL; +} diff --git a/modules/UserApp/src/UserApp.cpp b/modules/UserApp/src/UserApp.cpp index e583adc..1a7f90a 100644 --- a/modules/UserApp/src/UserApp.cpp +++ b/modules/UserApp/src/UserApp.cpp @@ -4,7 +4,7 @@ * @Autor: lishengyin * @Date: 2022-04-11 17:12:10 * @LastEditors: Please set LastEditors - * @LastEditTime: 2022-04-11 23:11:43 + * @LastEditTime: 2022-04-11 23:58:52 */ #include "UserApp.hpp" @@ -34,6 +34,7 @@ int8_t UserApp::Init(){ */ int8_t UserApp::StartTask(){ this->m_tcpServer->start(this->port); + return OK; } diff --git a/modules/dataType/include/login.hpp b/modules/dataType/include/login.hpp index bbfb7d7..a075c91 100644 --- a/modules/dataType/include/login.hpp +++ b/modules/dataType/include/login.hpp @@ -10,14 +10,14 @@ using namespace std; using namespace std; -class login +class Login { public: std::string usr; std::string pwd; public: - login(){} - ~login(){} + Login(){} + ~Login(){} bool jsonToObject(std::string& json); }; diff --git a/modules/dataType/src/login.cpp b/modules/dataType/src/login.cpp index d593ad8..7e4f2eb 100644 --- a/modules/dataType/src/login.cpp +++ b/modules/dataType/src/login.cpp @@ -1,6 +1,6 @@ #include "login.hpp" -bool login::jsonToObject(std::string& json){ +bool Login::jsonToObject(std::string& json){ rapidjson::Document doc; if (doc.Parse(json.c_str()).HasParseError()) { return false; @@ -12,7 +12,7 @@ bool login::jsonToObject(std::string& json){ }else{ usr = doc["usr"].GetString(); } - + if(end == doc.FindMember("pwd") || !doc["pwd"].IsString()) { return false; }else{ diff --git a/source/src/main.cpp b/source/src/main.cpp index 8cf11b3..1785b4f 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -22,13 +22,13 @@ int main(int argc, char *argv[]) Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); - // std::string AppName = "Satellite"; - // UserApp::Ptr app = UserApp::CreateNew(AppName); - // if(app->Init() != OK) { - // ErrorL << "UserApp Module initialization failed. Procedure" << endl; - // return ERR; - // } - // app->StartTask(); - // sem.wait(); + std::string AppName = "Satellite"; + UserApp::Ptr app = UserApp::CreateNew(AppName); + if(app->Init() != OK) { + ErrorL << "UserApp Module initialization failed. Procedure" << endl; + return ERR; + } + app->StartTask(); + sem.wait(); return 0; }