To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.legosOpen lugnet.robotics.rcx.legos in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / legOS / 2361
2360  |  2362
Subject: 
Re: problem with lnpd...help!?
Newsgroups: 
lugnet.robotics.rcx.legos
Date: 
Fri, 15 Mar 2002 20:26:39 GMT
Viewed: 
1883 times
  
This is really more of a general C question than an lnpd question. Anyway,
the problem is with serialization; you want to take a structure in memory,
and turn it into a stream of bytes, either to store in a file, send over a
network, or whatever.

Your serialization code needs to know more about the structure of your
data than you can get from the struct you provided. Will the two pointers
always be pointers to C strings? If so, you can write something like this:

void SendStruct(struct foo *toSend)
{
char *buf;
int bufLen;
char *cursor;
int stringLength;

bufLen = sizeof(int) + strlen(toSend->test2) + 1 +
strlen(toSend->test3) + 1;
buf = malloc(bufLen)
cursor = buf;

*((int *)cursor) = toSend->test; // copy test to cursor position
cursor += sizeof[int];

stringLength = strlen(toSend->test2);
memcpy(cursor, toSend->test2, stringLength + 1); // copy test2's
//string, including the terminating null
cursor += stringLength;

// do the same thing with test3

// send buf using LNP calls

free(buf);
}

Ok, there we are. You have to do some work, because, well, that's how C
is. This routine needs some free memory, and does some copying. It would
be possible to send everything directly without copying, but this would
require multiple sends, and thus multiple packets, and that could cause
problems if one of them is lost.

The strlen calls are duplicated; it will get called twice for each string.
This isn't necessary. It should be called once on each string and the
value stored, but I didn't think of it right away and for some reason
decided it was easier to type this explanation than to fix the code. :)

You should check malloc's return value to see if it's NULL, and handle the
error. This is true with C on any platform, but especially important on
legOS because there's so little memory to work with.

I hope this helps. I don't how how good your C is, so this may be above or
below your depth, depending. Please write back if you have any questions.

--
"From now on, we live in a world where man has walked on the moon.
And it's not a miracle, we just decided to go." -- Jim Lovell

Mike Ash - <http://www.mikeash.com/>, <mailto:mail@mikeash.com>



Message is in Reply To:
  problem with lnpd...help!?
 
hi all. im using legOS 0.2.4 and using lnpd to communicate with my linux box. now then, here is my problem. lets imagine i have the following: foo{ int test; char *test2; void *test3; }; char *test4 = 'a'; char *test5 = 'b'; struct foo bar* = NULL; (...) (23 years ago, 15-Mar-02, to lugnet.robotics.rcx.legos)

11 Messages in This Thread:





Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR