My Project
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Enumerations
Enumerator
Friends
Macros
Pages
netbuf.h
1
#ifndef netbuf_h
2
#define netbuf_h
3
4
#include <stddef.h>
/* size_t */
5
6
/*
7
* This header describes an interface for sending and receiving messages
8
* over byte-oriented streams that are accessed via a file descriptor.
9
* It is intended to be used for communication between machines that may
10
* have different architectures. Messages are composed of a few primitive
11
* datatypes which are packed into network byte-order for transmission
12
* and unpacked back to host-byte order on receipt. Method functions for
13
* packing, unpacking, reading and writing messages are provided below.
14
*/
15
16
/*
17
* Each network message is preceded by a 32-bit byte-count and a
18
* 32-bit opcode. The byte-count includes both of these items.
19
* The following enumerates the length of the message prefix in bytes.
20
*/
21
enum
{NET_PREFIX_LEN = 8};
22
23
/*
24
* List the packed sizes of each of the supported network data types.
25
*/
26
typedef
enum
{
27
NET_CHAR_SIZE = 1,
28
NET_SHORT_SIZE = 2,
29
NET_INT_SIZE = 4,
30
NET_FLOAT_SIZE = 4,
31
NET_DOUBLE_SIZE = 8
32
} NetTypeSize;
33
34
namespace
gcp {
35
namespace
control {
36
/*
37
* Declare an object type to encapsulate network messages in packed
38
* form. All members of this structure should be treated as private.
39
* An object of this type is encapsulated in both of the read and write
40
* stream structures described later.
41
*/
42
typedef
struct
NetBuf
{
43
unsigned
char
*buf;
/* The network I/O buffer */
44
unsigned
int
size;
/* The allocated size of buf[] */
45
unsigned
int
nget;
/* The number of bytes extracted from the buffer */
46
unsigned
int
nput;
/* The number of bytes added to the buffer */
47
unsigned
int
external;
/* True if buf[] is an externally provided buffer */
48
}
NetBuf
;
49
50
}
51
}
52
53
/*
54
* The following functions are used to create and destroy a network
55
* buffer object. Note that this is performed for you by new_NetReadStr()
56
* and new_NetSendStr() [see below].
57
*/
58
gcp::control::NetBuf
*new_NetBuf(
int
size);
/* NetBuf constructor */
59
gcp::control::NetBuf
*del_NetBuf(
gcp::control::NetBuf
*net);
/* NetBuf desctructor */
60
61
/*
62
* Return the size that was passed to new_NetBuf(). Note that this differs
63
* from net->size by NET_PREFIX_LEN.
64
*/
65
int
size_NetBuf(
gcp::control::NetBuf
*net);
66
67
/*
68
* NetBuf containers created with a buffer 'size' of zero must have
69
* an external buffer supplied before use.
70
*/
71
void
*net_set_buffer(
gcp::control::NetBuf
*net,
void
*buf,
size_t
length);
72
/*
73
* When an external buffer is being used to format messages for
74
* output, net_inc_nput() can be used to increment the recorded length
75
* of the message to account for externally formatted data in the
76
* provided buffer. net_inc_nput() returns the buffer index of the
77
* next byte in the buffer. It can thus also be used to determine the
78
* buffer index of the current end-of-message by sending nbytes=0.
79
*/
80
int
net_inc_nput(
gcp::control::NetBuf
*net,
int
nbytes);
81
82
/*
83
* After reading a message into a NetBuf buffer, as described later,
84
* the following method functions should be used to unpack the
85
* primitive datatype components of the message. To extract a message
86
* first call net_start_get() to read the message header, then use the
87
* net_get_*() functions to sequentially unpack each component of the
88
* message body, then finally call net_end_get() to verify that all
89
* of the bytes in the message have been extracted.
90
*/
91
int
net_start_get(
gcp::control::NetBuf
*net,
int
*opcode);
92
int
net_end_get(
gcp::control::NetBuf
*net);
93
int
net_get_char(
gcp::control::NetBuf
*net,
int
ndata,
unsigned
char
*data);
94
int
net_get_short(
gcp::control::NetBuf
*net,
int
ndata,
unsigned
short
*data);
95
int
net_get_int(
gcp::control::NetBuf
*net,
int
ndata,
unsigned
int
*data);
96
int
net_get_float(
gcp::control::NetBuf
*net,
int
ndata,
float
*data);
97
int
net_get_double(
gcp::control::NetBuf
*net,
int
ndata,
double
*data);
98
int
net_inc_nget(
gcp::control::NetBuf
*net,
int
nbytes);
99
100
/*
101
* The following method functions are used for packing
102
* primitive datatype components of a network message into a
103
* NetBuf buffer. To compose a new message first call
104
* net_start_put() to prepend the standard message prefix,
105
* then use the net_put_*() functions to pack one or more
106
* items into subsequent bytes of the message, then finally
107
* call net_end_put() to insert the final message byte-count
108
* at the start of the message. The message is then ready to
109
* be sent over a stream with nss_send_msg() as described below.
110
*
111
* Note that the opcode argument of net_start_put() is intended to be
112
* used by the application to enumerate the type of message being
113
* sent.
114
*/
115
int
net_start_put(
gcp::control::NetBuf
*net,
int
opcode);
116
int
net_end_put(
gcp::control::NetBuf
*net);
117
int
net_put_char(
gcp::control::NetBuf
*net,
int
ndata,
unsigned
char
*data);
118
int
net_put_short(
gcp::control::NetBuf
*net,
int
ndata,
unsigned
short
*data);
119
int
net_put_int(
gcp::control::NetBuf
*net,
int
ndata,
unsigned
int
*data);
120
int
net_put_float(
gcp::control::NetBuf
*net,
int
ndata,
float
*data);
121
int
net_put_double(
gcp::control::NetBuf
*net,
int
ndata,
double
*data);
122
123
namespace
gcp {
124
namespace
control {
125
/*
126
* Declare an object to encapsulate the process of reading messages
127
* from a network input stream.
128
*/
129
typedef
struct
NetReadStr
{
130
NetBuf
*net;
/* Network I/O buffer container */
131
unsigned
int
msglen;
/* The target message length (bytes) */
132
int
fd;
/* A stream file-descriptor open for reading */
133
enum
NetReadId {
/* (Do not change the ordering below) */
134
NET_READ_SIZE,
/* The message byte-count is being read */
135
NET_READ_DATA,
/* The message body is being read */
136
NET_READ_DONE,
/* The message has been completely read */
137
NET_READ_CLOSED,
/* Closed connection detected at start of message */
138
NET_READ_ERROR
/* I/O error detected while reading */
139
} state;
140
}
NetReadStr
;
141
}
142
}
143
144
/*
145
* The following functions must be used to attach/detach to a given open,
146
* readable byte stream. Note that the file-descriptor can refer to
147
* a blocking or non-blocking stream. The 'size' argument specifies
148
* the maximum expected size of a network message.
149
*/
150
gcp::control::NetReadStr
*new_NetReadStr(
int
fd,
int
size);
/* NetReadStr constructor */
151
gcp::control::NetReadStr
*del_NetReadStr(
gcp::control::NetReadStr
*nrs);
/* NetReadStr destructor */
152
void
attach_NetReadStr(
gcp::control::NetReadStr
*nrs,
int
fd);
/* Assign a new stream fd */
153
154
/*
155
* A single message is read through one or more calls to
156
* nrs_read_stream(). If the file-descriptor is set up to use
157
* non-blocking I/O then this may require more than one call. When
158
* a complete message has been read, nrs_read_stream() will return
159
* NET_READ_DONE. The resulting message can be found in nrs->net,
160
* and can be decoded as described earlier, via calls to net_start_get(),
161
* net_get_*() and net_end_get(). Other nrs_read_msg() return values
162
* are listed above in the declaration of the 'state' member of the
163
* NetReadStr object.
164
*/
165
int
nrs_read_msg(
gcp::control::NetReadStr
*nrs);
166
167
namespace
gcp {
168
namespace
control {
169
/*
170
* Declare an object to encapsulate the process of writing messages
171
* to a network output stream.
172
*/
173
typedef
struct
NetSendStr
{
174
NetBuf
*net;
/* Network I/O buffer container */
175
int
fd;
/* A stream file-descriptor open for writing */
176
enum
NetSendId {
177
NET_SEND_DATA,
/* Message is in the process of being written */
178
NET_SEND_DONE,
/* Message has been completely sent */
179
NET_SEND_CLOSED,
/* Closed connection detected */
180
NET_SEND_ERROR
/* Write I/O error detected */
181
} state;
182
}
NetSendStr
;
183
}
184
}
185
186
/*
187
* The following functions must be used to attach/detach to a given open,
188
* writable byte stream. Note that the file-descriptor can refer to
189
* a blocking or non-blocking stream. The 'size' argument specifies
190
* the maximum expected size of a network message.
191
*/
192
gcp::control::NetSendStr
*new_NetSendStr(
int
fd,
int
size);
/* NetSendStr constructor */
193
gcp::control::NetSendStr
*del_NetSendStr(
gcp::control::NetSendStr
*nss);
/* NetSendStr destructor */
194
void
attach_NetSendStr(
gcp::control::NetSendStr
*nss,
int
fd);
/* Assign a new stream fd */
195
196
/*
197
* Once a message has been completely composed in nss->net via calls
198
* to net_start_put(), net_put_*() and net_end_put(), it can be sent with
199
* one or more calls to nss_send_msg(). If the stream has been set up for
200
* non-blocking I/O then more than one call may be required. When the
201
* message has been completely sent, nss_send_msg() returns NET_SEND_DONE.
202
* Other return values are listed above in the declaration of the 'state'
203
* member of the NetSendStr object.
204
*/
205
int
nss_send_msg(
gcp::control::NetSendStr
*nss);
206
207
#endif
gcp::control::NetBuf
Definition:
netbuf.h:42
gcp::control::NetSendStr
Definition:
netbuf.h:173
gcp::control::NetReadStr
Definition:
netbuf.h:129
gcpCbass
control
code
unix
libunix_src
common
netbuf.h
Generated on Thu Jun 21 2018 14:30:00 for My Project by
1.8.6