コード例 #1
0
        private void GetThread()
        {
            while (true)
            {
                if (!client.Connected)
                {
                    //Not connected!
                    OnError(RSN_Exception_ErrorType.ConnectionLoss, "Connection to the server was lost.", null);
                }

                try {
                    RSN_Packet packet = RSN_Packet.AwaitPacketOverStream(networkReader);
                    if (packet != null)
                    {
                        //Got packet.
                        OnGotMessage(packet);
                    }
                }
                catch (Exception ex)
                {
                    OnError(RSN_Exception_ErrorType.Exception, "", ex);
                }
            }
        }
コード例 #2
0
        private void OnGotMessage(RSN_Packet packet)
        {
            if (packet.type == RSN_PacketType.AuthFail)
            {
                //Error.
                OnError(RSN_Exception_ErrorType.AuthError, "Token provided wasn't accepted by the server. Maybe you're sending requests before the server has served the token?");
                return;
            }

            try
            {
                switch (packet.type)
                {
                case RSN_PacketType.EncodedMessage:
                    //Find callback
                    RSN_ClientResponse callback = null;
                    try
                    {
                        //Get the callback
                        callback = callbacks[packet.id];
                    }
                    catch
                    {
                        //Wasn't found. Ignore.
                        break;
                    }
                    //Find the data type we should parse this as.
                    Type type = null;
                    try
                    {
                        type = registeredDataTypes[packet.parseType];
                    }
                    catch
                    {
                        //Bad! Ignore
                        return;
                    }
                    Type t = type;
                    //Deserialize
                    object obj = RSN_Tools.DeserializeObject(packet.body, t);
                    callback(obj);
                    break;

                case RSN_PacketType.Auth:
                    //Auth message. Check if login was okay, then set the token or fail
                    RSN_AuthPacketType auth = (RSN_AuthPacketType)RSN_Tools.DeserializeObject(packet.body, typeof(RSN_AuthPacketType));
                    if (auth.wasAuthOkay)
                    {
                        //OK!
                        token  = auth.token;
                        isAuth = true;
                    }
                    else
                    {
                        //Bad...
                        OnError(RSN_Exception_ErrorType.AuthError, "Couldn't be authorized with the server. Check the password.");
                    }
                    break;
                }
            } catch (Exception ex)
            {
                OnError(RSN_Exception_ErrorType.Exception, "Failed to process request. The request goes as follows: '" + packet.body + "'.", ex);
            }
        }