Length stored in a Byte

Posted By: GorNaKosh

Length stored in a Byte - 09/26/09 18:09

While reading the manual I fall over this here:
Originally Posted By: Online Manual
If the length of the buffer is not known to the client, it can be stored in a byte, short, or long at the beginning of the buffer.
http://www.conitec.net/beta/send_data.htm

How can I manage this - storing as well as reading?
Posted By: GorNaKosh

indetify different incomming structs - 09/29/09 15:28

I found a solution for my problem identifying incomming structs. Here is an example (not tested):
Code:
//flags for identify the structs
#define STRUCT_TYPE_A (1<<0)
#define STRUCT_TYPE_B (1<<1)
#define STRUCT_TYPE_C (1<<2)

//our test struct
typedef struct TEST_STRUCT {
     char flags;   //importent: the flag-char have to be the first property
                   //char = 1Byte -> can store 8flags
     var value;    //content of the struct
     char msg[25];
} TEST_STRUCT;

//example for a sending function
void sendingStruct(var reciever, var type, var value, char msg) {
     TEST_STRUCT *newPacket = malloc(sizeof(TEST_STRUCT));
     newPacket.flags = NULL;
     newPacket.flags |= type;
     newPacket.value = value;
     newPacket.msg = msg;
     
     send_struct_id(reciever,newPacket,sizeof(TEST_STRUCT));
     
     wait(-dplay_entrate/16);
     free(newPacket);
}

//example for a recieving function
on_server_event(void *clientBuf, var clientID) {
     
     if(event_type == EVENT_DATA) {
          
          char *tempBuf = clientBuf;
          
          //now tempBuf[0] points to the first byte of the
          //incomming struct - its the flag-property
          //so with this you can check the type
          
          if((tempBuf[0] & STRUCT_TYPE_A) == STRUCT_TYPE_A) {
          error("Type A");
          }

          if((tempBuf[0] & STRUCT_TYPE_B) == STRUCT_TYPE_B) {
          error("Type B");
          }

          if((tempBuf[0] & STRUCT_TYPE_C) == STRUCT_TYPE_C) {
          error("Type C");
          }
     }
}

//test main
void main() {
     
     //if it's a client
     if(connection == CONNECT_CLIENT) {

          //after connecting
          while (dplay_status < 2) { wait(1); }
          
          //sending a test
          sendingStruct(NULL, STRUCT_TYPE_A, 3, "Test!");
     }     

     return;
}


Anyone with other ideas or improvements???
Gor Na Kosh
© 2024 lite-C Forums