-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_impl.cpp
More file actions
103 lines (80 loc) · 2.21 KB
/
task_impl.cpp
File metadata and controls
103 lines (80 loc) · 2.21 KB
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
/*
* File: task_impl.cpp
* Author: georgez
*
* Created on 23 October 2014, 12:57
*/
#include "task_impl.h"
task_impl::task_impl() {
//std::cout << "Task Impl Created" << std::endl;
}
task_impl::~task_impl() {
//std::cout << "Task Impl Destroyed" << std::endl;
}
void task_impl::clean(){
//std::cout << "Task Impl Cleaned" << std::endl;
std::memset(_data,0,max_length);
length_=0;
}
void task_impl::clean(std::shared_ptr<session> ps){
_pSession=ps;
length_=0;
//std::cout << "Task Cleaned" << std::endl;
}
char* task_impl::getBuffer(){
return ((PACKETHEAD_PTR)_data)->buffer;
}
size_t task_impl::getBufferSize(){
return max_length;
}
int task_impl::getLength(){
return length_-sizeof(PACKETHEAD);
}
bool task_impl::writeBack(const char* data,size_t length){
auto pHeader=getHeader();
pHeader->resp=1;
pHeader->datasize=length;
boost:asio:write(*getSocket(),boost::asio::buffer(_data,length+sizeof(PACKETHEAD)));
}
tcp::socket* task_impl::getSocket(){
if(auto ps=_pSession.lock()){
return ps->getSocket();
}
return nullptr;
}
void task_impl::setLength(int l){
length_=l;
_data[l]=0;
}
PACKETHEAD_PTR task_impl::getHeader(){
return (PACKETHEAD_PTR)&_data;
}
bool task_impl::preProcess(){
return false; // indicates further processing, true indicates task complete
}
std::shared_ptr<session> task_impl::getSession(){
return _pSession.lock();
}
// This method sets up the header for command
void task_impl::setHeader(int type,int command,uint length){
switch(type){
case IAS_PACKETTYPE_REQUEST:
setRequestHeader(command,length);
break;
case IAS_PACKETTYPE_RESPONSE:
setResponseHeader(command,length);
break;
}
}
void task_impl::setResponseHeader(int command,uint length){
PACKETHEAD_PTR pHead=getHeader();
pHead->ais=(uint8_t)command;
pHead->datasize=(uint16_t)length;
pHead->type=(uint16_t)command;
}
void task_impl::setRequestHeader(int command,uint length){
PACKETHEAD_PTR pHead=getHeader();
pHead->ais=(uint8_t)command;
pHead->datasize=(uint16_t)length;
pHead->type=(uint16_t)command;
}