/// <summary> /// Create new instance of the <see cref="TcpListener"/> class. /// </summary> /// <param name="localPort">Local port.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="ArgumentException">Thrown if localPort already exists.</exception> public TcpListener(int localPort) { StateMachine = new Tcp(); StateMachine.rxBuffer = new Queue <TCPPacket>(8); StateMachine.localPort = localPort; if (localPort > 0) { listeners.Add((uint)localPort, this); } }
/// <summary> /// Stop listening for new TCP connections. /// </summary> /// <exception cref="OverflowException">Thrown on fatal error (contact support).</exception> /// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception> /// <exception cref="Exception">Thrown if TcpListener not started.</exception> public void Stop() { if (StateMachine == null) { new Exception("TcpListener is not started."); } if (StateMachine.Status == Status.LISTEN) { Tcp.RemoveConnection(StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address); StateMachine = null; } }
/// <summary> /// Create new instance of the <see cref="TcpClient"/> class. /// </summary> /// <param name="localPort">Local port.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="ArgumentException">Thrown if localPort already exists.</exception> public TcpClient(int localPort) { StateMachine = new Tcp(); StateMachine.rxBuffer = new Queue <TCPPacket>(8); StateMachine.Status = Status.CLOSED; StateMachine.LastSequenceNumber = 0; StateMachine.localPort = localPort; if (localPort > 0) { clients.Add((uint)localPort, this); } }
/// <summary> /// Close connection. /// </summary> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="Exception">Thrown if TCP Status is CLOSED.</exception> public void Close() { if (StateMachine.Status == Status.ESTABLISHED) { StateMachine.SendEmptyPacket(Flags.FIN | Flags.ACK); StateMachine.TCB.SndNxt++; StateMachine.Status = Status.FIN_WAIT1; if (StateMachine.WaitStatus(Status.CLOSED, 5000) == false) { throw new Exception("Failed to close TCP connection!"); } } Tcp.RemoveConnection(StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address); }
/// <summary> /// TCP handler. /// </summary> /// <param name="packetData">Packet data.</param> /// <exception cref="sys.ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="sys.IO.IOException">Thrown on IO error.</exception> /// <exception cref="sys.ArgumentException">Thrown on fatal error (contact support).</exception> /// <exception cref="sys.OverflowException">Thrown if packetData array length is greater than Int32.MaxValue.</exception> internal static void TCPHandler(byte[] packetData) { var packet = new TCPPacket(packetData); if (packet.CheckCRC()) { var connection = Tcp.GetConnection(packet.DestinationPort, packet.SourcePort, packet.DestinationIP, packet.SourceIP); if (connection != null) { connection.ReceiveData(packet); } } else { Global.mDebugger.Send("Checksum incorrect! Packet passed."); } }
/// <summary> /// Create new instance of the <see cref="TcpClient"/> class. /// </summary> /// <param name="stateMachine">Tcp state machine.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="ArgumentException">Thrown if localPort already exists.</exception> internal TcpClient(Tcp stateMachine) { StateMachine = stateMachine; }