WINDEX
tcp.h
1 #pragma once
2 #include <winsock2.h>
3 #include <windows.h>
4 #include "wex.h"
5 #include "ctcp.h"
6 #include "await.h"
7 
8 namespace wex
9 {
17  class tcp : public gui
18  {
19  public:
23  tcp(gui *parent) : gui(parent)
24  {
25  // Run asynchronous wait handler in its own thread
26  run();
27  }
28 
34  void RetryConnectServer(bool f)
35  {
36  myTCP.RetryConnectServer(f);
37  }
38 
43  void client(
44  const std::string &ipaddr = "127.0.0.1",
45  const std::string &port = "27678")
46  {
47  myTCP.server(ipaddr, port);
48 
49  // wait for connection to server
50  myWaiter(
51  [&]
52  {
53  myTCP.serverWait();
54  },
55  [this]
56  {
57  // check that server succesfully was connected to
58  if (!myTCP.isConnected())
59  {
60  std::cout << "wex::tcp failed connection to server" << std::endl;
61  return;
62  }
63  else
64  {
65  std::cout << "wex::tcp connected to server" << std::endl;
66  }
67 
68  // send message to parent window announcing success
69  if (!PostMessageA(
70  myParent->handle(),
71  WM_APP + 2,
72  myID,
73  0))
74  {
75  std::cout << "Post Message Error\n";
76  }
77  });
78  }
79 
91  void server(const std::string &port = "27654")
92  {
93  myTCP.server("", port);
94 
95  myWaiter(
96  [&]
97  { myTCP.acceptClient(); },
98  [this]
99  {
100  std::cout << "connected" << std::endl;
101  PostMessageA(
102  myParent->handle(),
103  WM_APP + 2,
104  myID,
105  0);
106  });
107  }
108 
110  bool isConnected()
111  {
112  return myTCP.isConnected();
113  }
114 
118  void send(const std::string &msg)
119  {
120  std::cout << "wex::tcp::send " << msg << "\n";
121  myTCP.send(msg);
122  }
123  void send(const std::vector<unsigned char> &msg)
124  {
125  myTCP.send(msg);
126  }
127 
140  void read()
141  {
142  myWaiter(
143  [this]
144  { myTCP.read(); },
145  [this]
146  {
147  // post read complete message
148  PostMessageA(
149  myParent->handle(),
150  WM_APP + 3,
151  myID,
152  0);
153  });
154  }
155 
157  std::string readMsg() const
158  {
159  return myTCP.readMsg();
160  }
161  std::string serverPort() const
162  {
163  return myTCP.serverPort();
164  }
165 
166  private:
167  std::string myRemoteAddress;
168 
169  raven::set::cTCP myTCP;
170  raven::await::cAwait myWaiter;
171 
173  void run()
174  {
175  std::thread t(
176  raven::await::cAwait::run,
177  &myWaiter);
178  t.detach();
179  }
180  };
188  class cSocket
189  {
190  public:
191  cSocket() : myWindow(maker::make()),
192  myTCP(&myWindow),
193  myConnectHandler([](std::string port) {})
194  {
195  // when a client connects, setup to read anything sent
196  myWindow.events()
197  .tcpServerAccept(
198  [this]
199  {
200  myConnectHandler(myPort);
201  myTCP.read();
202  });
203 
204  // handle data received on input
205  myWindow.events().tcpRead(
206  [this]
207  {
208  // check for connection closed
209  if (!myTCP.isConnected())
210  {
211  if (myIpaddr.empty())
212  {
213  // client disconnect
214  std::cout << "Input Connection closed, waiting for new client\n";
215 
216  server(
217  myPort,
218  myConnectHandler,
219  myReadHandler);
220  }
221  else
222  {
223  // server disconnected
224  std::cout << "server disconnected\n";
225  }
226  return;
227  }
228 
229  myReadHandler(
230  myPort,
231  myTCP.readMsg());
232 
233  // setup for next message
234  myTCP.read();
235  });
236  }
242  void server(
243  const std::string &port,
244  std::function<void(std::string & port)> connectHandler,
245  std::function<void(std::string & port, const std::string &msg)> readHandler)
246  {
247  myPort = port;
248  myIpaddr = "";
249  myConnectHandler = connectHandler;
250  myReadHandler = readHandler;
251  myTCP.server(port);
252  }
253 
259  void RetryConnectServer(bool f)
260  {
261  myTCP.RetryConnectServer(f);
262  }
263 
269  void client(
270  const std::string &ipaddr,
271  const std::string &port,
272  std::function<void(std::string & port, const std::string &msg)> readHandler)
273  {
274  myIpaddr = ipaddr;
275  myPort = port;
276  myReadHandler = readHandler;
277 
278  myTCP.client(ipaddr, port);
279 
280  }
281 
282  bool isConnected()
283  {
284  return myTCP.isConnected();
285  }
286 
288  void send(const std::string &msg)
289  {
290  myTCP.send(msg);
291  }
292 
303  void run()
304  {
305  myWindow.run();
306  }
307 
308  private:
309  gui &myWindow;
310  tcp myTCP;
311  std::function<void(std::string & port)> myConnectHandler;
312  std::function<void(std::string & port, const std::string &msg)> myReadHandler;
313  std::string myPort;
314  std::string myIpaddr;
315  };
316 
317 }
Read/Write to TCP/IP socket, client or server.
Definition: tcp.h:189
void send(const std::string &msg)
Send message to connected peer.
Definition: tcp.h:288
void RetryConnectServer(bool f)
Configure client() blocking.
Definition: tcp.h:259
void server(const std::string &port, std::function< void(std::string &port)> connectHandler, std::function< void(std::string &port, const std::string &msg)> readHandler)
Start server.
Definition: tcp.h:242
void client(const std::string &ipaddr, const std::string &port, std::function< void(std::string &port, const std::string &msg)> readHandler)
Connect to server.
Definition: tcp.h:269
void run()
Start the windex event handler.
Definition: tcp.h:303
void tcpRead(std::function< void(void)> f)
register function to call when tcp read accurs
Definition: wex.h:413
The base class for all windex gui elements.
Definition: wex.h:824
eventhandler & events()
Get event handler.
Definition: wex.h:1649
HWND handle()
get window handle
Definition: wex.h:1655
void run()
Run the windows message loop.
Definition: wex.h:1198
static gui & make()
Construct a top level window ( first call constructs application window )
Definition: wex.h:3252
Read/Write to TCP/IP socket, client or server.
Definition: tcp.h:18
void client(const std::string &ipaddr="127.0.0.1", const std::string &port="27678")
Create client socket connected to server.
Definition: tcp.h:43
bool isConnected()
true if valid connection
Definition: tcp.h:110
void send(const std::string &msg)
send message to peer
Definition: tcp.h:118
void server(const std::string &port="27654")
Create server socket waiting for connection requests.
Definition: tcp.h:91
void read()
asynchronous read message on tcp connection
Definition: tcp.h:140
std::string readMsg() const
Get last message from peer.
Definition: tcp.h:157
tcp(gui *parent)
CTOR.
Definition: tcp.h:23
void RetryConnectServer(bool f)
Configure client() blocking.
Definition: tcp.h:34