1 #pragma once
2 
3 #include <string>
4 #include <memory>
5 #include <asio/ts/buffer.hpp>
6 #include <asio/ts/internet.hpp>
7 #include <asio/ts/io_context.hpp>
8 #include <asio/ts/net.hpp>
9 
10 #include "dvlnet/packet.h"
11 #include "dvlnet/frame_queue.h"
12 #include "dvlnet/base.h"
13 #include "dvlnet/tcp_server.h"
14 
15 namespace dvl {
16 namespace net {
17 
18 class tcp_client : public base {
19 public:
20 	int create(std::string addrstr, std::string passwd);
21 	int join(std::string addrstr, std::string passwd);
22 
23 	virtual void poll();
24 	virtual void send(packet &pkt);
25 
26 	virtual bool SNetLeaveGame(int type);
27 
28 	virtual ~tcp_client();
29 
30 private:
31 	frame_queue recv_queue;
32 	buffer_t recv_buffer = buffer_t(frame_queue::max_frame_size);
33 
34 	asio::io_context ioc;
35 	asio::ip::tcp::resolver resolver = asio::ip::tcp::resolver(ioc);
36 	asio::ip::tcp::socket sock = asio::ip::tcp::socket(ioc);
37 	std::unique_ptr<tcp_server> local_server; // must be declared *after* ioc
38 
39 	void handle_recv(const asio::error_code &error, size_t bytes_read);
40 	void start_recv();
41 	void handle_send(const asio::error_code &error, size_t bytes_sent);
42 };
43 
44 } // namespace net
45 } // namespace dvl
46