コード例 #1
0
        private void State_MessageDecoded(object sender, CommunicationMessageEventArgs e)
        {
            string payload = Encoding.UTF8.GetString(e.Payload);

            switch (e.Type)
            {
            case 1:
                Tuple <int, long> authData = CommunicationMessageInterpreter.GetAuthentificationData(e.Payload);
                Type = authData.Item1;
                Id   = authData.Item2;
                OnAuthentificated();
                break;

            default:
                OnDataAvailable(e);
                break;
            }
            Console.WriteLine($"{e.Type} Payload: {payload}");
        }
コード例 #2
0
        public void ReadMessage()
        {
            ulong bytesRead = BytesRead;
            int   index     = 0;

            if (Header.Type == 0 && bytesRead >= sizeof(uint))
            {
                byte[] typeArray = new byte[sizeof(uint)];
                Array.Copy(Buffer, index, typeArray, 0, sizeof(uint));
                uint type = BitConverter.ToUInt32(typeArray, 0);
                Header.Type = type;
                bytesRead  -= sizeof(uint);
                index      += sizeof(uint);
            }

            if (Header.PayloadSize == 0 && bytesRead >= sizeof(uint))
            {
                byte[] payloadArray = new byte[sizeof(uint)];
                Array.Copy(Buffer, index, payloadArray, 0, sizeof(uint));
                uint payloadSize = BitConverter.ToUInt32(payloadArray, 0);
                bytesRead         -= sizeof(uint);
                index             += sizeof(uint);
                Header.PayloadSize = payloadSize;
            }

            if (Payload == null && Header.PayloadSize != 0)
            {
                Payload = new byte[Header.PayloadSize];
            }

            ulong remaningBytes = Header.PayloadSize - CurrentPayloadSize;

            if (bytesRead > remaningBytes)
            {
                Array.Copy(Buffer, index, Payload, (int)CurrentPayloadSize, (int)remaningBytes);
                bytesRead          -= remaningBytes;
                index              += (int)remaningBytes;
                CurrentPayloadSize += remaningBytes;
            }
            else
            {
                Array.Copy(Buffer, index, Payload, (int)CurrentPayloadSize, (int)bytesRead);
                CurrentPayloadSize += bytesRead;
                bytesRead           = 0;
            }

            if (bytesRead != 0)
            {
                Array.Copy(Buffer, index, Buffer, 0, (int)bytesRead);
            }
            Offset    = (int)bytesRead;
            BytesRead = bytesRead;
            if (CurrentPayloadSize == Header.PayloadSize)
            {
                CommunicationMessageEventArgs args = new CommunicationMessageEventArgs();
                args.Type    = Header.Type;
                args.Payload = new byte[Header.PayloadSize];
                Array.Copy(Payload, args.Payload, (int)Header.PayloadSize);
                OnMessageDecoded(args);
                Reset();
            }
            if (BytesRead >= sizeof(uint))
            {
                ReadMessage();
            }
        }
コード例 #3
0
 protected virtual void OnMessageDecoded(CommunicationMessageEventArgs args)
 {
     MessageDecoded?.Invoke(this, args);
 }
コード例 #4
0
 protected virtual void OnDataAvailable(CommunicationMessageEventArgs args)
 {
     DataAvailable?.Invoke(this, args);
 }