void newConnection(object obj) { NetworkClient soc = (NetworkClient)obj; string ip = ((IPEndPoint)soc.tcpClient.Client.RemoteEndPoint).Address.ToString(); Program.Log("[{0}] New connection.", ip); try { bool NoErrors = true; while (soc.tcpClient.Connected && NoErrors) { Program.Log("[{0}] Waiting for command.", ip); int comandInt = soc.ReadInt32(); IncomingRequestArgs new_args = new IncomingRequestArgs(); new_args.client = soc; new_args.ip = ip; new_args.command = (NetworkCommands)comandInt; NoErrors = IncomingRequest(this, new_args); } } catch (Exception ex) { Program.Log("[{0}] ERROR!!!", ip); Program.Log("[{0}] " + ex.ToString(), ip); if (soc.tcpClient.Connected) { soc.tcpClient.Close(); } } Program.Log("[{0}] Thread stopped", ip); soc.tcpClient.Close(); }
private static bool Tcp_IncomingRequest(object sender, IncomingRequestArgs args) { string ip = args.ip; if (args.command == NetworkCommands.GetFileList) { Program.Log("[{0}] GetFileList", ip); Encoding uni = Encoding.Unicode; try { byte[] data = uni.GetBytes(filelist); args.client.WriteBytes(data); data = uni.GetBytes(sceme); args.client.WriteBytes(data); } catch (Exception ex) { Program.Log("[{0}] ERROR!!!", ip); Program.Log("[{0}] " + ex.ToString(), ip); return(false); } return(true); } if (args.command == NetworkCommands.GetMotd) { Program.Log("[{0}] GetMOTD", ip); Encoding uni = Encoding.Unicode; byte[] data = uni.GetBytes(motd); args.client.WriteBytes(data); return(true); } if (args.command == NetworkCommands.GetFilesReconnect) { FileStream stream = null; try { Program.Log("[{0}] GetFilesReconnect", ip); Program.Log("[{0}] Waiting for id.", ip); int id = args.client.ReadInt32(); Program.Log("[{0}] Waiting for pos.", ip); long pos = args.client.ReadInt64(); string file = f.findById(id); Program.Log("[{0}] Send file {1} from {2}", ip, file, pos); System.IO.FileInfo fi = new System.IO.FileInfo(file); //поменять.... сделать для класс для этого stream = new FileStream(file, FileMode.Open, FileAccess.Read); stream.Seek(pos, SeekOrigin.Begin); args.client.WriteFromStream(stream, fi.Length - pos); stream.Close(); return(true); } catch (Exception ex) { Program.Log("[{0}] ERROR!!!", ip); Program.Log("[{0}] " + ex.ToString(), ip); if (stream != null) { stream.Close(); } return(false); } } if (args.command == NetworkCommands.GetFiles) { Program.Log("[{0}] GetFiles", ip); while (true) { FileStream stream = null; try { Program.Log("[{0}] Waiting for id.", ip); int id = args.client.ReadInt32(); Program.Log("[{0}] Requesting file {1}", ip, Convert.ToString(id)); if (id < 0) { break; } string file = f.findById(id); Program.Log("[{0}] Send file {1}", ip, file); System.IO.FileInfo fi = new System.IO.FileInfo(file); //поменять.... сделать для класс для этого stream = new FileStream(file, FileMode.Open, FileAccess.Read); args.client.WriteFromStream(stream, fi.Length); stream.Close(); } catch (Exception ex) { Program.Log("[{0}] ERROR!!!", ip); Program.Log("[{0}] " + ex.ToString(), ip); if (stream != null) { stream.Close(); } return(false); } } return(true); } if (args.command == NetworkCommands.GetFile2) { FileStream stream = null; try { int id = args.client.ReadInt32(); long pos = args.client.ReadInt64(); string file = f.findById(id); Program.Log("[{0}] Send file {1} from {2}", ip, file, pos); System.IO.FileInfo fi = new System.IO.FileInfo(file); //поменять.... сделать для класс для этого stream = new FileStream(file, FileMode.Open, FileAccess.Read); stream.Seek(pos, SeekOrigin.Begin); args.client.WriteFromStream(stream, fi.Length - pos); stream.Close(); return(true); } catch (Exception ex) { Program.Log("[{0}] ERROR!!!", ip); Program.Log("[{0}] " + ex.ToString(), ip); if (stream != null) { stream.Close(); } return(false); } } if (args.command == NetworkCommands.Disconnect) { Program.Log("[{0}] Disconect.", ip); args.client.tcpClient.Close(); } return(true); }