Exemplo n.º 1
0
 internal void RaiseMessageReceived(InProcSocket socket, object message)
 {
     if (MessageReceived != null)
     {
         MessageReceived(this, new MessageReceivedArgs(socket, message));
     }
 }
Exemplo n.º 2
0
 internal void AddClientChannel(InProcSocket newClient)
 {
     if (ClientConnected != null)
     {
         ClientConnected(this, newClient);
     }
     ClientChannels.Add(newClient);
     newClient.ParentServer = this;
 }
Exemplo n.º 3
0
 internal void RemoveClientChannel(InProcSocket exisitingClient)
 {
     if (ClientDisconnected != null)
     {
         ClientDisconnected(this, exisitingClient);
     }
     exisitingClient.ParentServer = null;
     if (!ClientChannels.Remove(exisitingClient))
     {
         throw new Exception("Failed to remove socket from server client list: " + exisitingClient.ToString());
     }
 }
Exemplo n.º 4
0
        public bool StartListening(string address)
        {
            Address = address;
            var listeningChannel = new InProcSocket(Network, ChannelType.Listening)
            {
                LocalAddress = address
            };

            listeningChannel.ParentServer = this;
            Network.RegisterListeningEndpoint(address, listeningChannel);
            ListeningChannel = listeningChannel;
            return(true);
        }
Exemplo n.º 5
0
        internal void RegisterListeningEndpoint(string addres, InProcSocket channel)
        {
            if (channel.Type != ChannelType.Listening)
            {
                throw new Exception("Only listening socket can be registered");
            }
            InProcSocket existingServer = null;

            if (ListeningSockets.TryGetValue(addres, out existingServer))
            {
                throw new Exception(string.Format("Address {0} already in use"));
            }

            ListeningSockets.Add(addres, channel);
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        /// <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));
            }
        }
Exemplo n.º 8
0
        internal bool SocketSendMessage(InProcSocket socket, object message)
        {
            if (socket.State != ConnectionState.Established)
            {
                throw new Exception("Only connected sockets can send messages");
            }
            InProcSocket destinationSocket = null;

            if (!Network.EstablishedSockets.TryGetValue(SocketId.RemoteSocketId(socket), out destinationSocket))
            {
                throw new Exception("Failed to send message, destination socket not found");
            }

            Network.TaskScheduler.SchedluleTask(() =>
            {
                destinationSocket.RaiseMessageReceived(message);
            }, TimeSpan.FromMilliseconds(Network.GetConnectionDelay(socket, destinationSocket)));
            return(true);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Get id of associated remote socket [remote_address, local_address]
 /// </summary>
 /// <param name="socket"></param>
 /// <returns></returns>
 public static SocketId RemoteSocketId(InProcSocket socket)
 {
     return(new SocketId(socket.RemoteAddress, socket.LocalAddress));
 }
Exemplo n.º 10
0
 internal int GetConnectionDelay(InProcSocket source, InProcSocket destination)
 {
     return(ConnectionDefaultLatency);
 }