Esempio n. 1
0
            /// <summary>
            /// Handler for enter event.
            /// </summary>
            /// <param name="context">Context.</param>
            /// <param name="previousState">Previous state.</param>
            public override void OnEnter(ClientContext context, ClientState previousState)
            {
                DebugEx.VerboseFormat("ClientContext.DisconnectedState.OnEnter(context = {0}, previousState = {1})", context, previousState);

                Server.DisconnectClient(context.mConnectionId);
            }
Esempio n. 2
0
        /// <summary>
        /// Update is called once per frame.
        /// </summary>
        void Update()
        {
            DebugEx.VeryVeryVerbose("ServerScript.Update()");

            RevisionChecker.Update();

            int  hostId;
            int  connectionId;
            int  channelId;
            int  dataSize;
            byte error;

            NetworkEventType eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, mBuffer, CommonConstants.PACKET_SIZE, out dataSize, out error);

            switch (eventType)
            {
            case NetworkEventType.Nothing:
            {
                // Nothing
            }
            break;

            case NetworkEventType.ConnectEvent:
            {
                DebugEx.DebugFormat("Client {0} connected", connectionId);

                if (connectionId == mClients.Count + 1)
                {
                    mClients.Add(new ClientContext(connectionId));
                }
                else
                {
                    if (connectionId > 0 && connectionId <= mClients.Count && mClients[connectionId - 1] == null)
                    {
                        mClients[connectionId - 1] = new ClientContext(connectionId);
                    }
                    else
                    {
                        DebugEx.FatalFormat("Incorrect behaviour on handling connected client. connectionId = {0} mClients.Count = {1}", connectionId, mClients.Count);

                        Server.DisconnectClient(connectionId);
                    }
                }
            }
            break;

            case NetworkEventType.DataEvent:
            {
                if (connectionId > 0 && connectionId <= mClients.Count && mClients[connectionId - 1] != null)
                {
                    mClients[connectionId - 1].OnMessageReceivedFromClient(mBuffer, dataSize);
                }
                else
                {
                    DebugEx.FatalFormat("Incorrect behaviour on handling data from client. connectionId = {0} mClients.Count = {1}", connectionId, mClients.Count);

                    Server.DisconnectClient(connectionId);
                }
            }
            break;

            case NetworkEventType.DisconnectEvent:
            {
                DebugEx.DebugFormat("Client {0} disconnected, error: {1}({2})", connectionId, (NetworkError)error, error);

                if (connectionId > 0 && connectionId <= mClients.Count && mClients[connectionId - 1] != null)
                {
                    mClients[connectionId - 1] = null;
                }
                else
                {
                    DebugEx.FatalFormat("Incorrect behaviour on handling disconnected client. connectionId = {0} mClients.Count = {1}", connectionId, mClients.Count);
                }
            }
            break;

            case NetworkEventType.BroadcastEvent:
            {
                DebugEx.ErrorFormat("Unexpected event type: {0}", eventType);
            }
            break;

            default:
            {
                DebugEx.ErrorFormat("Unknown event type: {0}", eventType);
            }
            break;
            }
        }