chenluhua1980
2026-01-08 e878ac7788fef44639145ffd971ab0eb06d692ad
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "stdafx.h"
#include "Servo.h"
#include "RemoteEquipment.h"
 
namespace BEQ {
    CServo::CServo()
    {
        m_pClientListener = nullptr;
        m_servoListener.onRemoteEqConnecting = nullptr;
        m_servoListener.onRemoteEqConnected = nullptr;
        m_servoListener.onRemoteEqConnectFailed = nullptr;
        m_servoListener.onRemoteEqDisconnecting = nullptr;
        m_servoListener.onRemoteEqDisconnected = nullptr;
        m_servoListener.onRemoteEqReadRawdata = nullptr;
        m_servoListener.onRemoteEqEventUpdate = nullptr;
    }
 
    CServo::CServo(const char* pszName)
    {
        m_strName = pszName;
        m_pClientListener = nullptr;
    }
 
    CServo::~CServo()
    {
        for (auto iter = m_clients.begin(); iter != m_clients.end(); iter++) {
            delete *iter;
        }
        m_clients.clear();
 
        if (m_pClientListener != nullptr) {
            delete m_pClientListener;
            m_pClientListener = nullptr;
        }
 
        if (m_pServer != nullptr) {
            m_pServer->Close();
            delete m_pServer;
            m_pServer = nullptr;
        }
 
        for (auto item : m_remoteEquipments) {
            delete (CRemoteEquipment*)item;
        }
        m_remoteEquipments.clear();
    }
 
    void CServo::setListener(BEQ::ServoListener listener)
    {
        m_servoListener.onRemoteEqConnecting = listener.onRemoteEqConnecting;
        m_servoListener.onRemoteEqConnected = listener.onRemoteEqConnected;
        m_servoListener.onRemoteEqConnectFailed = listener.onRemoteEqConnectFailed;
        m_servoListener.onRemoteEqDisconnecting = listener.onRemoteEqDisconnecting;
        m_servoListener.onRemoteEqDisconnected = listener.onRemoteEqDisconnected;
        m_servoListener.onRemoteEqReadRawdata = listener.onRemoteEqReadRawdata;
        m_servoListener.onRemoteEqEventUpdate = listener.onRemoteEqEventUpdate;
    }
 
    int CServo::getName(char* pszBuffer, int nMaxCount)
    {
        return strcpy_s(pszBuffer, nMaxCount, m_strName.c_str());
    }
 
    int CServo::createRemoteEquipment(IRemoteEquipment*& pRemoteEquipment, const char* pszAddr, int port)
    {
        CRemoteEquipment* p = new CRemoteEquipment(pszAddr, port);
        pRemoteEquipment = (IRemoteEquipment*)p;
        m_remoteEquipments.push_back(pRemoteEquipment);
 
        RemoteEquipmentListener listener;
        listener.onConnecting = [&](void* pRemoteEiuipment) -> void {
            if (m_servoListener.onRemoteEqConnecting != nullptr) {
                m_servoListener.onRemoteEqConnecting(this, pRemoteEquipment);
            }
        };
        listener.onConnected = [&](void* pRemoteEiuipment) -> void {
            if (m_servoListener.onRemoteEqConnected != nullptr) {
                m_servoListener.onRemoteEqConnected(this, pRemoteEquipment);
            }
        };
        listener.onConnectFailed = [&](void* pRemoteEiuipment, int errorCode) -> void {
            if (m_servoListener.onRemoteEqConnectFailed != nullptr) {
                m_servoListener.onRemoteEqConnectFailed(this, pRemoteEquipment, errorCode);
            }
        };
        listener.onDisconnecting = [&](void* pRemoteEiuipment) -> void {
            if (m_servoListener.onRemoteEqDisconnecting != nullptr) {
                m_servoListener.onRemoteEqDisconnecting(this, pRemoteEquipment);
            }
        };
        listener.onDisconnected = [&](void* pRemoteEiuipment) -> void {
            if (m_servoListener.onRemoteEqDisconnected != nullptr) {
                m_servoListener.onRemoteEqDisconnected(this, pRemoteEquipment);
            }
        };
        listener.onRead = [&](void* pRemoteEiuipment, const char* pszData, int len) -> void {
            if (m_servoListener.onRemoteEqReadRawdata != nullptr) {
                m_servoListener.onRemoteEqReadRawdata(this, pRemoteEquipment, pszData, len);
            }
        };
        listener.onEventUpdate = [&](void* pRemoteEiuipment, void* pUnit, REMOTE_EQ_EVENT eventCode) -> void {
            if (m_servoListener.onRemoteEqEventUpdate != nullptr) {
                m_servoListener.onRemoteEqEventUpdate(this, pRemoteEquipment, pUnit, eventCode);
            }
        };
        p->setRemoteEquipmentListener(listener);
 
        return 0;
    }
 
    int CServo::connectRemoteEquipment(IRemoteEquipment* pRemoteEquipment)
    {
        if (!findRemoteEquipment(pRemoteEquipment)) return -1;
        return pRemoteEquipment->connect();
    }
 
    bool CServo::findRemoteEquipment(IRemoteEquipment* pRemoteEquipment)
    {
        for (auto item : m_remoteEquipments) {
            if (item == pRemoteEquipment) return true;
        }
 
        return false;
    }
 
    int CServo::runOnServerMode(int port)
    {
        return -1;
 
        // ÔÝʱ²»Ö§³ÖServerģʽ
        m_pClientListener = new AcceptClientListener;
        m_pClientListener->onClose = [&](void* pClient) -> int {
            removeClient((CAcceptClient*)pClient);
            return 0;
        };
        m_pClientListener->onRead = [&](void* pClient, const char* pData, int len) -> int {
            AfxMessageBox(CString(pData, len));
            return 0;
        };
 
        ServerListener listener;
        listener.onAccept = [&](void* pServer, CAcceptClient* pClient) -> int {
            addClient(pClient);
            return 0;
        };
        listener.onClose = [&](void* pServer) -> int {
            AfxMessageBox("onClose");
            return 0;
        };
 
        m_pServer = new CSocketServer(port, listener);
        m_pServer->init();
        return 0;
    }
 
    int CServo::close()
    {
        return 0;
    }
 
    void CServo::addClient(CAcceptClient* pClient)
    {
        m_clients.push_back(pClient);
        pClient->setListener(*m_pClientListener);
    }
 
    void CServo::removeClient(CAcceptClient* pClient)
    {
        for (auto iter = m_clients.begin(); iter != m_clients.end(); iter++) {
            if (*iter == pClient) {
                delete *iter;
                m_clients.erase(iter);
                break;
            }
        }
    }
}