Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Packet Handling Buffer?


Jacquelinett
 Share

Recommended Posts

@kenny:

> I'm a novice when it comes to packets, but if you don't get any answers try looking for packets in the source and see how it's used.

U do know that im talking about setting up a packet system right? I know how packet work in eclipse and can code.

Im talking create a packet system like Eclipse with C++ using winsock
Link to comment
Share on other sites

@Yumi-chan:

> U do know that im talking about setting up a packet system right? I know how packet work in eclipse and can code.
>
> Im talking create a packet system like Eclipse with C++ using winsock

Just write a few functions which handle packing different variable types into dynamic character array and send that.
Link to comment
Share on other sites

Writing a packet buffer is merely based on memory management and pointer arithmetic, really.

```
#include
#include

typedef struct Buffer                  Buffer;

struct Buffer
{
  unsigned char *iBytePointer;
  unsigned char *iByteList;
  size_t        iByteCount;
};

Buffer *AllocateBuffer(size_t iByteCount)
{
  Buffer *objBuffer = NULL;

  objBuffer = (Buffer *)calloc(1, sizeof(Buffer));

  if (objBuffer == NULL) return NULL;

  objBuffer->iByteList = (unsigned char *)calloc(iByteCount,
    sizeof(unsigned char));

  if (objBuffer->iByteList == NULL)
  {
    free(objBuffer);

    return NULL;
  }

  objBuffer->iBytePointer = objBuffer->iByteList;
  objBuffer->iByteCount  = iByteCount;

  return objBuffer;
}

void FreeBuffer(Buffer *objBuffer)
{
  if (objBuffer == NULL) return;

  free(objBuffer->iByteList);
  free(objBuffer);
}

int ResetBuffer(Buffer *objBuffer)
{
  if (objBuffer == NULL) return -1;

  objBuffer->iBytePointer = objBuffer->iByteList;

  return 0;
}

int ReadShort(Buffer *objBuffer, short *iValue)
{
  if (objBuffer == NULL || iValue == NULL) return -1;

  *iValue = *((short *)objBuffer->iBytePointer);
  objBuffer->iBytePointer += sizeof(short);

  return 0;
}

int WriteShort(Buffer *objBuffer, short iValue)
{
  if (objBuffer == NULL) return -1;

  *((short *)objBuffer->iBytePointer) = iValue;
  objBuffer->iBytePointer += sizeof(short);

  return 0;
}

/* The other stuff is practice for the reader. */

int main(int argc, char *argv[])
{
  Buffer *objBuffer = AllocateBuffer(256);
  short  iValue    = 0;

  if (objBuffer == NULL)
  {
    printf("Couldn\'t allocate the buffer.\n");

    return -1;
  }

  printf("Writing values to buffer.\n");

  WriteShort(objBuffer, 42);
  WriteShort(objBuffer, 69);
  WriteShort(objBuffer, 72);

  printf("Resetting buffer.\n");

  ResetBuffer(objBuffer);

  printf("Reading values from buffer.\n");

  ReadShort(objBuffer, &iValue);

  printf(" Primary: %d.\n", iValue);

  ReadShort(objBuffer, &iValue);

  printf(" Secondary: %d.\n", iValue);

  ReadShort(objBuffer, &iValue);

  printf(" Tertiary: %d.\n", iValue);

  FreeBuffer(objBuffer);

  return 0;
}

```

Yours faithfully,
  Stephan.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...