Fawkes API Fawkes Development Version
fuse_message.cpp
1
2/***************************************************************************
3 * fuse_message.cpp - FireVision Remote Control Protocol Message Type
4 *
5 * Created: Wed Nov 07 13:01:20 2007
6 * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#include <core/exceptions/software.h>
25#include <fvutils/net/fuse_message.h>
26#include <fvutils/net/fuse_message_content.h>
27#include <netinet/in.h>
28
29#include <cstdio>
30#include <cstdlib>
31#include <cstring>
32
33namespace firevision {
34
35/** @class FuseNetworkMessage <fvutils/net/fuse_message.h>
36 * FUSE Network Message.
37 * This is the basic entity for messages that are sent over the network. Either
38 * just use this message to send arbitrary payload or derive this class for more
39 * complex behavior or nice encapsulations of messages.
40 *
41 * @ingroup FUSE
42 * @ingroup FireVision
43 * @author Tim Niemueller
44 */
45
46/** Constructor. */
48{
49 memset(&_msg, 0, sizeof(_msg));
50 content_ = NULL;
51}
52
53/** Constructor.
54 * @param msg message information to copy
55 */
57{
58 memcpy(&_msg, msg, sizeof(FUSE_message_t));
59 content_ = NULL;
60}
61
62/** Constructor.
63 * @param type message type
64 * @param payload payload
65 * @param payload_size size of payload
66 * @param copy_payload if true payload is copied, otherwise payload is referenced
67 * and ownership of payload is claimed.
68 */
70 void * payload,
71 size_t payload_size,
72 bool copy_payload)
73{
74 content_ = NULL;
77
78 if (copy_payload) {
79 _msg.payload = malloc(payload_size);
81 } else {
83 }
84}
85
86/** Constructor without payload.
87 * Constructs message without payload.
88 * @param type FUSE message type
89 */
91{
92 content_ = NULL;
94 _msg.header.payload_size = htonl(0);
95 _msg.payload = NULL;
96}
97
98/** Content constructor.
99 * Construct a message with complex message content.
100 * @param type FUSE message type
101 * @param content complex message content.
102 */
104{
105 content_ = content;
106 _msg.header.message_type = htonl(type);
107 _msg.header.payload_size = htonl(0);
108 _msg.payload = NULL;
109}
110
111/** Destructor. */
113{
114 if (content_ == NULL) {
115 if (_msg.payload != NULL) {
116 free(_msg.payload);
117 _msg.payload = NULL;
118 }
119 } else {
120 content_->free_payload();
121 delete content_;
122 }
123}
124
125/** Get message type.
126 * @return message type
127 */
128uint32_t
130{
131 return ntohl(_msg.header.message_type);
132}
133
134/** Get payload size.
135 * @return payload size
136 */
137size_t
139{
140 return ntohl(_msg.header.payload_size);
141}
142
143/** Get pointer to payload.
144 * @return pointer to payload.
145 */
146void *
148{
149 return _msg.payload;
150}
151
152/** Get plain message.
153 * @return plain message
154 */
155const FUSE_message_t &
157{
158 return _msg;
159}
160
161/** Set payload.
162 * Payload is referenced and ownership claimed.
163 * @param payload payload
164 * @param payload_size size of payload
165 */
166void
167FuseNetworkMessage::set_payload(void *payload, size_t payload_size)
168{
169 if (payload_size > 0xFFFFFFFF) {
170 // cannot carry that many bytes
171 throw fawkes::OutOfBoundsException("Payload too big", payload_size, 0, 0xFFFFFFFF);
172 }
175}
176
177/** Set from message.
178 * @param msg reference to message. Content is deep-copied.
179 */
180void
182{
183 memcpy(&_msg, &msg, sizeof(FUSE_message_t));
184}
185
186/** Pack data for sending.
187 * Use this if any additional packing is needed before sending the data (for
188 * example if using a DynamicBuffer).
189 */
190void
192{
193 if (content_ != NULL) {
194 content_->serialize();
195 _msg.payload = content_->payload();
196 _msg.header.payload_size = htonl(content_->payload_size());
197 }
198}
199
200} // end namespace firevision
Index out of bounds.
Definition: software.h:86
void free_payload()
Free message payload.
virtual void * payload() const
Return pointer to payload.
virtual size_t payload_size() const
Return payload size.
virtual void serialize()=0
Serialize message content.
void * payload() const
Get pointer to payload.
MT * msg() const
Get correctly casted payload.
Definition: fuse_message.h:67
void set_payload(void *payload, size_t payload_size)
Set payload.
FUSE_message_t _msg
Internal message.
Definition: fuse_message.h:128
size_t payload_size() const
Get payload size.
const FUSE_message_t & fmsg() const
Get plain message.
void pack()
Pack data for sending.
uint32_t type() const
Get message type.
void set(FUSE_message_t &msg)
Set from message.
uint32_t payload_size
payload size
Definition: fuse.h:86
uint32_t message_type
packet type from FUSE_message_type_t
Definition: fuse.h:85
FUSE message.
Definition: fuse.h:91
void * payload
payload
Definition: fuse.h:93
FUSE_header_t header
header
Definition: fuse.h:92