internal void Add(ITcpTransportLayer connection) { lock (_connectionLock) { _localOpenConnectionList.Add(connection.ConnectionId, new TcpConnectionHolder(connection)); _remoteLocalIdMap.Add(connection.RemoteConnectionId, connection.ConnectionId); } }
internal TcpConnectionHolder(ITcpTransportLayer conn) { Connection = conn; SendLock = new Object(); ReceiveLock = new Object(); autoOpenTimer = new Timer(10000); autoOpenTimer.Elapsed += delegate { if (!Connection.Established) { Logger.Debug("Connection timer for " + conn.ConnectionId + " elapsed, auto opening connection " + conn.ConnectionId); Connection.Open(); } }; }
// this is called when a connection closes private void ConnectionOnConnectionClose(object sender, EventArgs eventArgs) { ITcpTransportLayer connection = (ITcpTransportLayer)sender; if (!connection.Disconnected) { // Send the disconnect packet sequence (-> dis <- ack <- dis_rs) _tcpConnectionHelper.DisconnectConnection(connection.ConnectionId, connection.LastSeqSent); } else { SendData(new StandardDisconnectRsPacket(connection.RemoteConnectionId)); } // remove the connection from our list of open connections _tcpConnections.Remove(connection.ConnectionId); }
public ITcpOverUdptSocket OpenConnection(String endPoint, byte protocolId) { lock (_connectLock) { byte connectionId = GetNextConnectionId(); // this method will send the necessary packet requesting connection, handle the response ITcpTransportLayer connection = _tcpConnectionHelper.ConnectNamed(connectionId, endPoint, 10000, protocolId); Logger.Debug("Successfully connected to service " + endPoint + " local connection [" + connectionId + "], remote connection [" + connection.RemoteConnectionId + "]"); // add the connection to our list of active connections _tcpConnections.Add(connection); // notify remote endpoint we are ready to accept and send data on this connection SendData(new StandardConnectRsAckPacket(connectionId)); // set it up to tie up loose ends when the connection dies connection.ConnectionClose += ConnectionOnConnectionClose; // notify the connection it can start connection.Open(); // return the socket to upstream so it can start reading/writing return(connection.Socket); } }
public StandardTcpOverUdptSocket(ITcpTransportLayer connection) { this.Connection = connection; _clientBuffer = new Queue<byte[]>(BufferSize); Closed = false; }
public StandardTcpOverUdptSocket(ITcpTransportLayer connection) { this.Connection = connection; _clientBuffer = new Queue <byte[]>(BufferSize); Closed = false; }
public BufferedTcpOverUdptSocket(ITcpTransportLayer connection) { _connection = connection; clientBuffer = new CircularBuffer<byte>(32768); }
public BufferedTcpOverUdptSocket(ITcpTransportLayer connection) { _connection = connection; clientBuffer = new CircularBuffer <byte>(32768); }
private void ProcessNamedConnectionRequest(StandardNamedConnectRqPacket packet) { StandardNamedConnectRsPacket response = new StandardNamedConnectRsPacket { ConnectionId = packet.ConnectionId }; // By default the conn id going back is the conn id of the request // this will change if the connection is a success, then it will be our conn id. lock (_receiveConnectLock) { // If this connection exists if (_tcpConnections.IsRemoteConnection(packet.ConnectionId)) { Logger.Debug("Request for connection id [" + packet.ConnectionId + "] already established, resending response"); TcpConnectionHolder tcpConnectionHolder = _tcpConnections.GetRemoteConnection(packet.ConnectionId); response.RemoteConnectionId = tcpConnectionHolder.Connection.ConnectionId; response.ProtocolId = tcpConnectionHolder.Connection.ProtocolId; response.Sequence = tcpConnectionHolder.Connection.NextSeqToSend; // we can respond that we are already established here response.Success = true; } else { lock (_listeningNamedTCPEndPoints) { if (_listeningNamedTCPEndPoints.ContainsKey(packet.ConnectionName)) { byte localConnectionId = GetNextConnectionId(); Logger.Debug("Found a listener for " + packet.ConnectionName + " connecting local conn [" + localConnectionId + "] to remote conn [" + packet.ConnectionId + "]"); byte agreedProtocolId = packet.ProtocolId; if (packet.ProtocolId > MaxSupportedProtocolId) { Logger.Debug("They wanted protocol " + packet.ProtocolId + ", but I only support up to " + MaxSupportedProtocolId + ", so I propose it."); agreedProtocolId = MaxSupportedProtocolId; } Logger.Debug("I have agreed to protocol " + agreedProtocolId); response.ProtocolId = agreedProtocolId; response.Sequence = (ushort)(_rand.Next(ushort.MaxValue)); ITcpTransportLayer connection = null; switch (agreedProtocolId) { case (2): { connection = new TcpTransportLayerSlidingWindow(this, localConnectionId, packet.ConnectionId, response.Sequence, packet.Sequence); break; } default: { Logger.Error("Failed to agree on a tcp protocol, I don't support " + agreedProtocolId); break; } } if (connection != null) { // only add the connection if response.Success = _listeningNamedTCPEndPoints[packet.ConnectionName].ConnectCallback(connection.Socket); if (response.Success) { // set it up to tie up loose ends when the connection dies connection.ConnectionClose += ConnectionOnConnectionClose; _tcpConnections.Add(connection); response.RemoteConnectionId = localConnectionId; } } else { response.Success = false; } } else { Logger.Warn("No listener for " + packet.ConnectionName); response.Success = false; } } } } Logger.Debug("Sending connection response : " + response); SendData(response); if (response.Success) { // This sends a positive response, we would like to get an Ack of this connection response // which will open this connection, but should that ack not arrive, let set a timer to automatically // open this connection after 10 seconds // we know that the sender will retry the connection should it not receive our response. _tcpConnections.GetLocalOpenConnection(response.RemoteConnectionId).StartTimerToOpen(); } }