Browse Source

完成网络透传功能

master
Your Name 3 years ago
parent
commit
979981b8ed
  1. BIN
      lib/libmodels.so
  2. 31
      modules/TcpProxySession/include/TcpProxySession.hpp
  3. 46
      modules/TcpProxySession/src/TcpProxySession.cpp
  4. 3
      modules/UserApp/src/UserApp.cpp
  5. 6
      modules/dataType/include/login.hpp
  6. 4
      modules/dataType/src/login.cpp
  7. 16
      source/src/main.cpp

BIN
lib/libmodels.so

Binary file not shown.

31
modules/TcpProxySession/include/TcpProxySession.hpp

@ -4,12 +4,11 @@
* @Autor: lishengyin * @Autor: lishengyin
* @Date: 2022-04-11 17:30:58 * @Date: 2022-04-11 17:30:58
* @LastEditors: Please set LastEditors * @LastEditors: Please set LastEditors
* @LastEditTime: 2022-04-11 23:14:30 * @LastEditTime: 2022-04-12 00:07:32
*/ */
#ifndef __TCPPROXYSESSION_HPP_ #ifndef __TCPPROXYSESSION_HPP_
#define __TCPPROXYSESSION_HPP_ #define __TCPPROXYSESSION_HPP_
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "Util/logger.h" #include "Util/logger.h"
@ -25,15 +24,6 @@ using namespace toolkit;
class TcpProxySession; class TcpProxySession;
class ProxySessions
{
public:
std::string NameId;
std::shared_ptr<TcpProxySession> session = nullptr;
public:
ProxySessions(){}
~ProxySessions(){}
};
class TcpProxySession: public TcpSession { class TcpProxySession: public TcpSession {
@ -45,21 +35,16 @@ public:
~TcpProxySession() { ~TcpProxySession() {
DebugL; DebugL;
} }
virtual void onRecv(const Buffer::Ptr &buf) override{ virtual void onRecv(const Buffer::Ptr &buf) override;
//处理客户端发送过来的数据
TraceL << buf->data() << " from port:" << getIdentifier(); virtual void onError(const SockException &err) override;
}
virtual void onError(const SockException &err) override{ virtual void onManager() override;
//客户端断开连接或其他原因导致该对象脱离TCPServer管理
WarnL << err.what();
}
virtual void onManager() override{
//定时管理该对象,譬如会话超时检查
// DebugL;
}
private: private:
Ticker _ticker; Ticker _ticker;
bool alive = false;
std::string usr = "";
}; };

46
modules/TcpProxySession/src/TcpProxySession.cpp

@ -4,10 +4,54 @@
* @Autor: lishengyin * @Autor: lishengyin
* @Date: 2022-04-11 17:31:02 * @Date: 2022-04-11 17:31:02
* @LastEditors: Please set LastEditors * @LastEditors: Please set LastEditors
* @LastEditTime: 2022-04-11 22:58:38 * @LastEditTime: 2022-04-12 00:25:04
*/ */
#include "TcpProxySession.hpp" #include "TcpProxySession.hpp"
#include "login.hpp"
#include <algorithm>
static map<std::string, vector<TcpProxySession *>> 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<TcpProxySession *>::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;
}

3
modules/UserApp/src/UserApp.cpp

@ -4,7 +4,7 @@
* @Autor: lishengyin * @Autor: lishengyin
* @Date: 2022-04-11 17:12:10 * @Date: 2022-04-11 17:12:10
* @LastEditors: Please set LastEditors * @LastEditors: Please set LastEditors
* @LastEditTime: 2022-04-11 23:11:43 * @LastEditTime: 2022-04-11 23:58:52
*/ */
#include "UserApp.hpp" #include "UserApp.hpp"
@ -34,6 +34,7 @@ int8_t UserApp::Init(){
*/ */
int8_t UserApp::StartTask(){ int8_t UserApp::StartTask(){
this->m_tcpServer->start<TcpProxySession>(this->port); this->m_tcpServer->start<TcpProxySession>(this->port);
return OK; return OK;
} }

6
modules/dataType/include/login.hpp

@ -10,14 +10,14 @@ using namespace std;
using namespace std; using namespace std;
class login class Login
{ {
public: public:
std::string usr; std::string usr;
std::string pwd; std::string pwd;
public: public:
login(){} Login(){}
~login(){} ~Login(){}
bool jsonToObject(std::string& json); bool jsonToObject(std::string& json);
}; };

4
modules/dataType/src/login.cpp

@ -1,6 +1,6 @@
#include "login.hpp" #include "login.hpp"
bool login::jsonToObject(std::string& json){ bool Login::jsonToObject(std::string& json){
rapidjson::Document doc; rapidjson::Document doc;
if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) { if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
return false; return false;
@ -12,7 +12,7 @@ bool login::jsonToObject(std::string& json){
}else{ }else{
usr = doc["usr"].GetString(); usr = doc["usr"].GetString();
} }
if(end == doc.FindMember("pwd") || !doc["pwd"].IsString()) { if(end == doc.FindMember("pwd") || !doc["pwd"].IsString()) {
return false; return false;
}else{ }else{

16
source/src/main.cpp

@ -22,13 +22,13 @@ int main(int argc, char *argv[])
Logger::Instance().add(std::make_shared<FileChannel>()); Logger::Instance().add(std::make_shared<FileChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>()); Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
// std::string AppName = "Satellite"; std::string AppName = "Satellite";
// UserApp::Ptr app = UserApp::CreateNew(AppName); UserApp::Ptr app = UserApp::CreateNew(AppName);
// if(app->Init() != OK) { if(app->Init() != OK) {
// ErrorL << "UserApp Module initialization failed. Procedure" << endl; ErrorL << "UserApp Module initialization failed. Procedure" << endl;
// return ERR; return ERR;
// } }
// app->StartTask(); app->StartTask();
// sem.wait(); sem.wait();
return 0; return 0;
} }

Loading…
Cancel
Save