/// <summary> /// HandleClient function. /// handles the client-server communication. /// </summary> /// <param name="client">specified client</param> /// <param name="clients">list of all current clients</param> public void HandleClient(TcpClient client, List <TcpClient> clients) { try { new Task(() => { try { isRunning = true; NetworkStream stream = client.GetStream(); reader = new BinaryReader(stream); writer = new BinaryWriter(stream); while (isRunning) { Console.WriteLine("chbefore reading"); dp.write("chbefore reading"); string commandLine = reader.ReadString(); Logging.Log("ClientHandler got command: " + commandLine, MessageTypeEnum.INFO); dp.write("ClientHandler got command: " + commandLine); CommandRecievedEventArgs commandRecievedEventArgs = JsonConvert.DeserializeObject <CommandRecievedEventArgs>(commandLine); if (commandRecievedEventArgs.CommandID == (int)CommandEnum.CloseClient) { clients.Remove(client); client.Close(); isRunning = false; break; } // Console.WriteLine("Got command: {0}", commandLine); bool r; string result = imageController.ExecuteCommand((int)commandRecievedEventArgs.CommandID, commandRecievedEventArgs.Args, out r); dp.write(result); //Console.WriteLine("chExecutedCommand"+ (int)commandRecievedEventArgs.CommandID); // string result = handleCommand(commandRecievedEventArgs); // Mutex.WaitOne(); lock (mutexWrite) { writer.Write(result); } // Console.WriteLine("chsend"); //Mutex.ReleaseMutex(); } } catch (Exception e) { Console.WriteLine($"excption thrown senderch" + e.Message); clients.Remove(client); Logging.Log(e.ToString(), MessageTypeEnum.ERROR); client.Close(); isRunning = false; } }).Start(); } catch (Exception ex) { Logging.Log(ex.ToString(), MessageTypeEnum.ERROR); isRunning = false; } }
public void Start() { try { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); //Console.WriteLine("Waiting for connections..."); Logging.Log("Waiting for client connections...", MessageTypeEnum.INFO); Task task = new Task(() => { while (true) { try { dp.write("listening"); TcpClient client = listener.AcceptTcpClient(); //client.Close; Console.WriteLine("Got new connection"); dp.write("Got new connection\n"); clients.Add(client); ch.HandleClient(client, clients); } catch (SocketException) { break; } } Logging.Log("Server stopped", MessageTypeEnum.INFO); }); task.Start(); } catch (Exception ex) { Logging.Log(ex.ToString(), MessageTypeEnum.ERROR); } }
//checks if extension exist in extensionsToListen. if yes-use CommandRecievedEventArgs to notice. private void OnChanged(object o, FileSystemEventArgs comArgs) { // debug.write("OnChanged"); string argsFullPath = comArgs.FullPath; Debug_program debug = new Debug_program(); debug.write(comArgs.FullPath); string[] args = { comArgs.FullPath }; string fileExtension = Path.GetExtension(argsFullPath); // debug.write(fileExtension); if (extensionsToListen.Contains("*" + fileExtension)) { CommandRecievedEventArgs commandArgs = new CommandRecievedEventArgs((int)CommandEnum.NewFileCommand, args, fileExtension); OnCommandRecieved(this, commandArgs); } }
/// <summary> /// HandleClient function. /// handles the client-server communication. /// </summary> /// <param name="client">specified client</param> /// <param name="clients">list of all current clients</param> public void HandleClient(TcpClient client, List <TcpClient> clients) { try { new Task(() => { try { isRunning = true; NetworkStream stream = client.GetStream(); reader = new BinaryReader(stream); writer = new BinaryWriter(stream); string[] dirPaths = ConfigurationManager.AppSettings["Handler"].Split(';'); while (isRunning) { int lenName, lenImage; lenName = reader.ReadInt32(); string name = Encoding.Default.GetString(reader.ReadBytes(lenName)); debug.write("got " + name + "\n"); lenImage = reader.ReadInt32();; byte[] imageBytes = reader.ReadBytes(lenImage); debug.write("got " + lenImage + "\n"); Image img = byteArrayToImage(imageBytes); debug.write("converted \n"); img.Save(dirPaths[0] + "/" + name); debug.write("saved " + dirPaths[0] + "/" + name + "\n"); // String[] Args = { name } //System.IO.File.WriteAllBytes(@"C:\image.bmp", imageBytes); /* * * char[] chars = new char[imageBytes.Length / sizeof(char)]; * System.Buffer.BlockCopy(imageBytes, 0, chars, 0, imageBytes.Length); * string str = new string(chars); */ // PhotoPackage photo = new PhotoPackage(name, image); // savePhoto?.invoke(this, photo); // photo.photo?.Dispose(); /* * Console.WriteLine("chbefore reading"); * string commandLine = reader.ReadString(); * Logging.Log("ClientHandler got command: " + commandLine, MessageTypeEnum.INFO); * CommandRecievedEventArgs commandRecievedEventArgs = JsonConvert.DeserializeObject<CommandRecievedEventArgs>(commandLine); * if (commandRecievedEventArgs.CommandID == (int)CommandEnum.CloseClient) * { * clients.Remove(client); * client.Close(); * isRunning = false; * break; * } * // Console.WriteLine("Got command: {0}", commandLine); * bool r; * string result = imageController.ExecuteCommand((int)commandRecievedEventArgs.CommandID, * commandRecievedEventArgs.Args, out r); * //Console.WriteLine("chExecutedCommand"+ (int)commandRecievedEventArgs.CommandID); * // string result = handleCommand(commandRecievedEventArgs); * // Mutex.WaitOne(); * lock (mutexWrite) * { * writer.Write(result); * } * // Console.WriteLine("chsend"); * //Mutex.ReleaseMutex(); */ } } catch (Exception e) { debug.write($"excption thrown senderch" + e.Message); clients.Remove(client); Logging.Log(e.ToString(), MessageTypeEnum.ERROR); client.Close(); isRunning = false; } }).Start(); } catch (Exception ex) { Logging.Log(ex.ToString(), MessageTypeEnum.ERROR); isRunning = false; } }