示例#1
0
        // Helper method to serialize to the raw byte format used for arrays
        private static byte[] RawSerialize <T>(Array Orig, int size, GetBytes <T> Bytes)
        {
            byte[] Ret = new byte[Orig.Length * size + size];

            for (int i = 0; i < Ret.Length - size; i += size)
            {
                byte[] raw = Bytes((T)Orig.GetValue(i / size));
                for (int j = 0; j < size; j++)
                {
                    Ret[i + j] = raw[j];
                }
            }

            return(Ret);
        }
示例#2
0
        private static byte[] ToBytes <T>(T[] values, GetBytes <T> getBytes)
        {
            int bytesPerElement = Marshal.SizeOf(typeof(T));

            byte[] rawData = new byte[values.Length * bytesPerElement];
            for (int i = 0; i < values.Length; i++)
            {
                byte[] currentData = getBytes(values[i]);
                for (int k = 0; k < currentData.Length; k++)
                {
                    rawData[(i * bytesPerElement) + k] = currentData[k];
                }
            }
            return(rawData);
        }
示例#3
0
文件: CRC32.cs 项目: Simakeng/Sync
 public void Compute <T>(T value)
 {
     foreach (var GetBytes in typeof(BitConverter).GetMethods())
     {
         if (GetBytes.Name != "GetBytes")
         {
             continue;
         }
         if (GetBytes.GetParameters()[0].ParameterType != typeof(T))
         {
             continue;
         }
         var bytes = GetBytes.Invoke(null, new object[] { value }) as byte[];
         Compute(bytes);
         return;
     }
     throw new Exception("BitConverter Don't have GetBytes Instance that recive parameter with type " + typeof(T).FullName);
 }
示例#4
0
        public void Connect <T>(IPAddress address,
                                int port,
                                AccessToData <T> accessToData,
                                GetBytes <T> bytesGetter,
                                StoreAction <T> store,
                                ActionWithData <T> action,
                                StopCondition <T> closeConnectionCondition) where T : class
        {
            IPEndPoint ipEndPoint = new IPEndPoint(address, port);
            Socket     sender     = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            sender.Connect(ipEndPoint);
            byte[] buffer = new byte[config.BufferSize];
            while (true)
            {
                T data = accessToData();
                if (closeConnectionCondition(data))
                {
                    onCloseConnection();
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                    return;
                }
                byte[] dataBytes = bytesGetter(data);
                int    bytesRest = dataBytes.Length;
                while (bytesRest > 0)
                {
                    int bytesSent = sender.Send(dataBytes);
                    dataBytes  = dataBytes.Skip(bytesSent).ToArray();
                    bytesRest -= bytesSent;
                }
                while (sender.Available > 0)
                {
                    int byteRecived = sender.Receive(buffer);
                    data = store(buffer, byteRecived, data);
                }
                data = action(data);
            }
        }
示例#5
0
 public object Get(GetBytes request)
 {
     return(Encoding.UTF8.GetBytes("Welcome to Emby!"));
 }