#include "stdafx.h"
|
#include "SocketServer.h"
|
|
|
namespace BEQ {
|
CSocketServer::CSocketServer()
|
{
|
m_port = 80;
|
m_listener.onAccept = nullptr;
|
m_listener.onClose = nullptr;
|
}
|
|
CSocketServer::CSocketServer(int port, BEQ::ServerListener listener)
|
{
|
m_port = port;
|
m_listener.onAccept = listener.onAccept;
|
m_listener.onClose = listener.onClose;
|
}
|
|
CSocketServer::~CSocketServer()
|
{
|
|
}
|
|
void CSocketServer::setPort(UINT port)
|
{
|
m_port = port;
|
}
|
|
void CSocketServer::setListenter(BEQ::ServerListener listener)
|
{
|
m_listener.onAccept = listener.onAccept;
|
m_listener.onClose = listener.onClose;
|
}
|
|
void CSocketServer::init()
|
{
|
AfxSocketInit();
|
Create(m_port, 1, FD_ACCEPT | FD_CLOSE);
|
Listen(5);
|
}
|
|
void CSocketServer::OnAccept(int nErrorCode)
|
{
|
sockaddr address;
|
int address_len = sizeof(address);
|
CAcceptClient* pClient = new CAcceptClient();
|
if (Accept(*pClient, &address, &address_len))
|
{
|
int t = 1;
|
pClient->SetSockOpt(TCP_NODELAY, &t, sizeof(int), IPPROTO_TCP);
|
pClient->AsyncSelect(FD_READ | FD_CLOSE);
|
if (m_listener.onAccept != nullptr) {
|
m_listener.onAccept(this, pClient);
|
}
|
}
|
|
|
__super::OnAccept(nErrorCode);
|
}
|
|
void CSocketServer::OnClose(int nErrorCode)
|
{
|
if (m_listener.onClose != nullptr) {
|
m_listener.onClose(this);
|
}
|
|
__super::OnClose(nErrorCode);
|
}
|
|
|
void CSocketServer::OnReceive(int nErrorCode)
|
{
|
TRACE("<CSocketServer>OnReceive´Ë´¦²»Ó¦¸Ã½øÈë...");
|
__super::OnReceive(nErrorCode);
|
}
|
}
|