private void DequeueUpload(FTSocketContainer fTSocket) { if (fTSocket.UploadQueue.Count < 1) { return; } ResourceData resource = ResourceManager.GetResource(fTSocket.UploadQueue.Dequeue()); HandOutFile(fTSocket, resource); }
void HandOutFileCallback(IAsyncResult result) { FTSocketContainer ft = (FTSocketContainer)result.AsyncState; ft.FTSocket.Client.EndSendFile(result); ft.FTSocket.Client.Send(encoding.GetBytes("$End")); ft.Sending = false; DebugLogging("File Uploaded"); DequeueUpload(ft); OnUploadCompleted?.Invoke(ft); }
void AcceptedFileSocketCallback(System.IAsyncResult ar) { DebugLogging("Server: Client FTPSocket Connected"); TcpClient client = (ar.AsyncState as TcpListener).EndAcceptTcpClient(ar); IPAddress address = ((IPEndPoint)client.Client.RemoteEndPoint).Address; FTSocketContainer container = new FTSocketContainer() { FTSocket = client, address = address }; OnUploadCompleted += DequeueUpload; FTSocketsList.Add(container); }
void LaunchNetworkClient() { databuffer = new byte[buffersize]; OwnTcpSocket = new TcpClient(); OwnUdpClient = new UdpClient(UdpPortNum); FTContainer = new FTSocketContainer(); FTContainer.FTSocket = new TcpClient(); ConnectedClients = new List <ClientDataContainer_ForTool>(); try { OwnTcpSocket.BeginConnect(TargetIP, TcpPortNum, ConnectedToServerCallback, 0); } catch { DebugLogging("Couldnt find Server"); return; } LocalInst = this; }
public void HandOutFile(FTSocketContainer client, ResourceData resource) { try { string filename = resource.name + resource.extension; DebugLogging("Start File Upload : " + filename); if (client.Sending) { client.UploadQueue.Enqueue(resource.Id); return; } client.Sending = true; client.FTSocket.Client.Send(encoding.GetBytes("FT:" + filename + "|" + resource.Id + "$")); client.FTSocket.Client.BeginSendFile(resource.path, new AsyncCallback(HandOutFileCallback), client); } catch (Exception ex) { DebugLogging(ex.Message); DebugLogging("Connection Lost."); } }
public void RequestFile(string id, ClientDataContainer_ForTool clientData) { FTSocketContainer FTContainer = FTSocketsList.Find((ft) => ft.address.ToString() == clientData.address.ToString()); if (FTContainer == null) { DebugLogging("FTP Socket not found"); return; } if (FTContainer.Transdata != null) { if (FTContainer.Transdata.Id != id) { FTContainer.FTSocket.Client.Send(encoding.GetBytes("Req:" + id + "$")); } } else { FTContainer.FTSocket.Client.Send(encoding.GetBytes("Req:" + id + "$")); } }
public void ProcessFileTransmissionInfo(byte[] data, FTSocketContainer FTContainer) { if (data == null) { return; } string datastr = encoding.GetString(data); byte[] filedatabuffer; if (datastr.StartsWith("Req:")) { ResourceData resource = ResourceManager.GetResource(datastr.Substring(4, datastr.IndexOf("$") - 4)); if (resource == null) { return; } if (!FTContainer.UploadQueue.Contains(resource.path)) { HandOutFile(FTContainer, resource); } return; } if (datastr.StartsWith("FT:")) { int infodelim = datastr.IndexOf("$"), iddelim = datastr.IndexOf('|'); string filename = datastr.Substring(3, iddelim - 3), Id = datastr.Substring(iddelim + 1, infodelim - iddelim - 1); if (ResourceManager.GetResource(Id) != null) { FTContainer.Transdata = ResourceManager.GetResource(Id); FTContainer.Transdata.ChangeResourcePath(ResourceManager.GetDLResourceFolderPath() + filename, UriKind.Relative); FTContainer.IsReceivingUpdate = true; } else { FTContainer.Transdata = new ResourceData(ResourceManager.GetDLResourceFolderPath() + filename, Id, UriKind.Relative); } string cstr = datastr.Substring(0, infodelim + 1); FTContainer.filestream = new FileStream(FTContainer.Transdata.path, FileMode.OpenOrCreate, FileAccess.Write); int clength = data.Length - encoding.GetByteCount(cstr); if (clength > 0) { filedatabuffer = new byte[clength]; Buffer.BlockCopy(data, encoding.GetByteCount(cstr), filedatabuffer, 0, filedatabuffer.Length); FTContainer.filestream.Write(filedatabuffer, 0, filedatabuffer.Length); } else { filedatabuffer = new byte[0]; } DebugLogging("File Receiving..." + FTContainer.Transdata.name + FTContainer.Transdata.extension + " Id : " + FTContainer.Transdata.Id + "\n Updating = " + FTContainer.IsReceivingUpdate); DebugLogging("Received File Data :" + FTContainer.filestream.Length + " ( +" + FTContainer.filestream.Length); if (datastr.EndsWith("$End")) { FTContainer.filestream.Close(); if (FTContainer.IsReceivingUpdate) { FTContainer.Transdata.IssueFinishedUpdateNotify(); } else { ResourceManager.AddResource(FTContainer.Transdata); } DebugLogging("File Received successfully " + FTContainer.Transdata.name + FTContainer.Transdata.extension + " Id : " + FTContainer.Transdata.Id); OnDownloadCompleted?.Invoke(FTContainer); FTContainer.IsReceivingUpdate = false; FTContainer.Transdata = null; } } else if (datastr.EndsWith("$End")) { if (FTContainer.filestream == null) { DebugLogging("Failed to Receive File " + FTContainer.Transdata.name + FTContainer.Transdata.extension + " Id : " + FTContainer.Transdata.Id); FTContainer.IsReceivingUpdate = false; FTContainer.Transdata = null; return; } int slength = data.Length - encoding.GetByteCount("$End"); if (slength > 0) { filedatabuffer = new byte[slength]; Buffer.BlockCopy(data, 0, filedatabuffer, 0, filedatabuffer.Length); //client.filebuffer = client.filebuffer.Concat(fdata).ToArray(); FTContainer.filestream.Write(filedatabuffer, 0, filedatabuffer.Length); DebugLogging("Received File Data :" + FTContainer.filestream.Length + " ( +" + filedatabuffer.Length); } try { FTContainer.filestream.Close(); if (FTContainer.IsReceivingUpdate) { FTContainer.Transdata.IssueFinishedUpdateNotify(); } else { ResourceManager.AddResource(FTContainer.Transdata); } DebugLogging("File Received successfully " + FTContainer.Transdata.name + FTContainer.Transdata.extension + " Id : " + FTContainer.Transdata.Id); OnDownloadCompleted?.Invoke(FTContainer); FTContainer.IsReceivingUpdate = false; FTContainer.Transdata = null; } catch (Exception ex) { DebugLogging(ex.Message + " " + ex.StackTrace); } } else { if (FTContainer.filestream == null) { FTContainer.IsReceivingUpdate = false; FTContainer.Transdata = null; return; } FTContainer.filestream.Write(data, 0, data.Length); DebugLogging("Received File Data :" + FTContainer.filestream.Length + " ( +" + data.Length); } }