1 /*
2 * @file TCPClient.h
3 * @brief wrapper for TCP client
4 *
5 * @author Mohamed Amine Mzoughi <mohamed-amine.mzoughi@laposte.net>
6 * @date 2013-05-11
7 */
8 
9 #ifndef INCLUDE_TCPCLIENT_H_
10 #define INCLUDE_TCPCLIENT_H_
11 
12 #include <algorithm>
13 #include <cstddef>   // size_t
14 #include <cstdlib>
15 #include <cstring>   // strerror, strlen, memcpy, strcpy
16 #include <ctime>
17 #include <iostream>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string>
21 #include <vector>
22 
23 #include "Socket.h"
24 
25 class CTCPSSLClient;
26 
27 class CTCPClient : public ASocket
28 {
29    friend class CTCPSSLClient;
30 
31 public:
32    explicit CTCPClient(const LogFnCallback oLogger, const SettingsFlag eSettings = ALL_FLAGS);
33    ~CTCPClient() override;
34 
35    // copy constructor and assignment operator are disabled
36    CTCPClient(const CTCPClient&) = delete;
37    CTCPClient& operator=(const CTCPClient&) = delete;
38 
39    // Setters - Getters (for unit tests)
40    /*inline*/// void SetProgressFnCallback(void* pOwner, const ProgressFnCallback& fnCallback);
41    /*inline*/// void SetProxy(const std::string& strProxy);
42    /*inline auto GetProgressFnCallback() const
43    {
44       return m_fnProgressCallback.target<int(*)(void*, double, double, double, double)>();
45    }
46    inline void* GetProgressFnCallbackOwner() const { return m_ProgressStruct.pOwner; }*/
47    //inline const std::string& GetProxy() const { return m_strProxy; }
48    //inline const unsigned char GetSettingsFlags() const { return m_eSettingsFlags; }
49 
50 	// Session
51    bool Connect(const std::string& strServer, const std::string& strPort); // connect to a TCP server
52    bool Disconnect(); // disconnect from the TCP server
53    bool Send(const char* pData, const size_t uSize) const; // send data to a TCP server
54    bool Send(const std::string& strData) const;
55    bool Send(const std::vector<char>& Data) const;
56    int  Receive(char* pData, const size_t uSize, bool bReadFully = true) const;
57 
58    // To disable timeout, set msec_timeout to 0.
59    bool SetRcvTimeout(unsigned int msec_timeout);
60    bool SetSndTimeout(unsigned int msec_timeout);
61 
62 #ifndef WINDOWS
63    bool SetRcvTimeout(struct timeval Timeout);
64    bool SetSndTimeout(struct timeval Timeout);
65 #endif
66 
IsConnected()67    bool IsConnected() const { return m_eStatus == CONNECTED; }
68 
GetSocketDescriptor()69    Socket GetSocketDescriptor() const { return m_ConnectSocket; }
70 
71 protected:
72    enum SocketStatus
73    {
74       CONNECTED,
75       DISCONNECTED
76    };
77 
78    SocketStatus m_eStatus;
79    Socket m_ConnectSocket; // ConnectSocket
80    //unsigned m_uRetryCount;
81    //unsigned m_uRetryPeriod;
82 
83    struct addrinfo* m_pResultAddrInfo;
84    struct addrinfo  m_HintsAddrInfo;
85 };
86 
87 #endif
88