Ejemplo n.º 1
0
 public WaveguideMessage(WGMessageType type, WGStatus _status, byte[] payload, string payloadStr)
 {
     payloadBytes  = payload;
     messageType   = type;
     payloadString = payloadStr;
     status        = _status;
 }
Ejemplo n.º 2
0
        public static bool ParseMessage(byte[] data, out WaveguideMessage message)
        {
            bool success = true;

            message = null;

            if (data.Length < 4)
            {
                success = false;
            }
            else
            {
                try
                {
                    WGMessageType messageType = (WGMessageType)BitConverter.ToInt16(data, 0);
                    short         payloadSize = BitConverter.ToInt16(data, 2);
                    WGStatus      status      = WGStatus.UNKNOWN;
                    string        payloadStr  = "";
                    byte[]        payload     = null;

                    switch (messageType)
                    {
                    case WGMessageType.STATUS:
                        status  = (WGStatus)data[4];
                        payload = new byte[payloadSize - 1];
                        Buffer.BlockCopy(data, 5, payload, 0, payloadSize - 1);
                        payloadStr = Encoding.ASCII.GetString(payload);
                        break;

                    case WGMessageType.GET_STATUS:
                        break;

                    case WGMessageType.CONFIG_EXPERIMENT:
                        payload = new byte[payloadSize];
                        Buffer.BlockCopy(data, 4, payload, 0, payloadSize);
                        break;

                    case WGMessageType.START_EXPERIMENT:
                        break;

                    case WGMessageType.STOP_EXPERIMENT:
                        break;
                    }

                    message = new WaveguideMessage(messageType, status, payload, payloadStr);
                }
                catch (Exception)
                {
                    success = false;
                }
            }

            return(success);
        }