message.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __NETCOMM_FAWKES_MESSAGE_H_
00025 #define __NETCOMM_FAWKES_MESSAGE_H_
00026
00027 #include <core/utils/refcount.h>
00028 #include <core/exceptions/software.h>
00029
00030 #include <cstddef>
00031
00032 namespace fawkes {
00033
00034 #pragma pack(push,4)
00035
00036
00037
00038
00039 typedef struct {
00040 unsigned short int cid;
00041 unsigned short int msg_id;
00042 unsigned int payload_size;
00043 } fawkes_message_header_t;
00044
00045 #pragma pack(pop)
00046
00047
00048
00049
00050
00051
00052 typedef struct {
00053 fawkes_message_header_t header;
00054 void *payload;
00055 } fawkes_message_t;
00056
00057
00058
00059
00060
00061
00062 typedef struct {
00063 unsigned int size;
00064 } fawkes_transfer_header_t;
00065
00066
00067 class FawkesNetworkMessageTooBigException : public Exception
00068 {
00069 public:
00070 FawkesNetworkMessageTooBigException(size_t message_size);
00071 };
00072
00073 class FawkesNetworkMessageContent;
00074
00075 class FawkesNetworkMessage : public RefCount
00076 {
00077 public:
00078 FawkesNetworkMessage(unsigned int clid, fawkes_message_t &msg);
00079 FawkesNetworkMessage(fawkes_message_t &msg);
00080 FawkesNetworkMessage(unsigned int clid,
00081 unsigned short int cid, unsigned short int msg_id,
00082 void *payload, size_t payload_size);
00083 FawkesNetworkMessage(unsigned int clid,
00084 unsigned short int cid, unsigned short int msg_id);
00085 FawkesNetworkMessage(unsigned short int cid, unsigned short int msg_id,
00086 void *payload, size_t payload_size);
00087 FawkesNetworkMessage(unsigned int clid,
00088 unsigned short int cid, unsigned short int msg_id,
00089 FawkesNetworkMessageContent *content);
00090 FawkesNetworkMessage(unsigned short int cid, unsigned short int msg_id,
00091 FawkesNetworkMessageContent *content);
00092 FawkesNetworkMessage(unsigned short int cid, unsigned short int msg_id,
00093 size_t payload_size);
00094 FawkesNetworkMessage(unsigned short int cid, unsigned short int msg_id);
00095 FawkesNetworkMessage();
00096
00097 virtual ~FawkesNetworkMessage();
00098
00099 unsigned int clid() const;
00100 unsigned short int cid() const;
00101 unsigned short int msgid() const;
00102 size_t payload_size() const;
00103 void * payload() const;
00104 const fawkes_message_t & fmsg() const;
00105
00106
00107
00108
00109
00110
00111
00112
00113 template <typename MT>
00114 MT *
00115 msg() const
00116 {
00117 if ( payload_size() != sizeof(MT) ) {
00118 throw TypeMismatchException("FawkesNetworkMessage: message has incorrect size for this type");
00119 }
00120 return (MT *)(_msg.payload);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 template <typename MT>
00133 MT *
00134 msgge() const
00135 {
00136 if ( payload_size() < sizeof(MT) ) {
00137 throw TypeMismatchException("FawkesNetworkMessage: message has incorrect size for this type");
00138 }
00139 return (MT *)(_msg.payload);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 template <typename MT>
00153 MT *
00154 msgc() const
00155 {
00156 try {
00157 MT *m = new MT(cid(), msgid(), _msg.payload, payload_size());
00158 return m;
00159 } catch (Exception &e) {
00160 throw;
00161 } catch (...) {
00162 throw Exception("Unknown exception caught while parsing complex network message");
00163 }
00164 }
00165
00166 void set_client_id(unsigned int clid);
00167 void set_component_id(unsigned short int cid);
00168 void set_message_id(unsigned short int msg_id);
00169 void set_payload(void *payload, size_t payload_size);
00170 void set(fawkes_message_t &msg);
00171 void set_content(FawkesNetworkMessageContent *content);
00172
00173 void pack();
00174
00175 private:
00176 void init_cid_msgid(unsigned short int cid, unsigned short int msg_id);
00177 void init_payload(size_t payload_size);
00178
00179 unsigned int _clid;
00180 fawkes_message_t _msg;
00181
00182 FawkesNetworkMessageContent *_content;
00183 };
00184
00185 }
00186
00187 #endif