Ejemplo n.º 1
0
 void Pack()
 {
     if (_sendQueue.Count == 0)
     {
         return;
     }
     lock (_sendBuffer)
     {
         try
         {
             _iPackage.Pack(_sendQueue, _sendArray);
         }
         catch (Exception e)
         {
             throw new Exception("pack error: " + e);
         }
         _sendBuffer.WriteAllBytes(_sendArray.Read(_sendArray.Length));
     }
 }
Ejemplo n.º 2
0
 public T Read <T>()
 {
     return(Deserialize <T>(ByteArray.Read(ByteArray.Length)));
 }
Ejemplo n.º 3
0
 public T Read <T>(int length = 0)
 {
     if (typeof(T) == typeof(byte))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(byte)), typeof(T)));
     }
     if (typeof(T) == typeof(byte[]))
     {
         if (length == 0)
         {
             throw new Exception("read byte[] length:" + length);
         }
         return((T)Convert.ChangeType(ByteArray.Read(length), typeof(T)));
     }
     if (typeof(T) == typeof(bool))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(bool)), typeof(T)));
     }
     if (typeof(T) == typeof(char))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(char)), typeof(T)));
     }
     if (typeof(T) == typeof(double))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(double)), typeof(T)));
     }
     if (typeof(T) == typeof(short))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(short)), typeof(T)));
     }
     if (typeof(T) == typeof(int))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(int)), typeof(T)));
     }
     if (typeof(T) == typeof(long))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(long)), typeof(T)));
     }
     if (typeof(T) == typeof(float))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(float)), typeof(T)));
     }
     if (typeof(T) == typeof(string))
     {
         //index += length;
         if (length == 0)
         {
             throw new Exception("read string length:" + length);
         }
         return((T)Convert.ChangeType(Encoding.UTF8.GetString(ByteArray.Read(length)), typeof(T)));
     }
     if (typeof(T) == typeof(ushort))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(ushort)), typeof(T)));
     }
     if (typeof(T) == typeof(uint))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(uint)), typeof(T)));
     }
     if (typeof(T) == typeof(ulong))
     {
         return((T)Convert.ChangeType(ByteArray.Read(sizeof(ulong)), typeof(T)));
     }
     throw new Exception("can not get this type" + typeof(T));
 }