コード例 #1
0
        //creates a JsonCommand obj by a string
        public static JsonCommand GenerateJsonCommandObject(string json)
        {
            JObject     jsonObj = JObject.Parse(json);
            JsonCommand jc      = new JsonCommand((int)jsonObj["commandID"], jsonObj["args"].ToObject <string[]>(), (bool)jsonObj["result"], (string)jsonObj["jsonData"]);

            return(jc);
        }
コード例 #2
0
        //the func sends a command to all of the clients it is comunicating with
        public void SendCommandToAllClients(int commandID, string[] args)
        {
            JsonCommand command = new JsonCommand(commandID, args, false, ""); //result and jsonData are irrelevant now

            foreach (BinaryWriter writer in this.writers)
            {
                writer.Write(JsonConvertor.GenerateJsonCommandString(command));
            }
        }
コード例 #3
0
        //creates a string to represent a jsoncommand obj
        public static string GenerateJsonCommandString(JsonCommand jc)
        {
            JObject json = new JObject();

            json["commandID"] = jc.CommandID;
            json["args"]      = new JArray(jc.Args);
            json["result"]    = jc.Result;
            json["jsonData"]  = jc.JsonData;
            return(json.ToString());
        }
コード例 #4
0
        //this function is handeling the client
        public void HandleClient(TcpClient client)
        {
            NetworkStream stream = client.GetStream();
            BinaryReader  reader = new BinaryReader(stream);
            BinaryWriter  writer = new BinaryWriter(stream);

            //creates a new task to listen to the client's requests
            new Task(() =>
            {
                this.writers.Add(writer);
                string commandLine = reader.ReadString();
                bool result;
                JsonCommand command = JsonConvertor.GenerateJsonCommandObject(commandLine);

                try
                {
                    while (command.CommandID != (int)CommandsEnum.CloseCommand)
                    {
                        string message   = this.controller.ExecuteCommand(command.CommandID, command.Args, out result);
                        command.JsonData = message;
                        command.Result   = result;
                        writer.Write(JsonConvertor.GenerateJsonCommandString(command));

                        commandLine = reader.ReadString();
                        command     = JsonConvertor.GenerateJsonCommandObject(commandLine);
                    }
                }
                catch (Exception e)
                {
                    this.writers.Remove(writer);
                    return;
                }
                this.writers.Remove(writer);
                client.Close();
            }).Start();
        }
コード例 #5
0
 public CommandReceivedEventArgs(JsonCommand command)
 {
     this.JsonCommand = command;
 }
コード例 #6
0
        //this func sends a command to the server, result will come later in CommandReceived
        public void sendCommand(int commandID, string[] args)
        {
            JsonCommand command = new JsonCommand(commandID, args, false, "");

            this.writer.Write(JsonConvertor.GenerateJsonCommandString(command));
        }