Fawkes API Fawkes Development Version
message_content.cpp
1
2/***************************************************************************
3 * message_content.cpp - Fawkes network message content
4 *
5 * Created: Fri Jun 01 13:31:18 2007
6 * Copyright 2006-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 <netcomm/fawkes/message_content.h>
26
27#include <cstring>
28
29namespace fawkes {
30
31/** @class FawkesNetworkMessageContent <netcomm/fawkes/message_content.h>
32 * Fawkes network message content.
33 * Interface for complex Fawkes network messages. Use this type if you want
34 * either a nicer interface to your network message or if you need a more
35 * complex kind of message type, for example by using DynamicBuffer.
36 *
37 * Implement all accessor methods that you need and add any data you want.
38 * In the end you have to implement serialize() to create a single contiguous
39 * buffer that contains all the data that has to be sent. Make _payload point
40 * to this buffer and _payload_size contain the size of the buffer.
41 *
42 * @see DynamicBuffer
43 * @ingroup NetComm
44 * @author Tim Niemueller
45 *
46 * @fn void FawkesNetworkMessageContent::serialize() = 0
47 * Serialize message content.
48 * Generate a single contiguous buffer. Make _payload point to this buffer and
49 * _payload_size contain the size of the buffer.
50 */
51
52/** Constructor. */
54{
55 _payload = NULL;
56 _payload_size = 0;
57}
58
59/** Virtual empty destructor. */
61{
62}
63
64/** Return pointer to payload.
65 * @return pointer to payload
66 * @exception NullPointerException thrown if _payload does not point to a valid
67 * buffer or if _payload_size is zero.
68 */
69void *
71{
72 if ((_payload == NULL) || (_payload_size == 0)) {
73 throw NullPointerException("Payload in network message content may not be NULL");
74 }
75 return _payload;
76}
77
78/** Return payload size
79 * @return payload size
80 * @exception NullPointerException thrown if _payload does not point to a valid
81 * buffer or if _payload_size is zero.
82 */
83size_t
85{
86 if ((_payload == NULL) || (_payload_size == 0)) {
87 throw NullPointerException("Payload in network message content may not be NULL");
88 }
89 return _payload_size;
90}
91
92/** Copy payload into payload buffer to a specified offset.
93 * This assumes that you have made sure that the buffer is big enough!
94 * @param offset offset in _payload where to copy the data to
95 * @param buf buffer to copy from
96 * @param len number of bytes to copy from buf
97 */
98void
99FawkesNetworkMessageContent::copy_payload(size_t offset, const void *buf, size_t len)
100{
101 void *tmp = (void *)((size_t)_payload + offset);
102 memcpy(tmp, buf, len);
103}
104
105} // end namespace fawkes
virtual ~FawkesNetworkMessageContent()
Virtual empty destructor.
void copy_payload(size_t offset, const void *buf, size_t len)
Copy payload into payload buffer to a specified offset.
virtual size_t payload_size()
Return payload size.
void * _payload
Pointer to payload.
virtual void * payload()
Return pointer to payload.
A NULL pointer was supplied where not allowed.
Definition: software.h:32
Fawkes library namespace.