/// <summary>
 ///We will get a commannd for the client and by the command know what to do
 ///The format is commandid#string for example let suppose 1 is for sending config it will be 1x where x is the json string of the object.
 /// </summary>
 /// <param name="client"> The client</param>
 /// <param name="locker">The object we want to lock </param>
 public void HandleClient(TcpClient client, object locker)
 {
     
     //creating a task that will handle the client's commands.
     Task t = new Task(() =>
     {
         NetworkStream stream = client.GetStream();
         BinaryReader reader = new BinaryReader(stream);
         BinaryWriter writer = new BinaryWriter(stream);
         while (true)
         {
             //Getting the command.
             //readMutex.WaitOne();
             string commandLine = reader.ReadString();
            // readMutex.ReleaseMutex();
             Console.WriteLine("Got input: {0}", commandLine);
             //Getting id of command
             int id = int.Parse(commandLine[0] + "");
             string result = ExecuteCommand(commandLine, client);
             Console.WriteLine("Write the result");
             Console.WriteLine("Im here for  " + commandLine);
             //Sending to the client the result - false because we handle specific client
             MessageToClient message = new MessageToClient(id, result,false);
             //Locking this critical place
             lock (locker)
             {
                 writer.Write(message.ToJSON());
             }
         }
         //client.Close();
     });
     t.Start();
 }
示例#2
0
        /// <summary>
        /// sending messages to all the clients.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="content"></param>
        /// <param name="client"></param>
        /// <param name="allClients"></param>
        public void SendMessage(int type, string content, TcpClient client, bool allClients)
        {
            NetworkStream stream = client.GetStream();
            BinaryReader  reader = new BinaryReader(stream);
            BinaryWriter  writer = new BinaryWriter(stream);
            //Convert to type of message
            MessageToClient message = new MessageToClient(type, content, allClients);

            //Locking this critical place
            lock (locker)
            {
                writer.Write(message.ToJSON());
            }
        }