-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4618_Client.cpp
100 lines (88 loc) · 2.08 KB
/
4618_Client.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
////////////////////////////////////////////////////////////////
// ELEX 4618 Client Template project for BCIT
// Created Oct 5, 2016 by Craig Hennessey
// Last updated April 2022
////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <string>
#include <iostream>
#include <thread>
#include "Client.h"
std::string server_ip = "127.0.0.1";
int server_port = 4618;
float timeout_start;
void print_menu()
{
std::cout << "\n***********************************";
std::cout << "\n* ELEX4618 Client Project";
std::cout << "\n***********************************";
std::cout << "\n(1) Send image command";
std::cout << "\n(2) Send other command";
std::cout << "\n(0) Exit";
std::cout << "\nCMD> ";
}
void send_command(CClient &client, std::string cmd)
{
std::string str;
client.tx_str(cmd);
std::cout << "\nClient Tx: " << cmd;
if (cmd == "im")
{
cv::Mat im;
if (client.rx_im(im) == true)
{
timeout_start = cv::getTickCount();
if (im.empty() == false)
{
std::cout << "\nClient Rx: Image received";
cv::imshow("rx", im);
cv::waitKey(10);
}
}
else
{
if ((cv::getTickCount() - timeout_start) / cv::getTickFrequency() > 1000)
{
// No response, disconnect and reconnect
timeout_start = cv::getTickCount();
client.close_socket();
client.connect_socket(server_ip, server_port);
}
}
}
else
{
if (client.rx_str(str) == true)
{
timeout_start = cv::getTickCount();
std::cout << "\nClient Rx: " << str;
}
else
{
if ((cv::getTickCount() - timeout_start) / cv::getTickFrequency() > 1000)
{
// No response, disconnect and reconnect
timeout_start = cv::getTickCount();
client.close_socket();
client.connect_socket(server_ip, server_port);
}
}
}
}
int main(int argc, char* argv[])
{
CClient client;
int cmd = -1;
timeout_start = cv::getTickCount();
client.connect_socket(server_ip, server_port);
do
{
print_menu();
std::cin >> cmd;
switch (cmd)
{
case 1: send_command(client, "im"); break;
case 2: send_command(client, "cmd2"); break;
}
} while (cmd != 0);
}