-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExchangeConnectionHandler.cpp
233 lines (178 loc) · 4.75 KB
/
ExchangeConnectionHandler.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// ExchangeDataHandler.cpp: implementation of the CExchangeConnectionHandler class.
//
//////////////////////////////////////////////////////////////////////
#include "ExchangeConnectionHandler.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CExchangeConnectionHandler::CExchangeConnectionHandler(STExchangeConnectionInfo _ExConInfo)
{
this->m_ExchnageConnectionInfo = _ExConInfo;
m_Connected = 0;
}
CExchangeConnectionHandler::~CExchangeConnectionHandler()
{
Destroy();
}
int CExchangeConnectionHandler::handle_close(ACE_HANDLE handle, ACE_Reactor_Mask mask)
{
m_Connected = 0;
this->peer().close();
m_Event.signal();
return 0;
}
int CExchangeConnectionHandler::handle_input(ACE_HANDLE handle)
{
try
{
long _bufferSize = m_ExchnageConnectionInfo.BufferSize;
//--- If Buffer Size is Encoded, Decode It ----------
if(_bufferSize < 0)
_bufferSize = GetDynamicBufferSize(_bufferSize);
//---------------------------------------------------
szMsg = new char[_bufferSize+1];
memset(szMsg, 0, _bufferSize+1);
int nBytesRead = this->peer().recv(szMsg, _bufferSize);
if ( nBytesRead == 0 )
{
ACE_DEBUG( ( LM_DEBUG, "(%t) On %D\n" ) );
ACE_DEBUG( ( LM_DEBUG, "(%t) Data Received is not complete...\n\n" ) );
}
if ( nBytesRead > 0 )
{
std::string _item = szMsg;
delete szMsg;
this->m_Queue.push(_item);
}
else
{
delete szMsg;
ACE_DEBUG( ( LM_DEBUG, "(%t) On %D\n" ) );
ACE_DEBUG( ( LM_DEBUG, "(%t) Incomming Connection From FeedServer is disconnected...\n\n" ) );
m_Connected = 0;
return -1;
}
}
catch(int a){}
return 0;
}
bool CExchangeConnectionHandler::Start()
{
if( ACE_Thread::spawn((ACE_THR_FUNC)CExchangeConnectionHandler::Start_,
this,
THR_JOINABLE | THR_NEW_LWP,
&m_processorThreadID,
&m_processorThreadHandle ) == -1 )
{
// TODO: Log
return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////
void CExchangeConnectionHandler::Start_Callback()
{
while ( m_bthreadContinue )
{
CExchangeConnectionHandler* _svcHandler = this;
ACE_INET_Addr _endPoint(m_ExchnageConnectionInfo.Port, m_ExchnageConnectionInfo.IP.c_str());
try
{
if ( (0 == m_Connected || 2 == m_Connected) && (this->m_Connector.connect(_svcHandler, _endPoint) == -1))
{
m_Connected = 0;
Sleep(5000);
m_Connected = 2;
Sleep(5000);
continue;
}
else
{
m_Connected = 1;
char _date[50];
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime(_date,25,"%d %b %Y %H:%M:%S", timeinfo);
Last_Connection_Time = _date;
m_Event.wait(INFINITE);
}
}
catch(int exp)
{
}
}
}
//////////////////////////////////////////////////////////////////////////
void CExchangeConnectionHandler::Start_(void * pParam )
{
((CExchangeConnectionHandler*)pParam)->Start_Callback();
}
std::string CExchangeConnectionHandler::GetMessage( std::string& _delimeter )
{
std::string _item, _ret;
int _position;
while( true)
{
_item.erase();
if(false == m_Queue.pop(_item))
{
m_Queue.wait();
continue;
}
//--- Consider every item in the Queue as a complete message if BufferSize is Dynamic ( i.e < 0 )
if(m_ExchnageConnectionInfo.BufferSize < 0)
{
m_Queue.pop(_item);
return _item;
}
//-----------------------------------------------------------------------------------------------
m_temp += _item;
_position = m_temp.find(_delimeter);
if(_position > 0)
{
_ret = m_temp.substr(0, _position);
m_temp = m_temp.substr(_position+1,m_temp.length()+1);
return _ret;
}
}
}
bool CExchangeConnectionHandler::Create( void )
{
bool _ret = false;
m_bthreadContinue = true;
_ret = (bool)Start();
//szMsg = new char[m_ExchnageConnectionInfo.BufferSize];
return _ret;
}
bool CExchangeConnectionHandler::Destroy( void )
{
bool _ret;
m_bthreadContinue = false;
if(szMsg) delete[] szMsg;
return true;
}
long CExchangeConnectionHandler::GetDynamicBufferSize(long _bufferSize)
{
u_long _actuaSize = 0;
char _termpBuffer[20] = "\0";
switch(_bufferSize)
{
case -2:
if(peer().recv(_termpBuffer,2) > 0)
{
memcpy(&_actuaSize, _termpBuffer, 2);
_actuaSize = ntohs(_actuaSize);
}
break;
case -4:
if( peer().recv(_termpBuffer,4) > 0)
{
memcpy(&_actuaSize, _termpBuffer, 4);
_actuaSize = ntohl(_actuaSize);
}
break;
}
return _actuaSize;
}