COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
message.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef EC_MESSAGE_H
9 #define EC_MESSAGE_H
10 
11 #include <string.h>
12 #include <stdio.h>
13 #include <iostream>
14 
15 #include <util/coExport.h>
16 #include <util/byteswap.h>
17 
18 #ifdef _WIN64
19 #define __WORDSIZE 64
20 #endif
21 
22 /*
23  $Log: $
24  * Revision 1.4 1994/03/23 18:07:03 zrf30125
25  * Modifications for multiple Shared Memory segments have been finished
26  * (not yet for Cray)
27  *
28  * Revision 1.3 93/10/11 09:22:19 zrhk0125
29  * new types DM_CONTACT_DM and APP_CONTACT_DM included
30  *
31  * Revision 1.2 93/10/08 19:18:06 zrhk0125
32  * data type sizes introduced
33 * some fixed type sizes with sizeof calls replaced
34  *
35  * Revision 1.1 93/09/25 20:47:03 zrhk0125
36  * Initial revision
37  *
38  */
39 
40 /***********************************************************************\
41  ** **
42  ** Message classes Version: 1.1 **
43  ** **
44  ** **
45  ** Description : The basic message structure as well as ways to **
46  ** initialize messages easily are provided. **
47  ** Subclasses for special types of messages **
48  ** can be introduced. **
49  ** **
50  ** Classes : Message, ShmMessage **
51  ** **
52  ** Copyright (C) 1993 by University of Stuttgart **
53  ** Computer Center (RUS) **
54  ** Allmandring 30 **
55  ** 7000 Stuttgart 80 **
56  ** HOSTID **
57  ** **
58  ** Author : A. Wierse (RUS) **
59  ** **
60  ** History : **
61  ** 15.04.93 Ver 1.0 **
62  ** 15.04.93 Ver 1.1 new Messages and type added **
63  ** sender and send_type added **
64  ** **
65  ** **
66 \***********************************************************************/
67 
68 namespace covise
69 {
70 
71 const int MSG_NOCOPY = 0;
72 const int MSG_COPY = 1;
73 
74 class Connection;
75 
76 typedef long data_type;
77 
78 #ifdef BYTESWAP
79 
80 inline void swap_byte(unsigned int &byte) // only if necessary
81 {
82  byteSwap(byte);
83 }
84 
85 // only if necessary
86 inline void swap_bytes(unsigned int *bytes, int no)
87 {
88  byteSwap(bytes, no);
89 }
90 
91 inline void swap_short_byte(unsigned short &byte) // only if necessary
92 {
93  byteSwap(byte);
94 }
95 
96 // only if necessary
97 inline void swap_short_bytes(unsigned short *bytes, int no)
98 {
99  byteSwap(bytes, no);
100 }
101 
102 #else
103 inline void swap_byte(unsigned int){};
104 inline void swap_bytes(unsigned int *, int){};
105 inline void swap_short_byte(unsigned short){};
106 inline void swap_short_bytes(unsigned short *, int){};
107 #endif
108 
109 class TokenBuffer;
110 
111 class NETEXPORT Message // class for messages
112 {
113 public:
114  // message types
115  enum Type
116  {
117  EMPTY = -1,
118  HOSTID = 81,
119  SOCKET_CLOSED = 84,
120  CLOSE_SOCKET = 31,
121  STDINOUT_EMPTY = 54,
122  UI = 6,
123  RENDER = 45,
124  };
125 
127  {
130  };
131 
132  // static int new_count;
133  // static int delete_count;
134  int sender; // sender of message (max. 3bytes)
135  int send_type; // type of sender
136  int type; // type of the message
137  int length; // length of the message in byte
138  char *data; // pointer to the data of the message
139  Connection *conn; // connection at which message has been received (if so)
140  // empty initialization:
142  : sender(-1)
143  , send_type(Message::UNDEFINED)
144  , type(Message::EMPTY)
145  , length(0)
146  , data(NULL)
147  , conn(NULL)
148  , mustDelete(false)
149  {
150  //printf("+ in message no. %d for %p, line %d, type %d (%s)\n", 0, this, __LINE__, type, covise_msg_types_array[type]);
151  print();
152  };
153 
154  Message(TokenBuffer *);
155 
156  Message(TokenBuffer &);
157 
159  : sender(-1)
160  , send_type(Message::UNDEFINED)
161  , type(Message::EMPTY)
162  , length(0)
163  , data(NULL)
164  , conn(c)
165  , mustDelete(false)
166  {
167  //printf("+ in message no. %d for %p, line %d, type %d (%s)\n", 0, this, __LINE__, type, covise_msg_types_array[type]);
168  print();
169  };
170  // initialization with data only (for sending):
171  Message(int message_type, const std::string &str = std::string())
172  : sender(-1)
173  , send_type(Message::UNDEFINED)
174  , type(message_type)
175  , length(0)
176  , data(NULL)
177  , conn(NULL)
178  , mustDelete(true)
179  {
180  if (!str.empty())
181  {
182  length = (int)str.length() + 1;
183  data = new char[length];
184  memcpy(data, str.c_str(), length);
185  }
186  print();
187  };
188  Message(int message_type, const char *d, int cp)
189  : sender(-1)
190  , send_type(Message::UNDEFINED)
191  , type(message_type)
192  , length(0)
193  , data(NULL)
194  , conn(NULL)
195  , mustDelete(false)
196  {
197  //printf("+ in message no. %d for %p, line %d, type %d (%s)\n", 0, this, __LINE__, type, covise_msg_types_array[type]);
198  if (d)
199  length = (int)strlen(d) + 1;
200  else
201  length = 0;
202  if (cp == MSG_NOCOPY || d == NULL)
203  {
204  data = (char *)d;
205  }
206  else
207  {
208  data = new char[length];
209  memcpy(data, d, length);
210  mustDelete = true;
211  }
212  print();
213  };
214  Message(int message_type, int l, char *d, int cp = MSG_COPY)
215  : sender(-1)
216  , send_type(Message::UNDEFINED)
217  , type(message_type)
218  , length(l)
219  , data(NULL)
220  , conn(NULL)
221  , mustDelete(false)
222  {
223  //printf("+ in message no. %d for %p, line %d, type %d (%s)\n", 0, this, __LINE__, type, covise_msg_types_array[type]);
224  if (cp == MSG_NOCOPY || d == NULL)
225  {
226  data = d;
227  }
228  else
229  {
230  data = new char[length];
231  memcpy(data, d, length);
232  mustDelete = true;
233  }
234  print();
235  };
236  Message(const Message &); // copy constructor
238  {
239  if (mustDelete)
240  delete[] data;
241 
242  data = NULL;
243  // do NOT delete this pointer here - some apps take over the buffer!!
244  };
245  Message &operator=(const Message &); // assignment
246  void delete_data()
247  {
248  delete[] data;
249  data = NULL;
250  };
251  char *extract_data();
252  void print();
253 
254 private:
256 };
257 }
258 #endif
int type
Definition: message.h:136
void swap_short_bytes(unsigned short *, int)
Definition: message.h:106
void swap_byte(unsigned int)
Definition: message.h:103
void swap_bytes(unsigned int *, int)
Definition: message.h:104
Definition: covise_connect.h:115
void swap_short_byte(unsigned short)
Definition: message.h:105
SenderType
Definition: message.h:126
bool mustDelete
Definition: message.h:255
const int MSG_COPY
Definition: message.h:72
Definition: message_types.h:365
const int MSG_NOCOPY
Definition: message.h:71
Message(int message_type, int l, char *d, int cp=MSG_COPY)
Definition: message.h:214
#define NULL
Definition: covise_list.h:22
Definition: message.h:111
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
int sender
Definition: message.h:134
char * data
Definition: message.h:138
Definition: message_types.h:356
long data_type
Definition: message.h:74
~Message()
Definition: message.h:237
void delete_data()
Definition: message.h:246
Definition: tokenbuffer.h:73
const GLubyte * c
Definition: khronos-glext.h:9864
Type
Definition: message.h:115
Message(Connection *c)
Definition: message.h:158
GLenum type
Definition: khronos-glext.h:6279
Message(int message_type, const char *d, int cp)
Definition: message.h:188
double length(EDGE_VECTOR &vector)
Definition: CuttingSurfaceGPMUtil.h:70
Connection * conn
Definition: message.h:139
#define NETEXPORT
Definition: coExport.h:343
Message()
Definition: message.h:141
int length
Definition: message.h:137
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: khronos-glext.h:6354
int send_type
Definition: message.h:135
GLenum GLuint GLenum GLsizei length
Definition: khronos-glext.h:6279