-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkClient.h
executable file
·75 lines (58 loc) · 1.9 KB
/
NetworkClient.h
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
/*
* File: NetworkClient.h
* Author: scribble
*
* Created on November 24, 2012, 3:51 PM
*/
#ifndef NETWORKCLIENT_H
#define NETWORKCLIENT_H
#include <deque>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/algorithm/string.hpp> //Used for boost split function
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "RequestMessage.h"
#include "Path.h"
#include "ScribbleArea.h"
#include <fstream>
using boost::asio::ip::tcp;
typedef std::deque<RequestMessage> chat_message_queue;
class NetworkClient {
public:
NetworkClient(boost::asio::io_service& io_service, tcp::resolver::iterator endpoint_iterator, ScribbleArea* scribbleArea);
void close();
void sendMessage(std::string line);
tcp::socket& getSocket();
ScribbleArea* getScribbleArea();
bool isConnected();
bool failedConnecting();
void connect(tcp::resolver::iterator endpoint_iterator);
private:
void write(const RequestMessage& msg);
void handle_connect(const boost::system::error_code& error, tcp::resolver::iterator endpoint_iterator);
void handle_read_header(const boost::system::error_code& error);
void handle_read_body(const boost::system::error_code& error);
void do_write(RequestMessage msg);
void handle_write(const boost::system::error_code& error);
void do_close();
void decodeRequest(std::string msg);
void ServerConnectionFailed();
RequestMessage encodeMessage(std::string line);
private:
boost::asio::io_service& io_service_;
tcp::socket socket_;
RequestMessage read_msg_;
chat_message_queue write_msgs_;
ScribbleArea* scribbleArea;
bool connected;
bool connectionFailed;
boost::asio::deadline_timer timer_;
enum {
FILE_WAS_CREATED = 1,
FILE_EXISTS = 2,
FILE_CREATION_FAILED = 3,
} newFileCreationStatus;
};
#endif /* NETWORKCLIENT_H */