Пример #1
0
 public void Dispose()
 {
     if (callbacks != null)
     {
         callbacks.OnDisconnect(this);
         callbacks = null;
         netDriver.Disconnect(this);
     }
 }
Пример #2
0
 public void Dispose()
 {
     if (callbacks != null)
     {
         byte error;
         NetworkTransport.Disconnect(_socketID, _connectionID, out error);
         callbacks.OnDisconnect(this);
         callbacks = null;
         if (connections != null)
         {
             _netDriver.RemoveConnection(connections, _connectionID);
         }
     }
 }
    internal void ConnectionDisposed(SocketNetDriverConnection connection)
    {
        if (connection == _serverConnection)
        {
            try {
                connection.sockets[1].Close();
            } catch (Exception) {}

            _serverConnection = null;
            if ((_clientCallbacks != null) && connection.didHandshake)
            {
                _clientCallbacks.OnDisconnect(connection);
            }
        }
        else
        {
            bool r = _tcpConnections.Remove(connection.tcpEndpoint);
            Assert.IsTrue(r);

            if (connection.udpEndpoint != null)
            {
                r = _udpConnections.Remove(connection.udpEndpoint);
                Assert.IsTrue(r);
            }

            if ((_serverCallbacks != null) && connection.didHandshake)
            {
                _serverCallbacks.OnDisconnect(connection);
            }
        }

        try {
            connection.sockets[0].Close();
        } catch (Exception) { }

        connection.sockets[0] = null;
        connection.sockets[1] = null;
    }
Пример #4
0
    void TickSocket(IntHashtable <UnityNetDriverConnection> connections, INetDriverCallbacks callbacks, int socketID, int reliableChannelID, int unreliableChannelID, byte[] recvBuffer, ref NetIOMetrics reliableChannelMetrics, ref NetIOMetrics unreliableChannelMetrics)
    {
        int  connectionID;
        int  recvSize;
        int  channelID;
        byte error;

        while (true)
        {
            NetworkEventType eventType = NetworkTransport.ReceiveFromHost(socketID, out connectionID, out channelID, recvBuffer, recvBuffer.Length, out recvSize, out error);
            switch (eventType)
            {
            case NetworkEventType.ConnectEvent: {
                var conn = GetConnection(connections, connectionID);
                if (conn == null)
                {
                    conn = CreateConnection(connections, callbacks, socketID, connectionID, reliableChannelID, unreliableChannelID);
                }
                callbacks.OnConnect(conn);
            } break;

            case NetworkEventType.DisconnectEvent: {
                var conn = GetConnection(connections, connectionID);
                if (conn == null)
                {
                    conn = CreateConnection(connections, callbacks, socketID, connectionID, reliableChannelID, unreliableChannelID);
                }
                conn.callbacks = null;
                callbacks.OnDisconnect(conn);
                RemoveConnection(connections, connectionID);
            } break;

            case NetworkEventType.DataEvent: {
                var conn = GetConnection(connections, connectionID);
                if (conn != null)
                {
                    if (channelID == reliableChannelID)
                    {
                        reliableChannelMetrics.bytesRecv += recvSize;
                        ++reliableChannelMetrics.numPacketsRecv;
                    }
                    else
                    {
                        unreliableChannelMetrics.bytesRecv += recvSize;
                        ++unreliableChannelMetrics.numPacketsRecv;
                    }
                    if (error == (byte)NetworkError.MessageToLong)
                    {
                        callbacks.OnInvalidMessageReceived(conn);
                    }
                    else
                    {
                        callbacks.OnMessageReceived(conn, recvBuffer, recvSize);
                    }
                }
            } break;

            default:
                return;
            }
        }
    }