Tcp.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <etk/types.hpp>
9 #include <mutex>
10 #ifdef __TARGET_OS__Windows
11  #include <winsock2.h>
12  #include <ws2tcpip.h>
13 #endif
14 
15 namespace enet {
16  class Tcp {
17  private:
18  #ifdef __TARGET_OS__Windows
19  SOCKET m_socketId;
20  #else
21  int32_t m_socketId;
22  #endif
23  std::mutex m_mutex;
24  public:
25  Tcp();
26  #ifdef __TARGET_OS__Windows
27  Tcp(SOCKET _idSocket, const std::string& _name);
28  #else
29  Tcp(int32_t _idSocket, const std::string& _name);
30  #endif
31  // move constructor
32  Tcp(Tcp&& _obj);
33  // Move operator;
34  Tcp& operator= (Tcp&& _obj);
35  // Remove copy operator ... ==> not valid ...
36  Tcp& operator= (Tcp& _obj) = delete;
37  virtual ~Tcp();
38  private:
39  std::string m_name;
40  public:
45  const std::string& getName() {
46  return m_name;
47  }
48  public:
49  enum class status {
50  unlink,
51  link,
52  linkRemoteClose,
53  error
54  };
55  private:
56  enum status m_status;
57  public:
62  enum status getConnectionStatus() const {
63  return m_status;
64  }
65  public:
71  bool unlink();
79  int32_t read(void* _data, int32_t _maxLen);
87  int32_t write(const void* _data, int32_t _len);
95  int32_t write(const std::string& _data, bool _writeBackSlashZero = true) {
96  if (_data.size() == 0) {
97  return 0;
98  }
99  if (_writeBackSlashZero == true) {
100  return write(_data.c_str(), _data.size()+1);
101  }
102  return write(_data.c_str(), _data.size());
103  }
111  template <class T>
112  int32_t write(const std::vector<T>& _data) {
113  if (_data.size() == 0) {
114  return 0;
115  }
116  size_t ret = write(&_data[0], _data.size()*sizeof(T));
117  if (ret <=0) {
118  return ret;
119  }
120  return ret/sizeof(T);
121  }
122 
123  bool setTCPNoDelay(bool _enabled);
124  };
125 }
126 
const std::string & getName()
Get the decriptive name hot the host:port.
Definition: Tcp.hpp:45
int32_t write(const std::vector< T > &_data)
Write a chunk of data on the socket.
Definition: Tcp.hpp:112
enum status getConnectionStatus() const
Get the current Status of the connection.
Definition: Tcp.hpp:62
int32_t write(const std::string &_data, bool _writeBackSlashZero=true)
Write a chunk of data on the socket.
Definition: Tcp.hpp:95
int32_t write(const void *_data, int32_t _len)
Write a chunk of data on the socket.
int32_t read(void *_data, int32_t _maxLen)
Read a chunk of data on the socket.
Definition: Tcp.hpp:16
Main esvg namespace.
Definition: enet.hpp:17