Beispiel #1
0
        public void DisconnectFromARemoteEndPoint()
        {
            using (var host = new LocalDriverHelper(default(NetworkEndPoint)))
            {
                host.Host();
                var driver = new NetworkDriver(new IPCNetworkInterface(), new NetworkDataStreamParameter {
                    size = 64
                });

                // Need to be connected in order to be able to send a disconnect packet.
                NetworkConnection connectionId = driver.Connect(host.EndPoint);
                Assert.True(connectionId != default(NetworkConnection));
                driver.ScheduleUpdate().Complete();

                var local = driver.LocalEndPoint();
                host.Assert_GotConnectionRequest(local, true);

                NetworkConnection con;
                DataStreamReader  slice;
                // Pump so we get the accept message back.
                driver.ScheduleUpdate().Complete();
                Assert.AreEqual(NetworkEvent.Type.Connect, driver.PopEvent(out con, out slice));
                driver.Disconnect(connectionId);
                driver.ScheduleUpdate().Complete();

                host.Assert_GotDisconnectionRequest(local);

                driver.Dispose();
            }
        }
Beispiel #2
0
 public void Disconnect()
 {
     foreach (Client c in ClientList)
     {
         networkDriver.Disconnect(c.Connection);
     }
 }
Beispiel #3
0
    private void UpdateConnections()
    {
        // Clean up Connections.
        for (int i = 0; i < connections.Length; i++)
        {
            if (!connections[i].IsCreated)
            {
                playerInfoByID.Remove(connections[i].InternalId);
                connections.RemoveAt(i);
                i--;
            }
        }

        // Accept new Connections.
        NetworkConnection connection;

        while ((connection = driver.Accept()) != default)
        {
            if (connectionsLength >= maxConnections)
            {
                driver.Disconnect(connection);
            }
            else
            {
                // Send PlayerMessages and a WelcomeMessage to the new connection.
                var playerInfo = new PlayerInfo
                {
                    color = Random.ColorHSV(),
                    name  = "",
                };
                playerInfoByID.Add(connection.InternalId, playerInfo);

                for (int i = 0; i < connectionsLength; i++)
                {
                    var otherConnection  = connections[i];
                    var otherPlayerInfo  = playerInfoByID[otherConnection.InternalId];
                    var newPlayerMessage = new NewPlayerMessage(MessageID.nextID, otherConnection.InternalId, otherPlayerInfo.color, otherPlayerInfo._name);
                    Send(connection, newPlayerMessage);
                }

                var welcomeMessage = new WelcomeMessage(MessageID.nextID, connection.InternalId, playerInfo.color);
                Send(connection, welcomeMessage);

                connections.AddNoResize(connection);
            }
        }
    }
Beispiel #4
0
 /// <summary>
 /// Close an active NetworkConnection, similar to <see cref="Disconnect{T}"/>.
 /// </summary>
 /// <param name="driver">The driver that owns the virtual connection.</param>
 public int Close(NetworkDriver driver)
 {
     if (m_NetworkId >= 0)
     {
         return(driver.Disconnect(this));
     }
     return(-1);
 }
Beispiel #5
0
 public void Execute(Entity entity, int jobIndex, ref NetworkStreamConnection connection, [ReadOnly] ref NetworkStreamRequestDisconnect disconnect)
 {
     driver.Disconnect(connection.Value);
     commandBuffer.AddComponent(jobIndex, entity, new NetworkStreamDisconnected {
         Reason = disconnect.Reason
     });
     commandBuffer.RemoveComponent <NetworkStreamRequestDisconnect>(jobIndex, entity);
 }
Beispiel #6
0
    // Destroy the driver safely.
    public void DestroyDriver()
    {
        if (driver.IsCreated)
        {
            driver.Disconnect(connection);
            connection = default;
            driver.Dispose();

            readMessage = null;
        }
    }
