[C#] TCPBuffer - a class for maintaining a packet to be sent through TCP

Started by Ryex, August 07, 2010, 02:29:52 am

Previous topic - Next topic

Ryex

TCPBuffer
Authors: Ryex
Version: 1
Type: a class for maintaining a packet to be sent through TCP



Introduction

in my effort to create reliable and easy data transfer between two parts of an application i created this class
it allows some one to write longs, integers, and strings to a TCPBuffer class object and then quickly hand a byte[] with all the necessary data through a TCP connection and easily get the same data on the other side. with the under standing that data is read from the buffer in the same order it was written.
For example, if the first data written to the buffer is a long value that describes the data contained in the rest of the buffer then all a client has to do is read that first long value and then pass the buffer object to the appropriate function for further processing.


Features


  • read and write integers to a byte[]
  • read and write longs to a byte[]
  • read and write strings to a byte[]
  • maintain a read and write position in the buffer







Class Code


using System;
using System.Collections.Generic;
using System.Text;

namespace RmxosGUI
{
   class TCPBuffer
   {
       private byte[] buffer;      
       private long BufferSize;  
       private long WriteHead;
       private long ReadHead;

       public TCPbuffer()
       {
           Flush();
       }
       public static System.Array ResizeArray(System.Array oldArray, long newSize)
       {
           long oldSize = oldArray.Length;
           System.Type elementType = oldArray.GetType().GetElementType();
           System.Array newArray = System.Array.CreateInstance(elementType, newSize);
           long preserveLength = System.Math.Min(oldSize, newSize);
           if (preserveLength > 0)
               System.Array.Copy(oldArray, newArray, preserveLength);
           return newArray;
       }
       public void PreAllocate(long nLength)
       {
           WriteHead = 0;
           ReadHead = 0;
           BufferSize = nLength - 1;
           buffer = new byte[BufferSize];
       }

       public void Allocate(long nLength)
       {
           BufferSize = BufferSize + nLength;
           buffer = (byte[])ResizeArray(buffer, BufferSize);
       }

       public void Flush()
       {
           WriteHead = 0;
           ReadHead = 0;
           BufferSize = 0;
           buffer = new byte[0];
       }

       public void WriteByte(byte nByte)
       {
           if (WriteHead > BufferSize)
           {
               Allocate(1);
           }
           buffer[WriteHead] = nByte;
           WriteHead = WriteHead + 1;
       }

       public void WriteBytes(byte[] nBytes)
       {
           long nLength;
           nLength = nBytes.GetLongLength(0);
           System.Array.Copy(nBytes, 0, buffer, WriteHead, nLength);
           WriteHead = WriteHead + nLength;
       }

       public void WriteInteger(int nInteger)
       {
           if ((WriteHead + 1) > BufferSize)
           {
               Allocate(2);
           }
           byte[] nBytes = System.BitConverter.GetBytes(nInteger);
           System.Array.Copy(nBytes, 0, buffer, WriteHead, 2);
           WriteHead = WriteHead + 2;
       }

       public void WriteLong(long nLong)
       {
           if ((WriteHead + 3) > BufferSize)
           {
               Allocate(4);
           }
           byte[] nBytes = System.BitConverter.GetBytes(nLong);
           System.Array.Copy(nBytes, 0, buffer, WriteHead, 4);
           WriteHead = WriteHead + 4;
       }

       public void WriteString(string nString)
       {
           byte[] sBytes;
           long sLength;
           sLength = nString.Length;
           sBytes = System.Text.Encoding.UTF8.GetBytes(nString);
           WriteLong(sLength);
           if (sLength <= 0)
           {
               return;
           }
           if ((WriteHead + sLength - 1) > BufferSize)
           {
               Allocate(sLength);
           }
           System.Array.Copy(sBytes, 0, buffer, WriteHead, sLength);
           WriteHead = WriteHead + sLength;
       }
       public byte ReadByte()
       {
           bool MoveReadHead = true;
           if (ReadHead > BufferSize)
           {
               return(0);
           }
           byte value = buffer[ReadHead];
           if (MoveReadHead)
           {
               ReadHead = ReadHead + 1;
           }
           return (value);
       }
       public byte ReadByte(bool MoveReadHead)
       {
           if (ReadHead > BufferSize)
           {
               return(0);
           }
           byte value = buffer[ReadHead];
           if (MoveReadHead)
           {
               ReadHead = ReadHead + 1;
           }
           return (value);
       }

       public byte[] ReadBytes(long nLength)
       {
           bool MoveReadHead = true;
           byte[] Data;
           if ((ReadHead + nLength - 1) > BufferSize)
           {
               byte[] tempData = { 0 };
               return (tempData);
           }
           Data = new byte[nLength];
           System.Array.Copy(buffer, ReadHead, Data, 0, nLength);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + nLength;
           }
           return (Data);
       }
       public byte[] ReadBytes(long nLength, bool MoveReadHead)
       {
           byte[] Data;
           if ((ReadHead + nLength - 1) > BufferSize)
           {
               byte[] tempData = { 0 };
               return (tempData);
           }
           Data = new byte[nLength];
           System.Array.Copy(buffer, ReadHead, Data, 0, nLength);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + nLength;
           }
           return (Data);
       }

       public int ReadInteger()
       {
           bool MoveReadHead = true;
           if ((ReadHead + 1) > BufferSize)
           {
               return (0);
           }
           byte[] Data;
           int value;
           Data = new byte[2];
           System.Array.Copy(buffer, ReadHead, Data, 0, 2);
           value = System.BitConverter.ToInt32(Data, 0);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + 2;
           }
           return (value);
       }
       public int ReadInteger(bool MoveReadHead)
       {
           if ((ReadHead + 1) > BufferSize)
           {
               return (0);
           }
           byte[] Data;
           int value;
           Data = new byte[2];
           System.Array.Copy(buffer, ReadHead, Data, 0, 2);
           value = System.BitConverter.ToInt32(Data, 0);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + 2;
           }
           return (value);
       }

       public long ReadLong()
       {
           bool MoveReadHead = true;
           if ((ReadHead + 3) > BufferSize)
           {
               return (0);
           }
           byte[] Data;
           int value;
           Data = new byte[4];
           System.Array.Copy(buffer, ReadHead, Data, 0, 4);
           value = System.BitConverter.ToInt32(Data, 0);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + 4;
           }
           return (value);
       }
       public long ReadLong(bool MoveReadHead)
       {
           if ((ReadHead + 3) > BufferSize)
           {
               return (0);
           }
           byte[] Data;
           int value;
           Data = new byte[4];
           System.Array.Copy(buffer, ReadHead, Data, 0, 4);
           value = System.BitConverter.ToInt32(Data, 0);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + 4;
           }
           return (value);
       }

       public string ReadString()
       {
           bool MoveReadHead = true;
           long sLength;
           byte[] sBytes;
           sLength = ReadLong();
           if (sLength <= 0)
           {
               return ("");
           }
           sBytes = new byte[sLength - 1];
           System.Array.Copy(buffer, ReadHead, sBytes, 0, sLength);
           string value;
           value = System.Text.Encoding.UTF8.GetString(sBytes);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + sLength;
           }
           return (value);
       }
       public string ReadString(bool MoveReadHead)
       {
           long sLength;
           byte[] sBytes;
           sLength = ReadLong();
           if (sLength <= 0)
           {
               return ("");
           }
           sBytes = new byte[sLength - 1];
           System.Array.Copy(buffer, ReadHead, sBytes, 0, sLength);
           string value;
           value = System.Text.Encoding.UTF8.GetString(sBytes);
           if (MoveReadHead)
           {
               ReadHead = ReadHead + sLength;
           }
           return (value);
       }

       public long Count()
       {
           return (buffer.LongLength);
       }

       public long Length()
       {
           return (Count() - ReadHead);
       }

       public byte[] ToArray()
       {
           return (buffer);
       }
             


   }
}




Instructions

use the ToArray() function to generate the byte[] to pass through the TCP connection
when you receive a byte[] that was formated by the TCPBuffer class create a new buffer object and set the buffer variable to the received byte[] then proceed to use the read data from the buffer.
if the MoveReadHead value in any of the read functions is set to false the read head will not move and you will be able to read from that spot again. it is set to true by default




Credits and Thanks


  • Ryex



Author's Notes

Please look at the code tell me if you see any problems or areas for improvement.
also, tell me what you think! :P
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />