コード例 #1
0
        private bool TryDeserialize(byte[] data, out object result)
        {
            if (_deserializeFormat.HasValue)
            {
                bool shouldLogError = _isNewFormat ?? false;
                switch (_deserializeFormat.Value)
                {
                case SerializationFormat.Json:
                    return(JsonDeserializer.TryDeserialize(
                               data,
                               _messageType,
                               shouldLogError ? _log : null,
                               out result));

                case SerializationFormat.MessagePack:
                    return(MessagePackDeserializer.TryDeserialize(
                               data,
                               _messageType,
                               shouldLogError ? _log : null,
                               out result));

                case SerializationFormat.Protobuf:
                    return(ProtobufDeserializer.TryDeserialize(
                               data,
                               _messageType,
                               shouldLogError ? _log : null,
                               out result));

                default:
                    throw new NotSupportedException($"Serialization format {_deserializeFormat.Value} is not supported");
                }
            }
            bool success = JsonDeserializer.TryDeserialize(
                data,
                _messageType,
                null,
                out result);

            if (success)
            {
                _deserializeFormat = SerializationFormat.Json;
                return(true);
            }
            success = MessagePackDeserializer.TryDeserialize(
                data,
                _messageType,
                null,
                out result);
            if (success)
            {
                _deserializeFormat = SerializationFormat.MessagePack;
                return(true);
            }
            success = ProtobufDeserializer.TryDeserialize(
                data,
                _messageType,
                null,
                out result);
            if (!success)
            {
                _log.WriteWarning(nameof(TryDeserialize), null, $"Couldn't deserialize message with length {data.Length}");
            }
            return(success);
        }
コード例 #2
0
        protected void ProcessMessage(byte[] raw_data_buffer)
        {
            if (mServerReactionStopwatch != null)
            {
                mServerReactionStopwatch.Stop();
                ServerReaction = mServerReactionStopwatch.Elapsed;
            }

            StopCheckConnection();

            var message = MessagePackDeserializer.Parse(raw_data_buffer) as SnipeObject;

            if (message != null)
            {
                string      message_type  = message.SafeGetString("t");
                string      error_code    = message.SafeGetString("errorCode");
                int         request_id    = message.SafeGetValue <int>("id");
                SnipeObject response_data = message.SafeGetValue <SnipeObject>("data");

                DebugLogger.Log($"[SnipeClient] [{ConnectionId}] ProcessMessage - {request_id} - {message_type} {error_code} {response_data?.ToJSONString()}");

                if (!mLoggedIn)
                {
                    if (message_type == SnipeMessageTypes.USER_LOGIN)
                    {
                        if (error_code == SnipeErrorCodes.OK)
                        {
                            DebugLogger.Log($"[SnipeClient] [{ConnectionId}] ProcessMessage - Login Succeeded");

                            mLoggedIn = true;

                            if (response_data != null)
                            {
                                this.ConnectionId = response_data.SafeGetString("connectionID");
                            }
                            else
                            {
                                this.ConnectionId = "";
                            }

                            try
                            {
                                LoginSucceeded?.Invoke();
                            }
                            catch (Exception e)
                            {
                                DebugLogger.Log($"[SnipeClient] [{ConnectionId}] ProcessMessage - LoginSucceeded invokation error: " + e.Message);
                            }

                            if (mHeartbeatEnabled)
                            {
                                StartHeartbeat();
                            }
                        }
                        else
                        {
                            DebugLogger.Log($"[SnipeClient] [{ConnectionId}] ProcessMessage - Login Failed");

                            try
                            {
                                LoginFailed?.Invoke(error_code);
                            }
                            catch (Exception e)
                            {
                                DebugLogger.Log($"[SnipeClient] [{ConnectionId}] ProcessMessage - LoginFailed invokation error: " + e.Message);
                            }
                        }
                    }
                }

                if (MessageReceived != null)
                {
                    try
                    {
                        MessageReceived.Invoke(message_type, error_code, response_data, request_id);
                    }
                    catch (Exception e)
                    {
                        DebugLogger.Log($"[SnipeClient] [{ConnectionId}] ProcessMessage - MessageReceived invokation error: " + e.Message + "\n" + e.StackTrace);
                    }
                }
                else
                {
                    DebugLogger.Log($"[SnipeClient] [{ConnectionId}] ProcessMessage - no MessageReceived listeners");
                }

                if (mHeartbeatEnabled)
                {
                    ResetHeartbeatTimer();
                }
            }
        }