public static UInt32 InitConnection(Socket socket, Command action) { int i = 0; socket.ReceiveTimeout = 100; while (i < 20) { CommunicationFacade.Send(socket, new CommunicationPacket(0, 0, 0, (byte)Flag.SYN, new byte[] { (byte)action })); try { while (true) { CommunicationPacket recived = CommunicationFacade.Receive(socket); if (recived.Flags == (byte)Flag.SYN && recived.Data[0] == (byte)action && recived.SerialNumber == 0 && recived.ConfirmationNumber == 0) { Logger.WriteLine($"Connection established - communication {recived.ConnectionNumber:X}"); socket.ReceiveTimeout = 0; return(recived.ConnectionNumber); } else { Console.WriteLine("Data obtained before connection packet received, ignoring"); } } } catch (SocketException e) when(e.SocketErrorCode == SocketError.TimedOut) { Logger.WriteLine($"Connection timeouted, attemp number {i + 1}"); i++; } } throw new Exceptions.MaximumAttempException(); }
static private void ReceiveThread(object Param) { Logger.WriteLine($"Receive thread started with id {Task.CurrentId}"); SharedObject data = (SharedObject)Param; while (!data.Ended) { data.Socket.ReceiveTimeout = (int)PacketsProps.WAIT_TIME; try { CommunicationPacket p = CommunicationFacade.Receive(data.Socket); UInt64 currentPoint; lock (data.CountersLocker) currentPoint = data.Confirmed; UInt64 confirmationNumber = CommunicationFacade.ComputeRealNumber(p.ConfirmationNumber, currentPoint, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); UploadRecvPacket recv = new UploadRecvPacket(p.ConnectionNumber, p.Flags, p.Data, confirmationNumber); lock (data.ArriveQueue) data.ArriveQueue.Enqueue(recv); } catch (SocketException e) when(e.SocketErrorCode == SocketError.TimedOut) { } } }
public void AcceptFile() { try { byte[] empty = new byte[] { }; IPriorityQueue <DownloadPacket, UInt64> queue = new SimplePriorityQueue <DownloadPacket, UInt64>(); //TODO add priority queue while (true) { DownloadPacket pack = this.receive(CommunicationFacade.Receive(this.socket)); pack = this.validatePacket(pack); if (pack.Flags == (byte)Flag.FIN) { Logger.WriteLine("All data arrive", ConsoleColor.Cyan); CommunicationFacade.Send(this.socket, new CommunicationPacket(this.connectionNumber, 0, Convert.ToUInt16(this.required & UInt16.MaxValue), (byte)Flag.FIN, empty)); return; } queue.Enqueue(pack, pack.SerialNumber); //Attach into priority queue while (queue.Count > 0 && queue.First.SerialNumber <= this.required) { DownloadPacket toProccess = queue.Dequeue(); if (toProccess.SerialNumber < this.required) { continue; } Logger.WriteLine($"Accepted packet {toProccess.SerialNumber}"); this.outFile.Write(toProccess.Data); this.required += (uint)toProccess.Data.Length; if (toProccess.Data.Length != 255) { Logger.WriteLine("Last packet arrive, waiting to FIN packet", ConsoleColor.Cyan); this.waitingToFin = true; } } Logger.WriteLine($"Waiting for packet {this.required}"); CommunicationFacade.Send(this.socket, new CommunicationPacket(this.connectionNumber, 0, Convert.ToUInt16(this.required & UInt16.MaxValue), 0, empty)); } } catch (CommunicationException) { Logger.WriteLine("Occurs error during communication", ConsoleColor.Yellow); throw new TerminateException(); } catch (InvalidPacketException e) { Logger.WriteLine($"Obtained invalid packet: {e.Message}", ConsoleColor.Yellow); CommunicationFacade.Send(this.socket, new CommunicationPacket(this.connectionNumber, 0, Convert.ToUInt16(this.required & UInt16.MaxValue), (byte)Flag.RST, new byte[] { })); throw new TerminateException(); } }