示例#1
0
        /// <summary>
        /// Disconnect from the network.
        /// </summary>
        public virtual void Disconnect()
        {
            if (!IsStarted)
            {
                return;
            }

            if (IsServer)
            {
                ServerStopped?.Invoke();
            }
            else
            {
                ConnectionTerminated?.Invoke();
            }

            m_LastSendSize           = 0;
            m_ReceiveBuffer          = null;
            IsConnected              = false;
            IsServer                 = false;
            IsStarted                = false;
            m_HostID                 = -1;
            ConnectionID             = -1;
            LocalConnectionID        = -1;
            ReliableChannel          = 0;
            UnReliableChannel        = 0;
            ReliableSequencedChannel = 0;
            m_ReceiveHandlers?.Clear();
            m_ReceiveHandlers = null;
            m_Connections?.Clear();
            m_Connections = null;
            Scene?.Clear();
            Scene = null;
            RemoteProcedures?.Clear();

            ConnectionEstablished    = null;
            ConnectionTerminated     = null;
            ServerClientConnected    = null;
            ServerClientDisconnected = null;
            RemoteConnected          = null;
            RemoteDisconnected       = null;
            OnlineSceneLoaded        = null;
            ServerStarted            = null;
            ServerStopped            = null;
            InitializeHandlers       = null;
            Log = null;

            NetworkTransport.Shutdown();
            SceneManager.LoadScene(m_Settings.m_OfflineScene.m_SceneName);
        }
示例#2
0
        private void ReceiveData(IAsyncResult result)
        {
            try
            {
                CSockState cSockState = result.AsyncState as CSockState;
                //No connection ?
                if (!cSockState.Socket.Connected)
                {
                    return;
                }
                SocketError socketStatus = SocketError.Disconnecting;
                int         dataLength   = cSockState.Socket.EndReceive(result, out socketStatus);
                if (socketStatus == SocketError.Success && dataLength != 0)
                {
                    //We received data, copy to local byte buffer
                    byte[] buffer = new byte[dataLength];
                    Buffer.BlockCopy(cSockState.CBuffer, 0, buffer, 0, dataLength);
                    //TODO: Determine what this is used for ?
                    byte[] question = new byte[] { 1 };

                    DataReceived.Invoke(buffer, cSockState, question);

                    //If socket still connected receive more data
                    if (cSockState.Socket.Connected && question[0] == 1)
                    {
                        cSockState.Socket.BeginReceive(cSockState.CBuffer, 0, cSockState.CBuffer.Length, SocketFlags.None, ReceiveData, cSockState);
                    }
                }
                else
                {
                    //On error disconnect the socket
                    if (cSockState.Socket.Connected)
                    {
                        cSockState.Socket.Disconnect(true);
                    }
                    ConnectionTerminated.Invoke(cSockState);
                }
            }
            catch (Exception ex)
            {
                //TODO:Log exceptions
            }
        }