Quote:
Originally Posted by Super Aids
Well a 16 bit int length will only work with relative small images. If you want to use regular sized / a little big images you have to use a 32 bit int for the length of it.
Which means an uint for the length.
But I give up....
|
thanks any way for your help...
.................................................. .............................
i am working on client server app all and i need to send image from client to server so i made a packet class with the packet id, packet length and image bytes ... then i can send the class packet to server ... its fine and i receive the packet with the packet id but i need to get the image bytes from the packet to convert it back again to an image ..that i need to send image packet class from client to server and here is the code for client :
PHP Code:
public class Image : Writer, Interfaces.IPacket
{
public byte[] Buffer;
public byte[] ImageBytes;
public Image()
{
}
public void Deserialize(byte[] buffer)
{
Buffer = buffer;
}
public byte[] ToArray()
{
Buffer = new byte[(ImageBytes) + 6];
Writer.WriteUInt16(1003, 0 Buffer);
Writer.WriteUint16((ushort)(Buffer.Length), 2, Buffer);
Writer.WriteBytes(ImageBytes, 4, Buffer);
return Buffer;
}
}
and here is writer class :
PHP Code:
public class Writer
{
public static void WriteStringWithLength(string arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
int till = buffer.Length - offset;
till = Math.Min(arg.Length, till);
buffer[offset] = (byte)arg.Length;
offset++;
ushort i = 0;
while (i < till)
{
buffer[(ushort)(i + offset)] = (byte)arg[i];
i = (ushort)(i + 1);
}
}
public static void WriteString(string arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
if (buffer.Length >= offset + arg.Length)
{
unsafe
{
#if UNSAFE
fixed (byte* Buffer = buffer)
{
ushort i = 0;
while (i < arg.Length)
{
*((byte*)(Buffer + offset + i)) = (byte)arg[i];
i++;
}
}
#else
ushort i = 0;
while (i < arg.Length)
{
buffer[(ushort)(i + offset)] = (byte)arg[i];
i = (ushort)(i + 1);
}
#endif
}
}
}
public static void WriteByte(byte arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
buffer[offset] = arg;
}
public static void WriteBytes(byte[] arg, int offset, byte[] buffer)
{
foreach (byte arg2 in arg)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
buffer[offset] = arg2;
}
}
public static void WriteBoolean(bool arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
WriteByte(arg == true ? (byte)1 : (byte)0, offset, buffer);
}
public static void WriteUInt16(ushort arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
if (buffer.Length >= offset + sizeof(ushort))
{
unsafe
{
#if UNSAFE
fixed (byte* Buffer = buffer)
{
*((ushort*)(Buffer + offset)) = arg;
}
#else
buffer[offset] = (byte)arg;
buffer[offset + 1] = (byte)(arg >> 8);
#endif
}
}
}
public static void WriteUInt32(uint arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
if (buffer.Length >= offset + sizeof(uint))
{
unsafe
{
#if UNSAFE
fixed (byte* Buffer = buffer)
{
*((uint*)(Buffer + offset)) = arg;
}
#else
buffer[offset] = (byte)arg;
buffer[offset + 1] = (byte)(arg >> 8);
buffer[offset + 2] = (byte)(arg >> 16);
buffer[offset + 3] = (byte)(arg >> 24);
#endif
}
}
}
public static void WriteInt32(int arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
if (buffer.Length >= offset + sizeof(uint))
{
unsafe
{
#if UNSAFE
fixed (byte* Buffer = buffer)
{
*((int*)(Buffer + offset)) = arg;
}
#else
buffer[offset] = (byte)(arg);
buffer[offset + 1] = (byte)(arg >> 8);
buffer[offset + 2] = (byte)(arg >> 16);
buffer[offset + 3] = (byte)(arg >> 24);
#endif
}
}
}
public static void WriteUInt64(ulong arg, int offset, byte[] buffer)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
if (buffer.Length >= offset + sizeof(ulong))
{
unsafe
{
#if UNSAFE
fixed (byte* Buffer = buffer)
{
*((ulong*)(Buffer + offset)) = arg;
}
#else
buffer[offset] = (byte)(arg);
buffer[offset + 1] = (byte)(arg >> 8);
buffer[offset + 2] = (byte)(arg >> 16);
buffer[offset + 3] = (byte)(arg >> 24);
buffer[offset + 4] = (byte)(arg >> 32);
buffer[offset + 5] = (byte)(arg >> 40);
buffer[offset + 6] = (byte)(arg >> 48);
buffer[offset + 7] = (byte)(arg >> 56);
#endif
}
}
}
public static void WriteStringList(List<string> arg, int offset, byte[] buffer)
{
if (arg == null)
return;
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
buffer[offset] = (byte)arg.Count;
offset++;
foreach (string str in arg)
{
buffer[offset] = (byte)str.Length;
WriteString(str, offset + 1, buffer);
offset += str.Length + 1;
}
}
}
and i am using this to get packet id and length in server :
PHP Code:
ushort PacketID = BitConverter.UInt16(packet, 0);
ushort Length = BitConverter.UInt16(packet, 2);
PHP Code:
so i want to know how is the image packet class will go in server side ??
i am not sure of writeBites void in writer class ... and how to make it read specific image bytes to use this bytes to convert it to an image ?
PHP Code:
public static void WriteBytes(byte[] arg, int offset, byte[] buffer)
{
foreach (byte arg2 in arg)
{
if (buffer == null)
return;
if (offset > buffer.Length - 1)
return;
buffer[offset] = arg2;
}
}