-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpclient.cpp
More file actions
180 lines (154 loc) · 4.61 KB
/
tcpclient.cpp
File metadata and controls
180 lines (154 loc) · 4.61 KB
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
#include "tcpsock.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <io.h>
#include <fcntl.h>
#include <time.h>
#include "EnumCmdID.pb.h"
#include "message.pb.h"
using namespace std;
using namespace pb;
typedef struct{
int32_t id;
int32_t len; //len(body), not including field:id
char pbData[1]; //body
}MSG_T;
#define TCP_MSG_LEN 500
int GetTimeofday(struct timeval* tv, struct timezone* tz)
{
time_t clock;
struct tm tm;
SYSTEMTIME win_time;
GetLocalTime(&win_time);
tm.tm_year = win_time.wYear - 1900;
tm.tm_mon = win_time.wMonth - 1;
tm.tm_mday = win_time.wDay;
tm.tm_hour = win_time.wHour;
tm.tm_min = win_time.wMinute;
tm.tm_sec = win_time.wSecond;
tm.tm_isdst = -1;
clock = mktime(&tm);
tv->tv_sec = (long)clock;
tv->tv_usec = win_time.wMilliseconds * 1000;
return 0;
}
int64_t GetTimestamp(void)
{
struct timeval tv;
GetTimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}
int TcpSendPingMsg(string host, int port, char resBuf[])
{
string resp_s = "";
cout << "host=" << host << ",port=" << port << endl;
int ret = 0;
char buf[TCP_MSG_LEN]={0};
MSG_T *pMsg= (MSG_T*)buf;
Ping pingReq;
pingReq.set_timestamp(GetTimestamp());
pingReq.set_hello("ping");
string pingReq_s;
pingReq.SerializeToString(&pingReq_s);
pMsg->id = PING;
strncpy(pMsg->pbData, pingReq_s.c_str(),pingReq_s.size());
pMsg->len = pingReq_s.length();
int len = 4 + 4 + pMsg->len;
//cout << "msg.id=" << pMsg->id << ",len=" << pMsg->len << endl;
try{
SocketClient s(host,port);
ret = s.SendBuf(buf, len);
Ping pingRes;
memset(buf, 0, sizeof(buf));
s.RecvBuf(buf, sizeof(buf));
resp_s = pMsg->pbData;
pingRes.ParseFromString(resp_s);
int64_t ts = pingRes.timestamp();
string msg = pingRes.hello();
cout << "recv. msg.id="<<pMsg->id<<",len="<<pMsg->len<<endl;
cout << "recv pbData. ts=" << ts << ",hello=" << msg.c_str() << endl;
sprintf(resBuf,"ts=%lld,%s\n",ts,msg.c_str());
return strlen(resBuf);
}
catch(const char *s){
cerr<<s<<endl;
}
catch(std::string s){
cerr<<s<<endl;
}
catch(...){
cerr<<"unknown exception happen"<<endl;
}
return 0;
}
int TcpSendSceneMsg(string host, int port, string devId, string streamId, string scene, char resBuf[])
{
string resp_s = "";
cout << "host=" << host << ",port=" << port << endl;
int ret = 0;
char buf[TCP_MSG_LEN]={0};
MSG_T *pMsg= (MSG_T*)buf;
//login firstly then send scene-cmd.
LoginIndication loginReq;
loginReq.set_timestamp(GetTimestamp());
loginReq.set_devid(devId);
loginReq.set_streamid(streamId);
string loginReq_s;
loginReq.SerializeToString(&loginReq_s);
pMsg->id = LOGIN;
strncpy(pMsg->pbData, loginReq_s.c_str(),loginReq_s.size());
pMsg->len = loginReq_s.length();
int len = 4 + 4 + pMsg->len;
//cout << "msg.id=" << pMsg->id << ",len=" << pMsg->len << endl;
try{
SocketClient s(host,port);
ret = s.SendBuf(buf, len);
LoginReply loginRes;
memset(buf, 0, sizeof(buf));
s.RecvBuf(buf, sizeof(buf));
resp_s = pMsg->pbData;
loginRes.ParseFromString(resp_s);
int64_t ts = loginRes.timestamp();
string result = loginRes.result();
cout << "recv. msg.id="<<pMsg->id<<",len="<<pMsg->len<<endl;
cout << "recv pbData. ts=" << ts << ",result=" << result.c_str() << endl;
SceneCommand sceneCmd;
sceneCmd.set_timestamp(GetTimestamp());
sceneCmd.set_streamid(streamId);
sceneCmd.set_scenename(scene);
sceneCmd.set_inputname("");
string sceneCmd_s;
sceneCmd.SerializeToString(&sceneCmd_s);
pMsg->id = SCENE;
strncpy(pMsg->pbData, sceneCmd_s.c_str(), sceneCmd_s.size());
pMsg->len = sceneCmd_s.length();
int len = 4 + 4 + pMsg->len;
ret = s.SendBuf(buf, len);
//Sleep(200);
SceneResponse sceneRes;
memset(buf, 0, sizeof(buf));
s.RecvBuf(buf, sizeof(buf));
resp_s = pMsg->pbData;
sceneRes.ParseFromString(resp_s);
ts = sceneRes.timestamp();
result = sceneRes.result();
string currStreamId = sceneRes.streamid();
string currSceneName = sceneRes.scenename();
cout << "recv. msg.id=" << pMsg->id << ",len=" << pMsg->len << endl;
cout << "recv pbData. ts=" << ts << ",result=" << result.c_str() << ",streamId="<<currStreamId<<",sceneName="<<currSceneName<<endl;
sprintf(resBuf,"ts=%lld,result=%s,streamId=%s,sceneId=%s\n",ts,result.c_str(),currStreamId.c_str(),currSceneName.c_str());
return strlen(resBuf);
}
catch(const char *s){
cerr<<s<<endl;
}
catch(std::string s){
cerr<<s<<endl;
}
catch(...){
cerr<<"unknown exception happen"<<endl;
}
return 0;
}