-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
146 lines (122 loc) · 3.98 KB
/
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
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
#include <windows.h>
#include <iostream>
#include <thread>
#include <stdlib.h>
using namespace std;
#define RETRY_LIMIT 5
#define BUFF_SIZE 1023
#include "util.h"
#include "custom-named-pipe-client.h"
bool isRunning = true;
void WriteHandler(CustomNamedPipeClient *clientRead, LPTSTR readPipeName, CustomNamedPipeClient *clientWrite, LPTSTR writePipeName)
{
int response;
while (isRunning)
{
char message[BUFF_SIZE];
cin.getline(message, BUFF_SIZE);
cin.clear();
cin.ignore(INT_MAX, '\n');
if (!isRunning)
return;
response = clientWrite->WriteToPipe(message);
if (response == ERROR_BROKEN_PIPE || response == ERROR_PIPE_NOT_CONNECTED)
{
ShowMessage("Error , Trying to Reconnect...", RED);
BOOL connection = clientRead->OpenServerConnection(readPipeName, GENERIC_READ);
Sleep(100);
if (connection != FALSE)
clientWrite->OpenServerConnection(writePipeName, GENERIC_WRITE);
if (connection == FALSE)
{
ShowMessage("Reconnection Failed. Closing Client\n", GREEN);
isRunning = false;
// using exit to force quit the blocking cin
delete clientWrite;
delete clientRead;
exit(0);
}
continue;
}
else if (response == 1)
{
cout << "Shutting Down...\n";
isRunning = false;
break;
}
else
{
ShowMessage("Message Sent.\n", GREEN);
}
}
}
void ReadHandler(CustomNamedPipeClient *clientRead, LPTSTR readPipeName, CustomNamedPipeClient *clientWrite, LPTSTR writePipeName)
{
int response;
while (isRunning)
{
response = clientRead->ReadFromPipe();
if (!isRunning)
return;
if (response == ERROR_BROKEN_PIPE || response == ERROR_PIPE_NOT_CONNECTED)
{
cout << "Error in accessing server!\nTrying to reconnect...\n";
BOOL connection;
connection = clientRead->OpenServerConnection(readPipeName, GENERIC_READ);
Sleep(100);
if (connection != FALSE)
clientWrite->OpenServerConnection(writePipeName, GENERIC_WRITE);
if (connection == FALSE)
{
ShowMessage("Reconnection Failed. Closing Client\n", GREEN);
isRunning = false;
// using exit to force quit the blocking cin
delete clientWrite;
delete clientRead;
exit(0);
}
}
else if (response == 1)
{
ShowMessage("Server initiated Shutdown.\n", GREEN);
isRunning = false;
// using exit to force quit the blocking cin
delete clientWrite;
delete clientRead;
exit(0);
}
else
{
ShowMessage("Message Received.\n", GREEN);
}
}
}
int main()
{
system("CLS");
cout << "---- CLIENT ----\n\n";
CustomNamedPipeClient *clientRead = new CustomNamedPipeClient();
CustomNamedPipeClient *clientWrite = new CustomNamedPipeClient();
LPTSTR writePipeName = TEXT("\\\\.\\pipe\\mypipe1");
LPTSTR readPipeName = TEXT("\\\\.\\pipe\\mypipe2");
BOOL connection;
connection = clientRead->OpenServerConnection(readPipeName, GENERIC_READ);
Sleep(100);
if (connection != FALSE)
clientWrite->OpenServerConnection(writePipeName, GENERIC_WRITE);
if (connection != FALSE)
{
thread readThread(ReadHandler, clientRead, readPipeName, clientWrite, writePipeName);
thread writeThread(WriteHandler, clientRead, readPipeName, clientWrite, writePipeName);
readThread.join();
writeThread.join();
delete clientWrite;
delete clientRead;
cout << "\nClient Ended\n";
}
else
{
cout << "Client ended due to Timeout!\n";
}
return 0;
}