/
send.cc
160 lines (136 loc) · 4.18 KB
/
send.cc
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
// Copyright (c) 2010 The Trustees of Princeton University (Trustees)
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and/or hardware specification (the “Work”) to deal
// in the Work without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Work, and to permit persons to whom the Work is
// furnished to do so, subject to the following conditions: The above
// copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Work.
// THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER
// DEALINGS IN THE WORK.
#include "send.hh"
//
// SendReq
//
SendReq::SendReq()
:Message(SEND_REQ), _nb(false), _ipaddr(0),
_nsbuf(0), _nonserial_len(0), _flags(0)
{
memset(&_dst_obj_id, 0xff, sizeof(_dst_obj_id));
set_pld_len_v(serial_pld_len() + nonserial_pld_len());
}
SendReq::SendReq(bool nb,
unsigned char *buf, uint16_t buflen, int flags)
:Message(SEND_REQ), _nb(nb), _ipaddr(0),
_nsbuf(buf), _nonserial_len(buflen), _flags(flags)
{
memset(&_dst_obj_id, 0xff, sizeof(_dst_obj_id));
set_pld_len_v(serial_pld_len() + nonserial_pld_len());
}
SendReq::SendReq(sv_srvid_t dst_obj_id,
unsigned char *buf, uint16_t buflen, int flags)
:Message(SEND_REQ), _nb(false), _ipaddr(0),
_nsbuf(buf), _nonserial_len(buflen), _flags(flags)
{
memcpy(&_dst_obj_id, &dst_obj_id, sizeof(dst_obj_id));
set_pld_len_v(serial_pld_len() + nonserial_pld_len());
}
SendReq::SendReq(sv_srvid_t dst_obj_id, uint32_t ipaddr,
unsigned char *buf, uint16_t buflen, int flags)
:Message(SEND_REQ), _nb(false), _ipaddr(ipaddr),
_nsbuf(buf), _nonserial_len(buflen), _flags(flags)
{
memcpy(&_dst_obj_id, &dst_obj_id, sizeof(dst_obj_id));
set_pld_len_v(serial_pld_len() + nonserial_pld_len());
}
uint16_t
SendReq::serial_pld_len() const
{
return sizeof(_nb) +
sizeof(_dst_obj_id) + sizeof(_ipaddr) +
sizeof(_nonserial_len) + sizeof(_flags);
}
int
SendReq::check_type() const
{
return _type == SEND_REQ;
}
int
SendReq::write_serial_payload(unsigned char *buf) const
{
unsigned char *p = buf;
p += serial_write(_nb, p);
p += serial_write(_dst_obj_id, p);
p += serial_write(_ipaddr, p);
p += serial_write(_nonserial_len, p);
p += serial_write(_flags, p);
return p - buf;
}
int
SendReq::read_serial_payload(const unsigned char *buf)
{
const unsigned char *p = buf;
p += serial_read(&_nb, p);
p += serial_read(&_dst_obj_id, p);
p += serial_read(&_ipaddr, p);
p += serial_read(&_nonserial_len, p);
p += serial_read(&_flags, p);
return p - buf;
}
void
SendReq::print(const char *label) const
{
Message::print(label);
info("%s: dst_obj_id = %s, ipaddr = %i, buflen=%d, flags=%d\n",
label, oid_to_str(&_dst_obj_id), _ipaddr, _nonserial_len, _flags);
}
//
// SendRsp
//
SendRsp::SendRsp()
: Message(SEND_RSP), _err(0)
{
set_pld_len_v(serial_pld_len());
}
SendRsp::SendRsp(sv_err_t err)
: Message(SEND_RSP), _err(err)
{
set_pld_len_v(serial_pld_len());
}
uint16_t
SendRsp::serial_pld_len() const
{
return sizeof(_err);
}
int
SendRsp::check_type() const
{
return SEND_RSP;
}
int
SendRsp::write_serial_payload(unsigned char *buf) const
{
unsigned char *p = buf;
p += serial_write(_err, p);
return p - buf;
}
int
SendRsp::read_serial_payload(const unsigned char *buf)
{
const unsigned char *p = buf;
p += serial_read(&_err, p);
return p - buf;
}
void
SendRsp::print(const char *label) const
{
Message::print(label);
info("%s: err=%s", label, _err.v ? "t" : "f");
}