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. 2
      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 @@ @@ -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 <iostream>
#include <signal.h>
#include "Util/logger.h"
@ -25,15 +24,6 @@ using namespace toolkit; @@ -25,15 +24,6 @@ using namespace toolkit;
class TcpProxySession;
class ProxySessions
{
public:
std::string NameId;
std::shared_ptr<TcpProxySession> session = nullptr;
public:
ProxySessions(){}
~ProxySessions(){}
};
class TcpProxySession: public TcpSession {
@ -45,21 +35,16 @@ public: @@ -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 = "";
};

46
modules/TcpProxySession/src/TcpProxySession.cpp

@ -4,10 +4,54 @@ @@ -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 <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 @@ @@ -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(){ @@ -34,6 +34,7 @@ int8_t UserApp::Init(){
*/
int8_t UserApp::StartTask(){
this->m_tcpServer->start<TcpProxySession>(this->port);
return OK;
}

6
modules/dataType/include/login.hpp

@ -10,14 +10,14 @@ using namespace std; @@ -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);
};

2
modules/dataType/src/login.cpp

@ -1,6 +1,6 @@ @@ -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<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
return false;

16
source/src/main.cpp

@ -22,13 +22,13 @@ int main(int argc, char *argv[]) @@ -22,13 +22,13 @@ int main(int argc, char *argv[])
Logger::Instance().add(std::make_shared<FileChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
// 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;
}

Loading…
Cancel
Save