示例#1
0
 private void AttachErrorHandler(IDataErrorGenerator node)
 {
     node.DataIntegrityError    += err => DataIntegrityError?.Invoke(err);
     node.DataSymmetricityError += err => DataSymmetricityError?.Invoke(err);
 }
示例#2
0
        public void Receive(Stream data)
        {
            if (packetLength == 0)
            {
                data.Read(lengthBuffer, 0, 4);
                packetLength = BitConverter.ToInt32(lengthBuffer, 0);
                Log($"new data: packet length = {packetLength}", "DS");
                if (packetLength > bufferSize)
                {
                    Log($"DataIntegrityError: packet length = {packetLength}", "DS", LogType.ERROR);
                    DataIntegrityError?.Invoke(new DataIntegrityError("receive buffer overflow"));
                    return;
                }
                if (packetLength <= 0)
                {
                    Log($"DataIntegrityError: packet length = {packetLength}", "DS", LogType.ERROR);
                    DataIntegrityError?.Invoke(new DataIntegrityError("non-positive packet length"));
                    return;
                }
            }
            if (bufferPointer < packetLength)
            {
                int dataLength = data.Read(receiveBuffer, bufferPointer, packetLength - bufferPointer);
                bufferPointer += dataLength;
                Log($"buffering data: packet length = {packetLength}, bufferPointer = {bufferPointer}", "DS");
            }
            if (bufferPointer == packetLength)
            {
                using (MemoryStream copy = new MemoryStream(receiveBuffer, 0, bufferPointer))
                {
                    ClearBuffer();
                    object obj = null;
                    try
                    {
                        Log($"consuming data ...", "DS");
                        obj = formatter.Deserialize(copy);
                    }
                    catch
                    {
                        Log($"DataIntegrityError: non-serializable data", "DS", LogType.ERROR);
                        DataIntegrityError?.Invoke(
                            new DataIntegrityError("received non-serializable data"));
                        ClearBuffer();
                        return;
                    }

                    if (!(obj is AbstractProtocol))
                    {
                        Log($"DataIntegrityError: data is not [AbstractProtocol]", "DS", LogType.ERROR);
                        DataSymmetricityError?.Invoke(
                            new DataSymmetricityError("received data is not [AbstractProtocol]"));
                        ClearBuffer();
                        return;
                    }

                    ProviderDataReady?.Invoke((AbstractProtocol)obj);
                    copy.Close();
                }
            }
            if (data.Position < data.Length)
            {
                Log($"data is too large, position = {data.Position}, size = {data.Length}", "DS");
                Receive(data);
            }
        }