public void Register() { if (++countModels == 2 && client.Connected) { Task t = new Task(() => { Mutex mut = new Mutex(); while (true) { string JCommand = reader.ReadString(); CommandRecievedEventArgs command = CommandRecievedEventArgs.FromJSON(JCommand); if (command.CommandID == (int)CommandEnum.CloseClientCommand) { client.Close(); break; } mut.WaitOne(); CommandReceived?.Invoke(this, command); mut.ReleaseMutex(); } } ); t.Start(); } }
public void GetCommands() { Task t = new Task(() => { while (true) { string JCommand = reader.ReadString(); CommandRecievedEventArgs command = CommandRecievedEventArgs.FromJSON(JCommand); if (command.CommandID == (int)CommandEnum.CloseClientCommand) { client.Close(); break; } if (command.CommandID == (int)CommandEnum.LogChangedCommand) { logs.log.Insert(0, MessageRecievedEventArgs.FromJSON(command.Args[0])); continue; } if (command.CommandID == (int)CommandEnum.LogHistoryCommand) { foreach (MessageRecievedEventArgs log in MessageRecievedEventArgs.LogFromJSON(command.Args[0])) { logs.log.Insert(0, log); } continue; } if (command.CommandID == (int)CommandEnum.HandlerRemoveCommand) { handlers.Remove(command.Args[0]); continue; } if (command.CommandID == (int)CommandEnum.ConfigCommand) { config = (Config.FromJSON(command.Args[0])); JObject cnJson = JObject.Parse(command.Args[0]); JArray JHandler = (JArray)cnJson["Handler"]; foreach (JToken obj in JHandler.Children()) { handlers.Add(obj.ToString()); } continue; } } }); t.Start(); }
public void HandleClient(TcpClient client, List <TcpClient> clients) { Task task = new Task(() => { bool success; NetworkStream stream = client.GetStream(); BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, true); BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, true); while (true) { try { string commandLine = reader.ReadString(); m_log.Log(commandLine, MessageTypeEnum.FAIL); CommandRecievedEventArgs cmdArgs = CommandRecievedEventArgs.FromJSON(commandLine.ToString()); if (cmdArgs.CommandID == (int)CommandEnum.CloseClientCommand) { // if the client wants to disconnect clients.Remove(client); client.Close(); } // execute the command if (cmdArgs.CommandID == (int)CommandEnum.StartImageTransfer) { m_log.Log("entered", MessageTypeEnum.INFO); string name = cmdArgs.Args[0]; string image = ""; clients.Remove(client); while (true) { string input = reader.ReadString(); m_log.Log(input, MessageTypeEnum.FAIL); try { CommandRecievedEventArgs finishTransfer = CommandRecievedEventArgs.FromJSON(input.ToString()); if (finishTransfer.CommandID == (int)CommandEnum.FinishImageTransfer) { ExecuteCommand(finishTransfer.CommandID, new string[] { image, name }, out success); break; } } catch (Exception) { image += input; } } continue; } string cmdOut = ExecuteCommand(cmdArgs.CommandID, cmdArgs.Args, out success); // write output of command to client writer.Write(cmdOut); } catch (JsonException) { m_log.Log(DateTime.Now.ToString() + " Caught JSON Exception in client", MessageTypeEnum.WARNING); writer.Close(); reader.Close(); return; } catch (Exception) { m_log.Log(DateTime.Now.ToString() + " I/O Exception Caught in client", MessageTypeEnum.WARNING); writer.Close(); reader.Close(); return; } } }); task.Start(); }
public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Any, Port); listener = new TcpListener(ep); listener.Start(); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); logger.Log("Image broadcaster connected", MessageTypeEnum.INFO); NetworkStream stream = client.GetStream(); BinaryReader reader = new BinaryReader(stream); while (true) { // Handle client byte[] rcvLenBytes = new byte[4]; reader.Read(rcvLenBytes, 0, 4); int rcvLen = System.BitConverter.ToInt32(rcvLenBytes, 0); byte[] rcvBytes = new byte[rcvLen]; reader.Read(rcvBytes, 0, rcvLen); String rcv = System.Text.Encoding.ASCII.GetString(rcvBytes); CommandRecievedEventArgs cmdArgs = CommandRecievedEventArgs.FromJSON(rcv); if (cmdArgs.CommandID == (int)CommandEnum.StartImageTransfer) { string name = cmdArgs.Args[0]; // read image and save it byte[] inputLenBytes = new byte[4]; reader.Read(inputLenBytes, 0, 4); int inputLen = System.BitConverter.ToInt32(inputLenBytes, 0); // loop to recieve all packets // the amount of bytes is too big to fit in one packet // so the image spreads out across multiple ones byte[] inputBytes = new byte[inputLen]; int i = 0; do { byte[] rbytes = new byte[inputLen - i]; int numRead = reader.Read(rbytes, 0, inputLen - i); for (int j = i; j < i + numRead; j++) { inputBytes[j] = rbytes[j - i]; } i += numRead; }while (i != inputLen); // save image System.IO.File.WriteAllBytes(Path.Combine(serv.pathList[0], name), inputBytes); reader.Read(inputLenBytes, 0, 4); inputLen = System.BitConverter.ToInt32(inputLenBytes, 0); inputBytes = new byte[inputLen]; reader.Read(inputBytes, 0, inputLen); String input = System.Text.Encoding.ASCII.GetString(inputBytes); // file transfer finished CommandRecievedEventArgs finishTransfer = CommandRecievedEventArgs.FromJSON(input.ToString()); if (finishTransfer.CommandID == (int)CommandEnum.FinishImageTransfer) { continue; } } } } catch (SocketException) { logger.Log(DateTime.Now.ToString() + " Socket Exception ", MessageTypeEnum.FAIL); } catch (Exception e) { logger.Log("Exception in Receiver" + e.ToString(), MessageTypeEnum.FAIL); } } }); task.Start(); }