a8/a8/tcplistener.h
2018-08-26 20:34:01 +08:00

57 lines
1.4 KiB
C++

#ifndef A8_TCPLISTENER_H
#define A8_TCPLISTENER_H
namespace a8
{
enum TCPLISTENER_E
{
TE_CREATE_ERR,
TE_SETSOCKOPT_ERR,
TE_BIND_ERR,
TE_LISTEN_ERR,
};
struct TcpListenerImpl;
class TcpSession;
class TcpListener
{
public:
std::function<void (a8::TcpSession**)> on_create_client_socket;
std::function<void (const char*, int, bool&)> on_client_connect;
std::function<void (a8::TcpListener*, a8::TCPLISTENER_E, int)> on_error;
std::string bind_address;
unsigned short bind_port = 0;
public:
TcpListener(unsigned short max_client_cnt=0xEFFF);
virtual ~TcpListener();
void Open();
void Close();
bool IsActive();
bool SendClientMsg(unsigned short sockhandle, const char *buff, int buffLen);
void BroadcastMsg(const char* buff, int bufflen);
void ForceCloseClient(unsigned short sockhandle);
void MarkClient(unsigned short sockhandle, bool is_active);
int GetClientSocketCount();
int GetPoolSocketCount();
private:
void LockClients();
void FreeClientWithNoLock(a8::TcpSession *session);
void UnLockClients();
a8::TcpSession* GetClientSession(unsigned short handle);
private:
a8::TcpListenerImpl* impl_ = nullptr;
friend class TcpSession;
friend class a8::TcpListenerImpl;
};
}
#endif