![]() |
![]() |
![]() |
GNet Network Library Reference Manual | ![]() |
---|---|---|---|---|
#include <gnet.h> gint gnet_pack (const gchar *format, gchar *buffer, const gint length, ...); gint gnet_pack_strdup (const gchar *format, gchar **bufferp, ...); gint gnet_vpack (const gchar *format, gchar *buffer, const gint length, va_list args); gint gnet_calcsize (const gchar *format, ...); gint gnet_vcalcsize (const gchar *format, va_list args); gint gnet_unpack (const gchar *format, const gchar *buffer, gint length, ...); gint gnet_vunpack (const gchar *format, const gchar *buffer, gint length, va_list args);
The pack module provides functions for converting data into bytes ("packing") and converting bytes to data ("unpacking"). For example, two integers and a string may be packed into a buffer to be transfered over the network. These functions are similar to the ones in Python and Perl.
gint gnet_pack (const gchar *format, gchar *buffer, const gint length, ...);
Writes Varargs
to buffer
. format
is a string that describes
the Varargs
and how they are to be packed. This string is a list
of characters, each describing the type of an argument in
Varargs
.
An example:
char buf[5]; int myint = 42; int mybyte = 23; gnet_pack("!ib", buf, sizeof(buf), myint, mybyte); /* Now buf is { 42, 0, 0, 0, 23 }; */
As a shortcut, most types can be prefixed by an integer to specify how many times the type is repeated. For example, "4i2b" is equivalent to "iiiibb". This repeat argument is refered below to as REPEAT.
Native byte order and sizes are used by default. If the first
character of format
is < then little endian order and standard
sizes are used. If the first character is > or !, then big endian
(or network) order and standard size are used. Standard sizes are
1 byte for chars, 2 bytes for shorts, and 4 bytes for ints and
longs.
The types:
x is a NULL character. It can be used for padding. It does not
correspond to an argument in Varargs
.
b/B is a signed/unsigned char.
h/H is a signed/unsigned short.
i/I is a signed/unsigned int.
l/L is a signed/unsigned long.
f/D is a float/double (always native order/size).
v is a void pointer (always native size).
s is a zero-terminated string.
S is a zero-padded string of maximum length REPEAT. We write up-to a NULL character or REPEAT characters, whichever comes first. We then write NULL characters up to a total of REPEAT characters. Special case: if REPEAT is not specified, we write the string as a non-NULL-terminated string (note that it can't be unpacked easily then).
r is a byte array of NEXT bytes. NEXT is the next argument passed
to gnet_pack()
and is a gint.
R is a byte array of REPEAT bytes. REPEAT must be specified.
p is a Pascal string. The string passed is a NULL-termiated string of less than 256 character. The string writen is a non-NULL-terminated string with a byte before the string storing the string length. REPEAT is repeat.
Mnemonics: (B)yte, s(H)ort, (I)nteger, (F)loat, (D)ouble, (V)oid pointer, (S)tring, (R)aw
Pack was inspired by Python's and Perl's pack. It is more like Python's than Perl's. Note that in GNet, a repeat of 0 does not align the data (as in Python).
format : |
pack data format |
buffer : |
buffer to pack to |
length : |
length of buffer
|
... : |
variables to pack from |
Returns : | number of bytes packed; -1 on error. |
gint gnet_pack_strdup (const gchar *format, gchar **bufferp, ...);
Writes Varargs
into a buffer pointed to by bufferp
. The buffer
is allocated by the function and is caller owned. See gnet_pack()
for more information.
format : |
pack data format |
bufferp : |
pointer to a buffer (buffer is caller owned) |
... : |
variables to pack from |
Returns : | bytes packed; -1 on error. |
gint gnet_vpack (const gchar *format, gchar *buffer, const gint length, va_list args);
Var arg interface to gnet_pack()
. See gnet_pack()
for more
information.
format : |
pack data format |
buffer : |
buffer to pack to |
length : |
length of buffer
|
args : |
var args |
Returns : | bytes packed; -1 on error. |
gint gnet_calcsize (const gchar *format, ...);
Calculates the size of the buffer needed to pack Varargs
by the
given format. See gnet_pack()
for more information.
format : |
pack data format |
... : |
variables |
Returns : | number of bytes required to pack; -1 on error. |
gint gnet_vcalcsize (const gchar *format, va_list args);
Var arg interface to gnet_calcsize()
. See gnet_calcsize()
for
additional information.
format : |
pack data format |
args : |
var args |
Returns : | number of bytes required to pack; -1 on error. |
gint gnet_unpack (const gchar *format, const gchar *buffer, gint length, ...);
Reads the data in buffer
into Varargs
. format
is a string that
describes the Varargs
and how they are to be packed. This string
is a list of characters, each describing the type of an argument
in Varargs
.
An example:
char buf[5] = { 42, 0, 0, 0, 23 }; int myint; int mybyte; gnet_unpack("!ib", buf, sizeof(buf), &myint, &mybyte); /* Now myint is 42 and mybyte is 23 */
In unpack, the arguments must be pointers to the appropriate type.
Strings and byte arrays are allocated dynamicly (by g_new()
). The
caller is responsible for g_free()
-ing it.
As a shortcut, most types can be prefixed by an integer to specify how many times the type is repeated. For example, "4i2b" is equivalent to "iiiibb". This repeat argument is refered below to as REPEAT.
The types:
x is a pad byte. The byte is skipped and not stored. We do not check its value.
b/B is a signed/unsigned char.
h/H is a signed/unsigned short.
i/I is a signed/unsigned int.
l/L is a signed/unsigned long.
f/D is a float/double (always native order/size). v is a void pointer (always native size).
s is a zero-terminated string.
S is a zero-padded string of length REPEAT. We read REPEAT characters or until a NULL character. Any remaining characters are filled in with 0's. REPEAT must be specified.
r is a byte array of NEXT bytes. NEXT is the next argument and is a gint. REPEAT is repeat.
R is a byte array of REPEAT bytes. REPEAT must be specified.
p is a Pascal string. The first byte read is the length of the string and the string follows. The unpacked string will be a normal, NULL-terminated string. REPEAT is repeat.
format : |
unpack data format |
buffer : |
buffer to unpack from |
length : |
length of buffer
|
... : |
addresses of variables to unpack to |
Returns : | number of bytes unpacked; -1 on error. The bytes are
unpacked to the variables pointed to by the Varargs .
|
gint gnet_vunpack (const gchar *format, const gchar *buffer, gint length, va_list args);
Var arg interface to gnet_unpack()
. See gnet_unpack()
for more
information.
format : |
unpack data format |
buffer : |
buffer to unpack from |
length : |
length of buffer
|
args : |
var args |
Returns : | number of bytes packed; -1 on error. |