internal void RequestClientConnectioTo(InProcSocket clientSocket, string destinationAddress) { if (clientSocket.Type != ChannelType.Client) { throw new Exception("Server endpoint cannot establish connections"); } // set client socket state to connectting to destination address clientSocket.RemoteAddress = destinationAddress; clientSocket.ChangeStateTo(ConnectionState.Connecting); // add socket to connecting socket dictionary Network.ConnectingSockets.Add(SocketId.FromSocket(clientSocket), clientSocket); Network.TaskScheduler.SchedluleTask(() => { // remove client socket from connecting socket dictionary // if that operation fails, that means connect operation was cancelled by client if (!Network.ConnectingSockets.Remove(SocketId.FromSocket(clientSocket))) { return; } // Find server by listening channel InProcSocket listeningChannel = null; if (!Network.ListeningSockets.TryGetValue(clientSocket.RemoteAddress, out listeningChannel)) { // if listening socket not found, change client state to connection failed clientSocket.ChangeStateTo(ConnectionState.ConnectionFailed); return; } // Success finding listening socket Network.EstablishedSockets.Add(SocketId.FromSocket(clientSocket), clientSocket); // Create server client channel InProcSocket serverSideChannel = new InProcSocket(Network, ChannelType.Server); serverSideChannel.RemoteAddress = clientSocket.LocalAddress; serverSideChannel.LocalAddress = listeningChannel.LocalAddress; serverSideChannel.ChangeStateTo(ConnectionState.Established); Network.EstablishedSockets.Add(SocketId.FromSocket(serverSideChannel), serverSideChannel); clientSocket.ChangeStateTo(ConnectionState.Established); listeningChannel.ParentServer.AddClientChannel(serverSideChannel); }, TimeSpan.FromMilliseconds(Network.ConnectionEstablishLatency)); }
/// <summary> /// Called by socket Close functiion /// </summary> /// <param name="closingChannel"></param> internal void SocketClosingConnection(InProcSocket closingChannel) { Network.ConnectingSockets.Remove(SocketId.FromSocket(closingChannel)); if (closingChannel.State == ConnectionState.Closed) { throw new Exception("Only connected socket can be closed"); } else if (closingChannel.State != ConnectionState.Established) { closingChannel.ChangeStateTo(ConnectionState.Closed); } else { closingChannel.ChangeStateTo(ConnectionState.Closing); if (closingChannel.ParentServer != null) { closingChannel.ParentServer.RemoveClientChannel(closingChannel); } Network.EstablishedSockets.Remove(SocketId.FromSocket(closingChannel)); Network.TaskScheduler.SchedluleTask(() => { // find socket associated with closing one InProcSocket remoteChannel = null; if (!Network.EstablishedSockets.TryGetValue(SocketId.RemoteSocketId(closingChannel), out remoteChannel)) { return; } closingChannel.ChangeStateTo(ConnectionState.Closed); remoteChannel.ChangeStateTo(ConnectionState.Closed); // if remote socket belongs to server if (remoteChannel.ParentServer != null) { remoteChannel.ParentServer.RemoveClientChannel(remoteChannel); } Network.EstablishedSockets.Remove(SocketId.FromSocket(remoteChannel)); }, TimeSpan.FromMilliseconds(Network.ConnectionCloseLatency)); } }