-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcl_buffer.cpp
More file actions
executable file
·119 lines (104 loc) · 2.8 KB
/
Copy pathcl_buffer.cpp
File metadata and controls
executable file
·119 lines (104 loc) · 2.8 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2013, Jeffery Qiu. All rights reserved.
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/lgpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//// Author: Jeffery Qiu (dungeonsnd at gmail dot com)
////
#include "netserver/cl_buffer.hpp"
namespace cl
{
cf_void ReadBuffer::Clear()
{
CF_PRINT_FUNC;
_buf.clear();
_total =0;
_already =0;
}
cf_int ReadBuffer::Read(cf::T_SESSION session, bool & peerClosedWhenRead)
{
CF_PRINT_FUNC;
cf_char * p =&_buf[0];
cf_int rdn =session->RecvAsync(p+_already, GetLeft(),peerClosedWhenRead);
#if CF_SWITCH_PRINT
std::string s1 =cf::String2Hex(p+_already,rdn);
fprintf (stdout, "<<<< Read ,fd=%d,RecvAsync=%s \n", session->Fd(), s1.c_str());
#endif
_already +=rdn;
return rdn;
}
cf_void ReadBuffer::SetTotal(cf_uint32 total)
{
if(_total)
_THROW_FMT(cf::ValueError, "_total{%u}!=0 !", _total);
if(total>_total)
_buf.resize(total);
_total =total;
}
cf_uint32 ReadBuffer::GetLeft() cf_const
{
if(_total>_already)
return _total-_already;
else
_THROW_FMT(cf::ValueError, "_total{%u}<=_already{%u} !", _total,_already);
}
bool ReadBuffer::IsComplete() cf_const
{
if(0==_total)
_THROW(cf::ValueError, "0==_total !");
return _total==_already;
}
cf_void WriteBuffer::Clear()
{
CF_PRINT_FUNC;
_buf.clear();
_total =0;
_already =0;
}
cf_int WriteBuffer::Write(cf::T_SESSION session)
{
CF_PRINT_FUNC;
cf_char * p =&_buf[0];
cf_int rdn =session->SendAsync(p+_already, GetLeft());
#if CF_SWITCH_PRINT
std::string s1 =cf::String2Hex(p+_already,rdn);
fprintf (stdout, ">>>> Write ,fd=%d,SendAsync=%s \n", session->Fd(),
s1.c_str());
#endif
_already +=rdn;
return rdn;
}
cf_void WriteBuffer::SetBuffer(cf_cpvoid buffer, cf_uint32 total)
{
CF_PRINT_FUNC;
if(_total)
_THROW_FMT(cf::ValueError, "_total{%u}!=0 !", _total);
if(total>_total)
_buf.resize(total);
memcpy(&_buf[0],buffer,total);
_total =total;
}
cf_uint32 WriteBuffer::GetLeft() cf_const
{
if(_total>_already)
return _total-_already;
else
_THROW_FMT(cf::ValueError, "_total{%u}<=_already{%u} !", _total,_already);
}
bool WriteBuffer::IsComplete() cf_const
{
if(0==_buf.size())
_THROW(cf::ValueError, "0==_total !");
return _total==_already;
}
} // namespace cl