Beispiel #7
0
        public void Disconnect()
        {
            if (State != ClientState.Connected && State != ClientState.Connecting)
            {
                Debug.LogWarning($"Cannot disconnect in client state {State}");
                return;
            }

            if (_clientDriver.IsCreated)
            {
                _clientDriver.Disconnect(_clientToServerConnection);
                SceneManager.sceneLoaded -= OnSceneLoaded;
                Disconnected?.Invoke();
            }

            State = ClientState.Disconnected;
            _clientToServerConnection = default;
            Debug.Log("Disconnected");
        }
Beispiel #8
0
 public override void DisconnectLocalClient()
 {
     _ = Driver.Disconnect(Connections[0]);
 }
Beispiel #9
0
 /// <summary>
 /// Disconnects a virtual connection and marks it for deletion. This connection will be removed on next the next frame.
 /// </summary>
 /// <param name="driver">The driver that owns the virtual connection.</param>
 public int Disconnect(NetworkDriver driver)
 {
     return(driver.Disconnect(this));
 }
Beispiel #10
0
 public void Disconnect()
 {
     networkDriver.Disconnect(connection);
 }
Beispiel #11
0
 private void OnApplicationQuit()
 {
     driver.Disconnect(connection);
 }
Beispiel #12
0
    void Update()
    {
        // Update Network Driver.
        Driver.ScheduleUpdate().Complete();

        // Check connection to server.
        if (!Connection.IsCreated)
        {
            if (!Done)
            {
                Debug.LogWarning("Something went wrong trying to connect to the server.");
            }
            return;
        }

        // Pop event for this connection.
        DataStreamReader stream;

        NetworkEvent.Type cmd;
        while ((cmd = Connection.PopEvent(Driver, out stream)) != NetworkEvent.Type.Empty)
        {
            // On a connect event, write and send number to the sever.
            if (cmd == NetworkEvent.Type.Connect)
            {
                Debug.Log("Succesfully connected to the server!");
                SendActionToServer(Main.Instance.ConcatInt((int)DataCodes.USERINFO, Main.Instance.CurrentUser.UserID));

                Connected = true;
            }

            // On a data event, display recieved number form server.
            else if (cmd == NetworkEvent.Type.Data)
            {
                uint dataCode = stream.ReadUInt();
                switch (dataCode)
                {
                case (uint)DataCodes.LOGIN_ERROR:
                    Driver.Disconnect(Connection);
                    Main.Instance.Web.ServerMessagesText.SetText("Something went wrong trying to log in. Please try again.");
                    SceneManager.LoadScene("Login");
                    break;

                case (uint)DataCodes.DEBUG_MESSAGE:
                    Debug.Log("Debug message recieved!");
                    break;

                case (uint)DataCodes.PING:
                    break;

                case (uint)DataCodes.ASSIGN_P1:
                    PlayerNum = 1;
                    break;

                case (uint)DataCodes.ASSIGN_P2:
                    PlayerNum = 2;
                    break;

                case (uint)DataCodes.PASS_TURN:
                    GameManager.Instance.Turn = true;
                    break;

                case (uint)DataCodes.START_GAME:
                    Debug.Log("Players ready! Starting game...");
                    GameReady = true;
                    break;

                case (uint)DataCodes.P1_ROUND_WON:
                    GameManager.Instance.AssignScore(DataCodes.P1_ROUND_WON);
                    break;

                case (uint)DataCodes.P1_ROUND_LOST:
                    GameManager.Instance.AssignScore(DataCodes.P1_ROUND_LOST);
                    break;

                case (uint)DataCodes.ROUND_TIE:
                    GameManager.Instance.AssignScore(DataCodes.ROUND_TIE);
                    break;
                }
            }

            // On a disconnect event, display disconnect message and reset connection.
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                Debug.Log("Client got disconnected from the server.");
                Connection = default;
                Connected  = false;
                CancelInvoke();
                SceneManager.LoadScene("Login");
            }
        }
    }
 public void DisconnectPlayer()
 {
     networkJobHandle.Complete();
     PlayerManager.Instance.Players = null;
     networkDriver.Disconnect(connection);
 }