Skip to content

Commit

Permalink
服务器端代码重构
Browse files Browse the repository at this point in the history
  • Loading branch information
prudens committed Nov 7, 2017
1 parent fe75ce9 commit 6f9446e
Show file tree
Hide file tree
Showing 28 changed files with 1,083 additions and 929 deletions.
19 changes: 18 additions & 1 deletion base/async_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ namespace audio_engine
STask( ThreadPool* thread )
:_thread(thread->GetTaskThread())
{
}
STask( TaskThread&thread )
:_thread(thread)
{

}
template<class F, class... Args>
auto AddTask( F&& f, Args&&... args )
Expand All @@ -145,12 +150,16 @@ namespace audio_engine
_thread.AddTask(std::move(t));
return res;
}
TaskThread& GetTaskThread()
{
return _thread;
}
void Stop()
{
_stop = true;
}
private:
TaskThread& _thread;
TaskThread& _thread;
bool _stop = false;
std::mutex _mutex;
};
Expand All @@ -162,6 +171,10 @@ namespace audio_engine
{
_stask = std::make_shared<STask>(pool);
}
AsyncTask( TaskThread& thread )
{
_stask = std::make_shared<STask>(thread);
}
~AsyncTask()
{
_stask->Stop();
Expand All @@ -173,6 +186,10 @@ namespace audio_engine
{
return _stask->AddTask(std::forward<F>(f),std::forward<Args>(args)...);
}
TaskThread& TaskThread()
{
return _stask->GetTaskThread();
}
private:
std::shared_ptr<STask> _stask;
};
Expand Down
3 changes: 1 addition & 2 deletions base/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ namespace audio_engine
virtual std::error_code DisConnect()
{
std::error_code ec;
_socket.shutdown( asio::ip::tcp::socket::shutdown_both, ec );
_socket.close( ec );
_socket.shutdown( asio::ip::tcp::socket::shutdown_receive, ec );
return ec;
}
virtual void AsyncConnect( std::string ip, int16_t port, ConnectHandle handle )override
Expand Down
90 changes: 0 additions & 90 deletions outsource/real_audio_server/account_verify.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions outsource/real_audio_server/account_verify.h

This file was deleted.

93 changes: 93 additions & 0 deletions outsource/real_audio_server/client_conn_mgr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "client_conn_mgr.h"
#include "error_code.h"
#include "server_config.h"
#include "server_module.h"
#include "master_control.h"
#define MIN_SUPPORTED_VERSION "20171105"

namespace audio_engine{
ClientConnMgr::ClientConnMgr(MasterControl *ctrl)
:_task(ServerModule::GetInstance()->GetThreadPool())
{
_master_ctrl = ctrl;
}

ClientConnMgr::~ClientConnMgr()
{

}

void ClientConnMgr::StartListen()
{
std::string ip;
int16_t port;
if(ServerModule::GetInstance()->GetServerCnfig()->GetServer( 1, ip, port ))
{
TcpFactory *f = ServerModule::GetInstance()->GetSocketManager();
_acceptor = f->CreateTcpAcceptr( ip, port );
//ASSERT( _acceptor );
_acceptor->AsyncAccept( std::bind( &ClientConnMgr::HandleAccept, this, std::placeholders::_1, std::placeholders::_2 ) );
}
}

void ClientConnMgr::StopListen()
{
_acceptor->DisAccept();
_acceptor.reset();
}

bool ClientConnMgr::HandleAccept( std::error_code ec, TcpSocketPtr tcp )
{
if(ec)
{
printf( "accept error:%s\n", ec.message().c_str() );
}
if(!ec)
{
std::string ip;
int16_t port;
if(!tcp->QuerySocketInfo( ip, port ))
{
printf( "收到客户端新连接:%s:%u\n", ip.c_str(), (uint16_t)port );
}

auto user_conn = std::make_shared<UserConnection>( tcp, _task.TaskThread() );//保证跟上面同一线程
user_conn->SetVerifyAccountCB( std::bind( &ClientConnMgr::VerifyAccount, this, std::placeholders::_1,std::placeholders::_2) );
user_conn->Start();
}
if(_stop)
{
return false;
}
_acceptor->AsyncAccept( std::bind( &ClientConnMgr::HandleAccept, this, std::placeholders::_1, std::placeholders::_2 ) );
return true;
}

int ClientConnMgr::VerifyAccount( RAUserMessagePtr pb, UserConnPtr conn )
{
if( pb->has_request_login() )
{
auto reqLogin = pb->request_login();

if(reqLogin.version() < MIN_SUPPORTED_VERSION)
{
return ERR_NOT_VERSION_SUPPORTED;
}
if(reqLogin.userid().empty())
{
return ERR_INVALID_USER_ID;
}
if(reqLogin.roomkey().empty())
{
return ERR_INVALID_ROOM_KEY;
}

_master_ctrl->JoinRoom( pb, conn);
}
else
{
return ERR_INVALID_ARGUMENT;
}
return ERR_OK;
}
}
24 changes: 24 additions & 0 deletions outsource/real_audio_server/client_conn_mgr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <string>
#include "user_connection.h"
#include "base/tcp_socket.h"
#include "base/async_task.h"

namespace audio_engine{
class MasterControl;
class ClientConnMgr
{
public:
ClientConnMgr(MasterControl* host);
~ClientConnMgr();
void StartListen();
void StopListen();
int VerifyAccount( RAUserMessagePtr pb, UserConnPtr conn );
bool HandleAccept( std::error_code ec, TcpSocketPtr tcp );
private:
TcpAcceptorPtr _acceptor;
AsyncTask _task;
bool _stop = false;
MasterControl *_master_ctrl;
};
}
35 changes: 24 additions & 11 deletions outsource/real_audio_server/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#include "server_module.h"
#include "user_manager.h"
#include <thread>
#include <chrono>
#include "service.h"

using namespace audio_engine;
int main( int argc, char** argv )
{
ServerModule::CreateInstance();
auto usermgr = std::make_shared<UserManager>();
usermgr->Start();
system( "pause" );
usermgr->Stop();
usermgr.reset();
ServerModule::DestroyInstance();
bool running = true;
while(running)
{
Service service;
service.Run();
std::string cmd;
while(true)
{
std::cin >> cmd;
if(cmd == "q")
{
running = false;
break;
}
else if( cmd == "restart" )
{
break;
}
}
}


return 0;
}
37 changes: 37 additions & 0 deletions outsource/real_audio_server/master_control.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "master_control.h"
#include "error_code.h"
namespace audio_engine{
MasterControl::MasterControl()
:_conn_mgr(this)
{

}
MasterControl::~MasterControl()
{

}

void MasterControl::Start()
{
_conn_mgr.StartListen();
}

int MasterControl::JoinRoom( RAUserMessagePtr pb, UserConnPtr conn )
{
auto reqLogin = pb->request_login();
auto roomkey = reqLogin.roomkey();
auto it = _rooms.find(roomkey);
if(it != _rooms.end())
{
if(it->second.FindMember(reqLogin.userid()))
{
// 已经有一个同名用户在里面。
}
else
{
it->second.HandleConnection(conn);
}
}
return ERR_OK;
}
}
Loading

0 comments on commit 6f9446e

Please sign in to comment.