Example #1
0
        internal void ConnectHost(NetworkServer server, IDataHandler serverDataHandler)
        {
            ThrowIfActive();

            logger.Log("Client Connect Host to Server");
            // start connecting for setup, then "Peer_OnConnected" below will change to connected
            _connectState = ConnectState.Connecting;

            World = server.World;

            // create local connection objects and connect them
            MessageHandler = new MessageHandler(World, DisconnectOnException);
            var dataHandler = new DataHandler(MessageHandler);

            (var clientConn, var serverConn) = PipePeerConnection.Create(dataHandler, serverDataHandler, OnHostDisconnected, null);

            // set up client before connecting to server, server could invoke handlers
            IsLocalClient = true;
            Player        = new NetworkPlayer(clientConn);
            dataHandler.SetConnection(clientConn, Player);
            RegisterHostHandlers();
            InitializeAuthEvents();
            // invoke started event after everything is set up, but before peer has connected
            _started.Invoke();

            // we need add server connection to server's dictionary first
            // then invoke connected event on client (client has to connect first or it will miss message in NetworkScenemanager)
            // then invoke connected event on server

            server.AddLocalConnection(this, serverConn);
            Peer_OnConnected(clientConn);
            server.InvokeLocalConnected();
        }
Example #2
0
        public static (IConnection clientConn, IConnection serverConn) Create(IDataHandler clientHandler, IDataHandler serverHandler, Action clientOnDisconnect, Action serverOnDisconnect)
        {
            var client = new PipePeerConnection();
            var server = new PipePeerConnection();

            client._onDisconnect = clientOnDisconnect;
            server._onDisconnect = serverOnDisconnect;

            client._otherHandler = serverHandler ?? throw new ArgumentNullException(nameof(serverHandler));
            server._otherHandler = clientHandler ?? throw new ArgumentNullException(nameof(clientHandler));

            client._otherConnection = server;
            server._otherConnection = client;

            client.State = ConnectionState.Connected;
            server.State = ConnectionState.Connected;

            client._name = "[Client Pipe Connection]";
            server._name = "[Server Pipe Connection]";

            return(client, server);
        }