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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef _CONNECTION_H_
#define _CONNECTION_H_

#include "ByteBuffer.h"
#include <ctime>
#include <iostream>
#include <string>
#include <stdint.h>
#include <boost/bind.hpp>
#include <memory>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

class Connection: public boost::enable_shared_from_this<Connection>
{
public:
  typedef std::shared_ptr<Connection> pointer;
  
  explicit Connection(boost::asio::io_service& io_service);
  virtual ~Connection();
  boost::asio::ip::tcp::socket& socket();

  virtual void OnConnected()=0;
  virtual void Send(std::shared_ptr<uint8_t> buffer, int length);
  void start_read();
  void Disconnect();

protected:
  virtual void OnReceived(ByteBuffer &b) = 0;

private:
  void handle_read_some(const boost::system::error_code& error, std::size_t bytes_transferred );

  boost::asio::ip::tcp::socket socket_;
  bool disconnecting;
  boost::array<int8_t, 4096> read_buffer_;
};


#endif



#include "Connection.h"
using namespace std;

Connection::Connection(boost::asio::io_service& io_service)
    : socket_(io_service),disconnecting(false)
{
    
}

Connection::~Connection()
{
    //dtor
}


boost::asio::ip::tcp::socket& Connection::socket(){
    return socket_;
}

void Connection::Send(std::shared_ptr<uint8_t> buf, int length){

  boost::asio::async_write(socket_,boost::asio::buffer(buf.get(),length),
            boost::bind(&Connection::handler, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
        
void Connection::Disconnect()
{
  if (!disconnecting) {
    boost::system::error_code ec;
    socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send,ec);
    socket_.close(ec);
    disconnecting = true;
  }
}

void Connection::start_read(){
  socket_->async_read_some(boost::asio::buffer(read_buffer_),
    boost::bind(&Connection::handle_read_some, shared_from_this(),
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

void Connection::handle_read_some(const boost::system::error_code& error, std::size_t bytes_transferred)
{
  if(!error)
  {
    ByteBuffer b(reinterpret_cast<uint8_t*>(read_buffer_.begin()), bytes_transferred);
    OnReceived(b);

    start_read();
  }
}

#ifndef _SERVER_H_
#define _SERVER_H_

#include "Connection.h"
#include "ConnectionFactory.h"

class Server
{
public:
  Server(boost::asio::io_service& io_service , std::string ip,short port,std::shared_ptr<ConnectionFactory> factory);
  ~Server();  
private:
  void start_accept();
  void handle_accept(std::shared_ptr<Connection> conn,const boost::system::error_code& error);

  std::shared_ptr<ConnectionFactory> m_factory;
  boost::asio::io_service io_service;
  boost::asio::ip::tcp::acceptor acceptor_;
};

#endif

#include "Server.h"


Server::Server(boost::asio::io_service& io_service,std::string ip,short port,std::shared_ptr<ConnectionFactory> factory)
  : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(ip.data()), port)){

    m_factory = factory;
    start_accept();

    std::cout<<"Socket accepting connections..."<<std::endl;
}

Server::~Server()
{
}


void Server::start_accept(){
  
  std::shared_ptr<Connection> conn = m_factory->create(this->io_service);

  acceptor_.async_accept(conn->socket(),
    boost::bind(&Server::handle_accept, this,conn,
          boost::asio::placeholders::error));
  
}


void Server::handle_accept(std::shared_ptr<Connection> conn,const boost::system::error_code& error){

    if (!error)
    {
    std::cout<<"on connected"<<std::endl;
    conn->OnConnected();
    conn->start_read();
    start_accept();
    }
  else{
    conn->Disconnect();
  }
  }