public void SendText(string addr, string text) { //aSocket?.Cancel(); Message message = new Message { Text = text }; try { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(addr), ReceiverPort); UdpSend(remoteEP, Message.CreateTextMeta(text.Length)); remoteEP.Port = TransferPort; TcpSetupStream(remoteEP, ns => { byte[] bs = message.ToBytes(); ns.Write(bs, 0, bs.Length); }); OnTransferDone?.Invoke(new State(ActionCode.TextSend, StateCode.Success, "")); } catch (OperationCanceledException) { OnTransferError?.Invoke(new State(ActionCode.TextSend, StateCode.Error, "Confirm Addr Timeout")); } catch (SocketException e) { OnTransferError?.Invoke(new State(ActionCode.TextSend, StateCode.Error, e.Message)); throw e; } }
public async void SendFileAsync(string addr, string filepath) { Message meta = Message.CreateMeta(filepath, PackSize); string status = ""; long continueId = 0; try { status = "Wait Confirmation"; IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(addr), ReceiverPort); UdpSend(remoteEP, meta); await WaitMessage( msg => msg.Type == MsgType.Continue, (remoteEP, msg) => continueId = msg.PackID); if (continueId == -1) { OnTransferError?.Invoke(new State(ActionCode.FileSend, StateCode.Error, "Request rejected")); return; } remoteEP.Port = TransferPort; status = "Transferring"; TcpSetupStream(remoteEP, ns => { using FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); byte[] bs = null; Message dataMsg = new Message { PackID = continueId, Data = new byte[PackSize] }; fs.Seek((continueId - 1) * PackSize, SeekOrigin.Begin); while (fs.Length - fs.Position > PackSize) { fs.Read(dataMsg.Data, 0, PackSize); bs = dataMsg.ToBytes(); ns.Write(bs, 0, bs.Length); ns.Flush(); dataMsg.PackID++; OnPackTransfered?.Invoke((fs.Position / fs.Length)); } fs.Read(dataMsg.Data, 0, (int)(fs.Length - fs.Position)); bs = dataMsg.ToBytes(); ns.Write(bs, 0, bs.Length); ns.Flush(); }); OnTransferDone?.Invoke(new State(ActionCode.FileSend, StateCode.Success, "")); } catch (OperationCanceledException) { OnTransferError?.Invoke(new State(ActionCode.FileSend, StateCode.Error, status + " Timeout")); } catch (SocketException e) { OnTransferError?.Invoke(new State(ActionCode.FileSend, StateCode.Error, e.Message)); } }
public void StartWorking() { try { while (true) { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, SenderPort); Message meta = null; bool isText = false; UdpReceive(ref remoteEP, msg => { Console.WriteLine(msg); return(msg.Type != MsgType.Invalid); }, msg => { if (msg.Type == MsgType.Meta) { if (OnReceivedRequest(msg, Message.IsText(msg))) { meta = msg; isText = Message.IsText(msg); } else { UdpSend(remoteEP, new Message { PackID = -1 }); } } }); if (meta != null) { if (!isText) { ReceiveFile(remoteEP, meta); OnTransferDone?.Invoke(new State(ActionCode.FileReceive, StateCode.Success, meta.Filename)); } else { TcpAcceptStream(ns => { byte[] bs = new byte[meta.Size + 1]; ns.Read(bs, 0, (int)meta.Size + 1); string txt = Message.Parse(bs).Text; //Console.WriteLine(txt); OnTransferDone?.Invoke(new State(ActionCode.TextReceive, StateCode.Success, txt)); }); } } } } catch (ChecksumMismatchException e) { OnTransferError?.Invoke(new State(ActionCode.FileCheck, StateCode.Error, e.Message)); } }