-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDLRacerInterface.cpp
235 lines (213 loc) · 4.67 KB
/
DLRacerInterface.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
234
235
#include "StdAfx.h"
#include "DLRacerInterface.h"
#include "MainFrm.h"
#include "MyMessageBox.h"
#include "resource.h"
void msgbox(CString str)
{
MyMessageBox_Error(str);
}
//a TCP interface interacting with RacerPro rewritten by me in C++, by consulting the original JRacer interface (a Java TCP interface).
DLRacerInterface::DLRacerInterface(CString a_strIP, int a_iPort)
{
racerSocket = NULL;
m_strIP = a_strIP;
m_iPort = a_iPort;
WORD wVersionRequested =MAKEWORD(1, 1);
int iError = WSAStartup(wVersionRequested, &wsaData);
if (iError != 0)
{
MyMessageBox_Error(_T("DLRacerInterface"));
}
else
{
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
MyMessageBox_Error(_T("DLRacerInterface"));
}
}
}
DLRacerInterface::~DLRacerInterface()
{
closesocket(racerSocket);
}
BOOL DLRacerInterface::openConnection()
{
int result;
racerSocket = socket(AF_INET, SOCK_STREAM, 0);
//CMainFrame *pMainFrm = (CMainFrame*) AfxGetMainWnd();
//WSAAsyncSelect(racerSocket, pMainFrm->m_hWnd, WM_SOCKET, FD_ACCEPT | FD_CLOSE | FD_READ | FD_WRITE);
if(racerSocket == INVALID_SOCKET)
{
msgbox(_T("openConnection"));
return FALSE;
}
else
{
racerAddr.sin_family = AF_INET;
racerAddr.sin_port = htons(m_iPort);
racerAddr.sin_addr.s_addr = inet_addr(_bstr_t(m_strIP));
result = connect(racerSocket, (SOCKADDR*) &racerAddr, sizeof(racerAddr));
if (result == -1)
{
//msgbox(_T("openConnection"));
int dwError = WSAGetLastError(); //akisn0w
if (dwError == 10035)
{
//Ô¶³ÌûÆô¶¯£¬»¹ËãÕý³£
return FALSE;
}
else
{
MyMessageBox_Error(_T("openConnection"));
return FALSE;
}
}
else
{
return TRUE;
}
}
}
void DLRacerInterface::closeConnection()
{
if (racerSocket)
{
closesocket(racerSocket);
racerSocket = INVALID_SOCKET;
}
//WSACleanup();
}
CString DLRacerInterface::readFromSocket()
{
char c;
CString strTemp;
recv(racerSocket, &c, 1, 0);
strTemp += c;
while (c != 10)
{
recv(racerSocket, &c, 1, 0);
if (c != 10)
{
strTemp += c;
}
}
// char buf[1024];
// recv(racerSocket, buf, 1024, 0);
// strTemp += buf;
//
// while (c != 10)
// {
// recv(racerSocket, &c, 1, 0);
// if (c != 10)
// {
// strTemp += c;
// }
// }
return strTemp;
}
CString DLRacerInterface::sendCommand(CString strCommand)
{
strCommand += _T("\n");
int status;
status = send(racerSocket, strCommand, strCommand.GetLength(), 0);
if (status == -1)
{
int dwError = WSAGetLastError();
MyMessageBox_Error(_T("sendCommand"));
return _T("null");
}
CString strResult = readFromSocket();
return parseResult(strCommand, strResult);
}
CString DLRacerInterface::parseResult(CString strCommand, CString strResult)
{
if (strResult[1] == _T('o'))
{
CString strWarning = getWarningFromOK(strResult);
if (strWarning != _T(""))
printWarning(strWarning);
return _T("null");
}
else if (strResult[1] == _T('e'))
{
CString strError = getError(strResult);
return strError;
}
else if (strResult[1] == _T('a'))
{
vector<CString> strAnsAndWar = getResultAndWarningFromAnswer(strResult);
if (strAnsAndWar[1] != _T(""))
{
printWarning(strAnsAndWar[1]);
}
return strAnsAndWar[0];
}
else
{
msgbox(_T("parseResult"));
return _T("parseResult");
}
}
CString DLRacerInterface::getWarningFromOK(CString strResult)
{
CString strWarning = _T("");
int ini = strResult.Find(_T('"'), 6);
int fi = strResult.GetLength() - 1;
if (ini < fi - 1)
{
strWarning = strResult.Mid(ini + 1, fi - ini - 1);
}
return strWarning;
}
void DLRacerInterface::printWarning(CString strWarning)
{
msgbox(strWarning);
// for (int i = 0; i < strWarning.GetLength(); i ++)
// {
// char c = strWarning[i];
// if (c == 9)
// {
// //cout << endl;
// }
// else
// {
// msgbox(strWarning[i]);
// }
// }
}
CString DLRacerInterface::getError(CString strResult)
{
CString s;
int iniMessage = strResult.Find(_T(' '), 7);
int ini = strResult.Find(_T('"'), iniMessage);
int fi = strResult.Find(_T('"'), ini + 1);
s = (iniMessage + 1 < ini - 1)? strResult.Mid(iniMessage + 1, ini - 1 - iniMessage - 1) : _T("");
if ((iniMessage + 1 < ini - 1) && (ini + 1 < fi))
{
s = s + strResult.Mid(ini + 1, fi - ini - 1);
}
return s;
}
vector<CString> DLRacerInterface::getResultAndWarningFromAnswer(CString strResult)
{
vector<CString> s;
int ini = strResult.Find(_T('"'), 10);
int fi = ini;
bool esFinal = FALSE;
while (!esFinal)
{
fi = strResult.Find(_T('"'), fi + 1);
esFinal = (strResult[fi -1] != _T('\\'));
}
s.push_back(strResult.Mid(ini + 1, fi - ini - 1));
if (fi + 4 < strResult.GetLength())
{
s.push_back(strResult.Mid(fi + 3, strResult.GetLength() - 1 - fi - 3));
}
else
{
s.push_back(_T(""));
}
return s;
}