Skip to content

Commit

Permalink
update server to server reconnect logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
freeeyes committed Mar 19, 2024
1 parent 61b762c commit 5d5e9d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions PSS_ASIO/Common/CommunicationService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ bool CCommunicationService::add_connect(const CConnect_IO_Info& io_info, EM_CONN

void CCommunicationService::set_connect_id(uint32 server_id, uint32 connect_id)
{
std::lock_guard <std::recursive_mutex> lock(mutex_);

//查找是否存在正在连接的
auto server_is_connecting = server_is_connect_list_.find(server_id);
if (server_is_connecting != server_is_connect_list_.end())
{
//找到了清除之
server_is_connect_list_.erase(server_id);
}

auto f = communication_list_.find(server_id);
if (f != communication_list_.end())
{
Expand Down Expand Up @@ -71,6 +81,22 @@ void CCommunicationService::io_connect(CCommunicationIOInfo& connect_info)
return;
}

//判断是否存在正在连接的对象(需要加锁)
{
std::lock_guard <std::recursive_mutex> lock(mutex_);
auto server_connecting = server_is_connect_list_.find(connect_info.io_info_.server_id);
if (server_connecting != server_is_connect_list_.end())
{
//找到了正在连接服务器的对象,则退出
PSS_LOGGER_DEBUG("[CCommunicationService::io_connect]server_id is connecting.", connect_info.io_info_.server_id);
return;
}
else
{
server_is_connect_list_[connect_info.io_info_.server_id] = connect_info.io_info_.server_id;
}
}

if (connect_info.io_type_ == EM_CONNECT_IO_TYPE::CONNECT_IO_TCP)
{
//IO是TCP
Expand Down
3 changes: 3 additions & 0 deletions PSS_ASIO/Common/CommunicationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ class CCommunicationService : public ICommunicationInterface
private:
using communication_list = unordered_map<uint32, CCommunicationIOInfo>;
using server_connect_id_list = unordered_map<uint32, uint32>;
using server_is_connect_list = unordered_map<uint32, uint32>;
communication_list communication_list_;
server_connect_id_list server_connect_id_list_;
server_is_connect_list server_is_connect_list_;

std::recursive_mutex mutex_;
CreateIoContextCallbackFunc callback_;
bool communication_is_run_ = false;
Expand Down

0 comments on commit 5d5e9d4

Please sign in to comment.