summaryrefslogtreecommitdiff
path: root/server/session.cpp
blob: f86e7f1179bd2b45bfc4a977ae16ff2d7c604783 (plain)
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
// Copyright © 2013 sss
//.
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


#include "headers.h"

using namespace proto;
using namespace service;

void session::handle_handshake(const boost::system::error_code& error)
{
	if (!error)
	{
		recv_data = new char [32];
		socket_.async_read_some(boost::asio::buffer(recv_data, 32), boost::bind(&session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
	}
	else
	{
		delete this;
	}
}

void session::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
	if (!error)
	{
//		std::cout<<"recieved: "<<recv_data<<"\n";
		std::vector<unsigned char> v;
		v.assign(recv_data, recv_data + bytes_transferred);
		delete [] recv_data;
		packet p(v);
		v.clear();
		if(!handle_data(this, p))
			delete this;
	}
}

void session::handle_write(const boost::system::error_code& error, size_t bytes_transferred)
{
	delete [] snd_data;
	if(error)
	{
		
		delete this;
		return; //?
	}
	recv_data = (char*)new unsigned char[128];
	socket_.async_read_some(boost::asio::buffer(recv_data, 128), boost::bind(&session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}

void session::write_w_close(const std::vector<unsigned char>& data)
{
	snd_data = (char*)new unsigned char[data.size() + 1];
	memset(snd_data, 0, data.size() + 1);
	for(int i = 0; i < data.size(); i++)
		snd_data[i] = data[i];
	socket_.async_write_some(boost::asio::buffer(snd_data, data.size() + 1), boost::bind(&session::handle_write_close, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}

void session::write_w_response(const std::vector<unsigned char>& data)
{
	pack_buffer(data);
	socket_.async_write_some(boost::asio::buffer(snd_data, data.size()+1), boost::bind(&session::handle_write, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}

void session::write_wo_response(const std::vector<unsigned char>& data)
{
	pack_buffer(data);
	socket_.async_write_some(boost::asio::buffer(snd_data, data.size() +1), boost::bind(&session::handle_operation, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}


void session::handle_write_close(const boost::system::error_code& error, size_t bytes_transferred)
{
	delete [] snd_data;
	delete this;
}

void session::handle_operation(const boost::system::error_code& error, size_t bytes_transferred)
{
	delete [] snd_data;
}

void session::pack_buffer(const std::vector<unsigned char>& data)
{
	snd_data = (char*)new unsigned char[data.size()+1];
	memset(snd_data, 0, data.size()+1);
	for(int i = 0; i < data.size(); i++)
		snd_data[i] = data[i];
}