Beispiel #1
0
        public static void SendDataToClient(NetworkStream stream, string body, int messageId, int parseType, string token = "                ", RSN_PacketType type = RSN_PacketType.EncodedMessage)
        {
            //Create packet
            RSN_Packet packet = new RSN_Packet(token, type, messageId, body, parseType);

            //Decode
            byte[] data = packet.EncodePacket();
            //Send it over
            stream.Write(data, 0, data.Length);
        }
Beispiel #2
0
            new Dictionary <int, Type>(); //Stores data parse types.

        public static RSN_Client Connect(RSN_Client_CallbackConfig[] callbacks, string password, string ip, Int32 port, RSN_Error onError = null)
        {
            //Create client
            TcpClient client = new TcpClient(ip, port);

            //Create class
            RSN_Client output = new RSN_Client();

            output.client        = client;
            output.stream        = client.GetStream();
            output.networkReader = new BinaryReader(output.stream);
            output.networkWriter = new BinaryWriter(output.stream);
            output.errorCallback = onError;

            //Add data parse types
            foreach (RSN_Client_CallbackConfig c in callbacks)
            {
                if (c.id < 1)
                {
                    throw new Exception("Bad id: Must be greater than zero.");
                }
                output.registeredDataTypes.Add(c.id, c.type);
            }

            //Begin thread
            var getThread = new System.Threading.Thread(new System.Threading.ThreadStart(output.GetThread));

            getThread.Start();

            //Log into the server
            //Create class
            RSN_AuthPacketType authPacket = new RSN_AuthPacketType();

            authPacket.password = password;
            RSN_Packet packet = new RSN_Packet(output.token, RSN_PacketType.Auth, output.currentMessage, RSN_Tools.SerializeObject(authPacket), 0);

            output.currentMessage++;
            //Write
            output.RawWrite(packet.EncodePacket());
            //When we get a response for that, we'll set our token.

            return(output);
        }
Beispiel #3
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);
     }
 }