-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathStream.cc
More file actions
144 lines (125 loc) · 3.21 KB
/
Stream.cc
File metadata and controls
144 lines (125 loc) · 3.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
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
#include <elle/log.hh>
#include <elle/protocol/Stream.hh>
#include <elle/protocol/Serializer.hh>
#include <elle/protocol/exceptions.hh>
#include <elle/reactor/scheduler.hh>
#ifdef ELLE_WINDOWS
# include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <elle/serialization/binary.hh>
ELLE_LOG_COMPONENT("elle.protocol.Stream");
namespace elle
{
namespace protocol
{
using namespace elle::serialization::binary;
/*-------------.
| Construction |
`-------------*/
Stream::Stream(elle::reactor::Scheduler& scheduler)
: _scheduler(scheduler)
{}
Stream::Stream()
: Stream(*elle::reactor::Scheduler::scheduler())
{}
Stream::~Stream()
{}
/*----------.
| Receiving |
`----------*/
elle::Buffer
Stream::read()
{
ELLE_TRACE_SCOPE("%s: read packet", this);
return this->_read();
}
/*--------.
| Sending |
`--------*/
void
Stream::write(elle::Buffer const& packet)
{
ELLE_TRACE_SCOPE("%s: write packet (%s bytes)", this, packet.size());
this->_write(packet);
}
/*------------------.
| Int serialization |
`------------------*/
void
Stream::uint32_put(elle::Buffer& b, uint32_t i, elle::Version const& v)
{
if (v >= elle::Version(0, 3, 0))
{
elle::IOStream output(b.ostreambuf());
SerializerOut::serialize_number(output, i);
}
else
{
i = htonl(i);
b.append(&i, 4);
}
}
uint32_t
Stream::uint32_get(elle::Buffer& b, elle::Version const& v)
{
if (v >= elle::Version(0, 3, 0))
{
elle::IOStream input(b.istreambuf());
int64_t res;
b.pop_front(SerializerIn::serialize_number(input, res));
return (uint32_t) res;
}
else
{
uint32_t i;
ELLE_ASSERT_GTE((signed)b.size(), 4);
i = *(uint32_t*)b.contents();
b.pop_front(4);
return ntohl(i);
}
}
void
Stream::uint32_put(std::ostream& s, uint32_t i, elle::Version const& v)
{
if (v >= elle::Version(0, 3, 0))
{
SerializerOut::serialize_number(s, i);
}
else
{
elle::IOStreamClear clearer(s);
// FIXME: should rethrow the underlying streambuf error.
if (!s.good())
throw elle::Exception("stream is not good");
i = htonl(i);
s.write(reinterpret_cast<char*>(&i), sizeof(i));
}
}
uint32_t
Stream::uint32_get(std::istream& s, elle::Version const& v)
{
if (v >= elle::Version(0, 3, 0))
{
int64_t res = 0;
SerializerIn::serialize_number(s, res);
if (res < 0 || std::numeric_limits<uint32_t>::max() < res)
throw Error(elle::sprintf("unexpected uint32_t: %s", res));
return (uint32_t) res;
}
else
{
uint32_t res = 0;
elle::IOStreamClear clearer(s);
// FIXME: should rethrow the underlying streambuf error.
if (!s.good())
throw elle::Exception("stream is not good");
s.read(reinterpret_cast<char*>(&res), sizeof(res));
if (s.gcount() != signed(sizeof(res)))
throw Serializer::EOS();
return ntohl(res);
}
}
}
}