-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpSender.h
65 lines (51 loc) · 1.35 KB
/
TcpSender.h
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
#ifndef HTTP_SENDER_H_
#define HTTP_SENDER_H_
#include <string>
#include <thread>
#include <iostream>
#include "TcpSocket.h"
#include "Windows.h"
static void WaitCommandStop(TcpSocket* _socket){
Error* err = nullptr;
while (err == nullptr){
string buffer = "";
err = _socket->Recive(sizeof("stop"), buffer);
if (buffer == "stop"){
_socket->Close();
exit(-1);
}
}
}
class TcpSender{
public:
TcpSender(std::string ip, int port){
this->_socket = new TcpSocket(ip, port);
Error* err = this->_socket->Connection();
std::thread waitThreadCommandStop(WaitCommandStop, this->_socket);
waitThreadCommandStop.detach();
if (err != nullptr){
err->Print();
}
}
TcpSender& operator<< (const std::string key) {
Error* err = this->_socket->Send(key);
if (err != nullptr){
err->Print();
while(err != nullptr){
Sleep(10000);
err = this->_socket->Connection();
}
std::thread waitThreadCommandStop(WaitCommandStop, this->_socket);
waitThreadCommandStop.detach();
}
return *this;
}
~TcpSender(){
this->_socket->Close();
}
private:
TcpSocket* _socket;
std::string _ip;
int _port;
};
#endif