forked from embeddedmz/socket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPClient.h
More file actions
95 lines (77 loc) · 2.74 KB
/
TCPClient.h
File metadata and controls
95 lines (77 loc) · 2.74 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
/*
* @file TCPClient.h
* @brief wrapper for TCP client
*
* @author Mohamed Amine Mzoughi <[email protected]>
* @date 2013-05-11
*/
#ifndef INCLUDE_TCPCLIENT_H_
#define INCLUDE_TCPCLIENT_H_
#include <algorithm>
#include <chrono>
#include <cstddef> // size_t
#include <cstdlib>
#include <cstring> // strerror, strlen, memcpy, strcpy
#include <ctime>
#include <iostream>
#include <random>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include "Socket.h"
class CTCPSSLClient;
class CTCPClient : public ASocket
{
friend class CTCPSSLClient;
public:
explicit CTCPClient(const LogFnCallback oLogger, const SettingsFlag eSettings = ALL_FLAGS);
~CTCPClient() override;
// copy constructor and assignment operator are disabled
CTCPClient(const CTCPClient&) = delete;
CTCPClient& operator=(const CTCPClient&) = delete;
// Setters - Getters (for unit tests)
/*inline*/// void SetProgressFnCallback(void* pOwner, const ProgressFnCallback& fnCallback);
/*inline*/// void SetProxy(const std::string& strProxy);
/*inline auto GetProgressFnCallback() const
{
return m_fnProgressCallback.target<int(*)(void*, double, double, double, double)>();
}
inline void* GetProgressFnCallbackOwner() const { return m_ProgressStruct.pOwner; }*/
//inline const std::string& GetProxy() const { return m_strProxy; }
//inline const unsigned char GetSettingsFlags() const { return m_eSettingsFlags; }
// Session
bool Connect(const std::string& strServer, const std::string& strPort); // connect to a TCP server
bool Disconnect(); // disconnect from the TCP server
bool Send(const char* pData, const size_t uSize) const; // send data to a TCP server
bool Send(const std::string& strData) const;
bool Send(const std::vector<char>& Data) const;
int Receive(char* pData, const size_t uSize, bool bReadFully = true) const;
// To disable timeout, set msec_timeout to 0.
bool SetRcvTimeout(unsigned int msec_timeout);
bool SetSndTimeout(unsigned int msec_timeout);
#ifndef WINDOWS
bool SetRcvTimeout(struct timeval Timeout);
bool SetSndTimeout(struct timeval Timeout);
#endif
bool IsConnected() const { return m_eStatus == CONNECTED; }
Socket GetSocketDescriptor() const { return m_ConnectSocket; }
std::string GetLastAddress() const { return m_strLastAddress; }
protected:
enum SocketStatus
{
CONNECTED,
DISCONNECTED
};
SocketStatus m_eStatus;
Socket m_ConnectSocket; // ConnectSocket
//unsigned m_uRetryCount;
//unsigned m_uRetryPeriod;
struct addrinfo* m_pResultAddrInfo;
struct addrinfo m_HintsAddrInfo;
std::random_device m_RandDevice{};
std::default_random_engine m_Rng;
std::string m_strLastAddress;
};
#endif