-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,083 additions
and
929 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.