|
|
|
@ -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;
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|