/// <summary> /// Sends a text message or a link to the end point /// </summary> /// <param name="message"></param> /// <param name="endPoint"></param> public static void SendTextMessage(string message, IPEndPoint endPoint) { int s = 2; TextTransferPacket ttp = new TextTransferPacket(message); NetComm.SendByteArray(ttp.ToByteArray(), endPoint); }
/// <summary> /// Sends the information about the device to the endpoint requesting it. Follows a broadcast request /// </summary> /// <param name="ep"></param> private static void SendScoutingInformation(IPEndPoint ep) { ScoutResponsePacket packet = new ScoutResponsePacket(new NetworkDevice() { Name = ProgramSettings.DeviceName, Type = ProgramSettings.DeviceType }); NetComm.SendByteArray(packet.ToByteArray(), ep); }
public void Start() { timer.Start(); IsRunning = true; // First we load the file LoadFile(); // Sending the request packet TransferRequestPacket trp = new TransferRequestPacket(SelectedFileStructure, LocalTransferID); NetComm.SendByteArray(trp.ToByteArray(), TargetEndPoint); // Update the state ProcessState = SendProcessState.SentTransferRequest; ProcessThread = new Thread(MainLoop); ProcessThread.Start(); }
private static void ProcessFileTransferRequest(IPEndPoint ep, TransferRequestPacket packet) { LocalFileStructure fStruct = packet.GetFileStructure(); if (fStruct == null) { // Send error return; } // Check that its ok //if(fStruct.FileSize > 500000000) // Refuse if over 500mb //{ // // Send transfer refusal // return; //} // set up int slot = GetAviableInboundTransferProcessID(); if (slot == -1) { // Send transfer refusal. No slots aviable return; } // Create a new inbound process ActiveInboundTransferProcesses[slot] = new InboundTransferProcess(packet.GetSenderTransferID(), fStruct, ep, _fileHandler); ActiveInboundTransferProcesses[slot].OnTransferFinished += InboundTransferFinished; // Send Transfer acceptal TransferAcceptPacket ta = new TransferAcceptPacket(slot, packet.GetSenderTransferID()); NetComm.SendByteArray(ta.ToByteArray(), ep); }
/// <summary> /// Receives and processes an INetworkPacket packet /// </summary> /// <param name="packet"></param> public void ReceivePacket(INetworkPacket packet) { switch (packet) { case DataCarryPacket dcp: //using (FileStream fStream = new FileStream(TempFilePath, FileMode.Append, FileAccess.Write)) //{ // fStream.Write(packet.PacketData, 0, packet.PacketData.Length); //} fStream.Write(packet.PacketData, 0, packet.PacketData.Length); //using (FileStream fStream = File.Open(TempFilePath, FileMode.OpenOrCreate)) //{ // fStream.Seek(0, SeekOrigin.End); // fStream.Write(packet.PacketData, 0, packet.PacketData.Length); //} // Send the ack packet back AcknowledgePacket ack = new AcknowledgePacket(dcp.PacketNumber, TransferID); NetComm.SendByteArray(ack.ToByteArray(), SenderEndPoint); // If it's marked as last, process the file if (dcp.IsLast) { //ReceivedChunks.Add(new DataChunk(dcp.PacketNumber, dcp.PacketData)); //ProcessFinishedFile(); ProcessFinishedFileStream(); } else { //ReceivedChunks.Add(new DataChunk(dcp.PacketNumber, dcp.PacketData)); } break; } }
/// <summary> /// Sends a packet with the new chunk /// </summary> private void SendChunk() { // Update progress //decimal pp = (decimal)ReadIndex / (decimal)FileBuffer.Length; decimal pp = (decimal)ReadIndex / (decimal)SelectedFileStructure.FileSize; int percent = (int)Math.Round(pp * 100); if (percent > DownloadProgressNextTick) { FileTransferProgressEventArgs args = new FileTransferProgressEventArgs(); args.PercentProgress = percent; args.ID = LocalTransferID; args.FileName = SelectedFileStructure.FullName; OutTransferProgress?.Invoke(null, args); DownloadProgressNextTick = percent + DownloadProgressTreshold; } DataCarryPacket dcp = CreateDataPacket(); NetComm.SendByteArray(dcp.ToByteArray(), TargetEndPoint); LastActionSnapshot = timer.ElapsedMilliseconds; //DataCarryPacket dcp = new DataCarryPacket(NextBlockNumber, TransferID, ) }
/// <summary> /// Sends an immediate termination request to the target device's process /// </summary> /// <param name="ep"></param> /// <param name="id"></param> private static void SendTransferTermination(IPEndPoint ep, int id) { TransferTerminationPacket ttp = new TransferTerminationPacket(id); NetComm.SendByteArray(ttp.ToByteArray(), ep); }