LAPTOP-SNT8I5JK\Boounion
2025-05-24 1f5f8a22e9c584f709d4b34d2e4d24eaad2d5544
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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);
    }
}