I would please like some help with this case.
I need to send one XDR message, that it's composed by 2 files (switch case == OK).
Assuming I have in my code a message
object called response
:
message response;
If I had to only send one file in a message, I would do:
response.message_u.fdata.last_mod_time = last_modification;
response.message_u.fdata.contents.contents_len = file_size;
response.message_u.fdata.contents.contents_val = buffer;
With buffer being:
buffer = malloc(file_size * sizeof(char));
Now, I understand that struct file fdata<2>;
it's a variable size array (2 is the max length) and that I would have them index as something[0]
and something[1]
.
Also I know I need to allocate memory, but I do not know how to do this in this case with multiple files.
Do I need to do one single malloc for everything I need to send? Something like this:
response.message_u.fdata.fdata_val = malloc ( (file_size1 * sizeof(char)) + (file_size2 * sizeof(char)) + (2 * sizeof(uint32_t)));
(2 * sizeof(uint32_t))
: one last_mod_time for each file to send
And the other question how to I refer to each file structure:
response.message_u.fdata[0] //?
response.message_u.fdata.last_mod_time[0] //?
response.message_u.fdata[0].last_mod_time //?
response.message_u.fdata.contents.contents_len[0] //?
response.message_u.fdata.contents.contents_val[0] //?
The .x file:
enum tagtype {
GET = 0,
OK = 1,
QUIT = 2,
ERR = 3
};
struct file {
opaque contents<>;
unsigned int last_mod_time;
};
typedef string filename<256>;
union message switch (tagtype tag) {
case GET:
filename filenamedata<2>;
case OK:
struct file fdata<2>;
case QUIT:
void;
case ERR:
void;
};
The types.c (generated with rpcgen):
#include "xdr_types.h"
bool_t
xdr_tagtype (XDR *xdrs, tagtype *objp)
{
register int32_t *buf;
if (!xdr_enum (xdrs, (enum_t *) objp))
return FALSE;
return TRUE;
}
bool_t
xdr_file (XDR *xdrs, file *objp)
{
register int32_t *buf;
if (!xdr_bytes (xdrs, (char **)&objp->contents.contents_val, (u_int *) &objp->contents.contents_len, ~0))
return FALSE;
if (!xdr_u_int (xdrs, &objp->last_mod_time))
return FALSE;
return TRUE;
}
bool_t
xdr_filename (XDR *xdrs, filename *objp)
{
register int32_t *buf;
if (!xdr_string (xdrs, objp, 256))
return FALSE;
return TRUE;
}
bool_t
xdr_message (XDR *xdrs, message *objp)
{
register int32_t *buf;
if (!xdr_tagtype (xdrs, &objp->tag))
return FALSE;
switch (objp->tag) {
case GET:
if (!xdr_array (xdrs, (char **)&objp->message_u.filenamedata.filenamedata_val, (u_int *) &objp->message_u.filenamedata.filenamedata_len, 10,
sizeof (filename), (xdrproc_t) xdr_filename))
return FALSE;
break;
case OK:
if (!xdr_array (xdrs, (char **)&objp->message_u.fdata.fdata_val, (u_int *) &objp->message_u.fdata.fdata_len, 10, sizeof (file), (xdrproc_t) xdr_file))
return FALSE;
break;
case QUIT:
break;
case ERR:
break;
default:
return FALSE;
}
return TRUE;
}
Thank you for reading this, and trying to understand this. I really appreciate it.
Thank you!