Example #1
0
        private void CreateRequest(RequestType type, string strOne, string strTwo, int intOne, int intTwo, RSN_ClientResponse callback = null, ArkIOInterface_SavedContext context = null)
        {
            ArkIOInterfaceRequestData data = new ArkIOInterfaceRequestData();

            data.strArgOne = strOne;
            data.strArgTwo = strTwo;
            data.intArgOne = intOne;
            data.intArgTwo = intTwo;
            data.type      = type;
            if (callback == null)
            {
                callback = new RSN_ClientResponse(Callback);
            }
            int id = client.SendData(callback, data);

            if (context != null)
            {
                savedContexts.Add(id, context);
            }
        }
Example #2
0
 public int SendData <T>(RSN_ClientResponse callback, T data)
 {
     try
     {
         int msgId = currentMessage;
         currentMessage++;
         //Add the callback to the dictonary
         callbacks.Add(msgId, callback);
         //serialize data
         string dataString = RSN_Tools.SerializeObject(data);
         int    type       = 0;
         try
         {
             //Reverse lookup the dictonary so we know what type this is.
             type = registeredDataTypes.FirstOrDefault(x => x.Value == data.GetType()).Key;
         }
         catch
         {
             //Bad type.
             throw new Exception("Bad type when sending data! Make sure it is registered.");
         }
         if (type == 0)
         {
             throw new Exception("Bad type when sending data! Make sure it is registered.");
         }
         //Create a packet
         RSN_Packet packet = new RSN_Packet(token, RSN_PacketType.EncodedMessage, msgId, dataString, type);
         //Submit
         RawWrite(packet.EncodePacket());
         //return id
         return(msgId);
     } catch (Exception ex)
     {
         OnError(RSN_Exception_ErrorType.Exception, "", ex);
         return(-1);
     }
 }
Example #3
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);
            }
        }