This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.h
110 lines (76 loc) · 2.59 KB
/
session.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef SESSION_H_INCLUDED
#define SESSION_H_INCLUDED
#ifndef Session_H_INCLUDED
#define Session_H_INCLUDED
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/array.hpp>
#include <string>
#include <iostream>
#include "defines.h"
#include "user.h"
using boost::asio::ip::tcp;
/**
A Session handle the communication with the Client via its socket.
So it can read incoming data or send a message
*/
class Session : public boost::enable_shared_from_this<Session>
{
public:
/**
A typedef to make our life simplest
*/
typedef boost::shared_ptr<Session> pointer;
/**
Creates a Session and returns a shared_pointer on it
*/
static pointer create(boost::asio::io_service& io_service);
/**
Starts the Session, starts an async_read by calling read()
*/
void start();
/**
Send a message to the client (async_write) and call handleWrite. Put the server header in the message.
*/
void sendAsServer(const std::string& message);
/**
Send a message to the client (async_write) and call handleWrite. Put the USer header in the message.
*/
void sendAsUser(const std::string& message);
/**
Send a message to the client (async_write) and call handleWrite. No header will be added to the message.
*/
void send(const std::string& message);
/**
Stops the Session, close the socket
*/
void close();
/**
Returns the Session Socket
*/
tcp::socket& socket();
/**
Returns the Session IP in a std::string
*/
std::string ip() const;
private:
/**
Session constructor, initialize the Session socket
*/
Session(boost::asio::io_service& io_service);
/**
Starts an async_read_until that will read a message and call handleRead
*/
void read();
/**
Handle the data that has been read
*/
void handleRead(const boost::system::error_code& error, std::size_t bytes);
User mUser; ///< User that is linked to this connection
tcp::socket mSocket; ///< TCP Socket
boost::asio::streambuf mBuffer; ///< Buffer used to read incoming data
std::string mMessage; ///< A string buffer used to write (send) data to client
};
#endif // Session_H_INCLUDED
#endif // SESSION_H_INCLUDED