LAPTOP-SNT8I5JK\Boounion
2025-08-29 9e25e06d7ec9d7cd1c31a9d30a8a018e2af63e3a
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
#include "stdafx.h"
#include "HsmsAction.h"
 
 
CHsmsAction::CHsmsAction()
{
    m_nAction = ACTION_IDLE;
    m_bNeedWaitReply = FALSE;
    m_nTimeout = 45;
    m_nResponseTime = 0;
    m_pContext = NULL;
    m_hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
}
 
CHsmsAction::CHsmsAction(int nAction, BOOL bNeedReply, unsigned int nTimeout)
{
    m_nAction = nAction;
    m_bNeedWaitReply = bNeedReply;
    m_nTimeout = nTimeout;
    m_nResponseTime = 0;
    m_pContext = NULL;
    m_hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
}
 
CHsmsAction::~CHsmsAction()
{
    if (m_hEvent != NULL) {
        CloseHandle(m_hEvent);
        m_hEvent = NULL;
    }
 
    if (m_pSendMessage != NULL) {
        HSMS_Destroy1Message(m_pSendMessage);
        m_pSendMessage = NULL;
    }
}
 
void CHsmsAction::reset()
{
    m_nAction = ACTION_IDLE;
    m_bNeedWaitReply = FALSE;
    m_nTimeout = 45;
    m_nResponseTime = 0;
    ::ResetEvent(m_hEvent);
}
 
void CHsmsAction::resetResponseTime()
{
    m_nResponseTime = 0;
}
 
void CHsmsAction::setAction(int nAction, BOOL bNeedReply/* = TRUE*/, unsigned int nTimeout/* = 45*/)
{
    m_nAction = nAction;
    m_bNeedWaitReply = bNeedReply;
    m_nTimeout = nTimeout;
}
 
int CHsmsAction::getAction()
{
    return m_nAction;
}
 
void CHsmsAction::setContext(CContext* pContext)
{
    m_pContext = pContext;
}
 
CContext* CHsmsAction::getContext()
{
    return m_pContext;
}
 
void CHsmsAction::setSendMessage(IMessage* pMessage)
{
    m_pSendMessage = pMessage;
}
 
IMessage* CHsmsAction::getSendMessage()
{
    return m_pSendMessage;
}
 
unsigned int CHsmsAction::getTimeout()
{
    return m_nTimeout;
}
 
BOOL CHsmsAction::isNeedWaitReply()
{
    return m_bNeedWaitReply;
}
 
HANDLE CHsmsAction::getEvent()
{
    return m_hEvent;
}
 
int CHsmsAction::responseTimeIncrement()
{
    return InterlockedIncrement(&m_nResponseTime);
}
 
BOOL CHsmsAction::incrementAndCheckTimeout()
{
    unsigned int nResponseTime = InterlockedIncrement(&m_nResponseTime);
    return nResponseTime >= m_nTimeout + 1;
}
 
int CHsmsAction::serialize(char* pszBuffer, int nBufferSize)
{
    int index = 0;
    if (pszBuffer == nullptr) {
        index += sizeof(int);
        index += sizeof(m_nTimeout);
        index += sizeof(int);
        index += sizeof(BOOL);
        index += m_pSendMessage->serialize(pszBuffer, nBufferSize);
 
        return index;
    }
    else {
        memcpy(&pszBuffer[index], &m_nAction, sizeof(int));
        index += sizeof(int);
 
        memcpy(&pszBuffer[index], &m_nTimeout, sizeof(int));
        index += sizeof(int);
 
        memcpy(&pszBuffer[index], &m_nResponseTime, sizeof(int));
        index += sizeof(int);
 
        memcpy(&pszBuffer[index], &m_bNeedWaitReply, sizeof(BOOL));
        index += sizeof(BOOL);
 
        index += m_pSendMessage->serialize(&pszBuffer[index], nBufferSize - index);
 
        return index;
    }
}
 
int CHsmsAction::unserialize(const char* pszBuffer, int nBufferSize)
{
    int index = 0;
    if (index + sizeof(int) > nBufferSize) return -1;
    memcpy(&m_nAction, &pszBuffer[index], sizeof(int));
    index += sizeof(int);
 
    if (index + sizeof(int) > nBufferSize) return -1;
    memcpy(&m_nTimeout, &pszBuffer[index], sizeof(int));
    index += sizeof(int);
 
    if (index + sizeof(int) > nBufferSize) return -1;
    memcpy(&m_nResponseTime, &pszBuffer[index], sizeof(int));
    index += sizeof(int);
 
    if (index + sizeof(BOOL) > nBufferSize) return -1;
    memcpy(&m_bNeedWaitReply, &pszBuffer[index], sizeof(BOOL));
    index += sizeof(BOOL);
 
    HSMS_Create1Message(m_pSendMessage, 1, 1 | REPLY, 1, 1);
    int nRet = m_pSendMessage->unserialize(&pszBuffer[index], nBufferSize - index);
    if (nRet < 0) return nRet;
 
    return index + nRet;
}