Пример #1
0
        protected override IClientConnection SetupClientConnection(out RSAAsymmetricKey k)
        {
            var c = new NetworkClientConnection(p);

            k = (RSAAsymmetricKey)c.LocalKey;

            return(c);
        }
        internal void Connect()
        {
            IEnumerable <IPAddress> addresses = Dns.GetHostEntry(Host).AddressList;

            // Try to connect to an IP address
            // TODO this might not reconnect to the same IP, break out option to prioritised last connected to.
            // TODO this will always try the same IP address, break out round robin option for load balancing
            this.connection = GetResultOfFirstSuccessfulInvocationOf(addresses, (address) =>
            {
                NetworkClientConnection c = serverGroup.GetConnection(address, Port);

                c.MessageReceived += MessageReceivedHandler;
                c.Disconnected    += DisconnectedHandler;

                c.Connect();

                return(c);
            });

            using (DarkRiftWriter writer = DarkRiftWriter.Create())
            {
                writer.Write(remoteServerManager.ServerID);

                using (Message message = Message.Create((ushort)CommandCode.Identify, writer))
                {
                    message.IsCommandMessage = true;
                    SendMessage(message, SendMode.Reliable);
                }
            }

            EventHandler <ServerConnectedEventArgs> handler = ServerConnected;

            if (handler != null)
            {
                void DoServerConnectedEvent()
                {
                    long startTimestamp = Stopwatch.GetTimestamp();

                    try
                    {
                        handler?.Invoke(this, new ServerConnectedEventArgs(this));
                    }
                    catch (Exception e)
                    {
                        serverConnectedEventFailuresCounter.Increment();

                        // TODO this seems bad, shouldn't we disconenct them?
                        logger.Error("A plugin encountered an error whilst handling the ServerConnected event. The server will still be connected. (See logs for exception)", e);
                    }

                    double time = (double)(Stopwatch.GetTimestamp() - startTimestamp) / Stopwatch.Frequency;

                    serverConnectedEventTimeHistogram.Report(time);
                }

                threadHelper.DispatchIfNeeded(DoServerConnectedEvent);
            }
        }
Пример #3
0
        /// <summary>
        /// Create a new TCP/IPv4 socket that will act as a server.
        /// </summary>
        public ServerSocket()
        {
            this.Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.Port = 0;

            this.Listening = false;

            this.OnConnect    = null;
            this.OnReceive    = null;
            this.OnDisconnect = null;
        }
Пример #4
0
        /// <summary>
        /// Create a new TCP/IPv4 socket that will act as a client.
        /// </summary>
        public ClientSocket()
        {
            this.Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.Sock.ReceiveBufferSize = Kernel.MAX_BUFFER_SIZE;
            this.Sock.SendBufferSize    = Kernel.MAX_BUFFER_SIZE;

            this.Buffer = new Byte[Kernel.MAX_BUFFER_SIZE];

            this.Alive = false;

            this.OnConnect    = null;
            this.OnReceive    = null;
            this.OnDisconnect = null;
        }
        /// <summary>
        ///     Handles a server disconnecting.
        /// </summary>
        /// <param name="lostConnection">The connection that was lost.</param>
        /// <param name="remoteServer">The server that the connection was for.</param>
        /// <param name="exception">The exception that caused the disconnection.</param>
        internal void DisconnectedHandler(NetworkClientConnection lostConnection, UpstreamRemoteServer remoteServer, Exception exception)
        {
            logger.Trace($"Lost connection to server {remoteServer.ID} on {remoteServer.Host}:{remoteServer.Port}.", exception);

            lostConnection.Disconnected = null;
            threadHelper.ExponentialBackoff(
                (context) =>
            {
                logger.Trace($"Reconnecting to server {remoteServer.ID} on {remoteServer.Host}:{remoteServer.Port}. Attempt {context.Tries}.");

                remoteServer.Connect();

                logger.Info($"Reconnected to server {remoteServer.ID} on {remoteServer.Host}:{remoteServer.Port}.");
            },
                reconnectAttempts,
                (lastException) =>
            {
                logger.Warning($"Could not reconnect to server {remoteServer.ID} on {remoteServer.Host}:{remoteServer.Port}.", lastException);

                // Inform the registry plugin. We don't remove this server as that's up to the registry connector to do
                serverRegistryConnectorManager.ServerRegistryConnector.HandleConnectionFailure(remoteServer.ID);
            }
                );
        }
Пример #6
0
        public static void Initialize(ContentManager content, GraphicsDevice device)
        {
            Check.NullArgument (content, "content");
            Check.NullArgument (device, "device");

            TileEngine.TextureManager = new TextureManager(content, device, "Graphics");
            TileEngine.Player = null; // Cannot assign to abstract class, removed player
            TileEngine.ModuleManager = new ModuleManager();
            TileEngine.Configuration = null;
            TileEngine.WorldManager = new WorldManager();
            TileEngine.NetworkManager = new NetworkClientConnection();
            TileEngine.NetworkPlayerCache = new Dictionary<uint, BaseCharacter>();
            TileEngine.GraphicsDevice = device;
        }
Пример #7
0
 private void CloseClientConnection()
 {
     _networkClientConnection?.Close();
     _networkClientConnection = null;
 }
Пример #8
0
 public void Connect(string address)
 {
     CloseServerConnection();
     _networkClientConnection = new NetworkClientConnection(address);
     _networkClientConnection.Connect();
 }
Пример #9
0
        public static ScreenshareClient CreateNew()
        {
            var connection = new NetworkClientConnection(ScreenshareProtocol.Instance);

            return(new ScreenshareClient(connection));
        }