Ejemplo n.º 1
0
        public bool ClientReceive(out TransportEventData eventData)
        {
            eventData = default(TransportEventData);
            if (clientPeer == null)
            {
                return(false);
            }

            clientPeer.Recv();

            if (clientPeer.eventQueue.Count == 0)
            {
                return(false);
            }

            if (!clientPeer.eventQueue.TryDequeue(out eventData))
            {
                return(false);
            }

            switch (eventData.type)
            {
            case ENetworkEvent.DisconnectEvent:
                // Disconnect from server
                StopClient();
                break;

            case ENetworkEvent.ErrorEvent:
                // Disconnect from server
                StopClient();
                break;
            }

            return(true);
        }
Ejemplo n.º 2
0
        private void HandleRecvData(byte[] buffer, int length, TransportEventData eventData)
        {
            recvReader.Clear();
            recvReader.SetSource(buffer, 0, length);
            eventData.type = (ENetworkEvent)recvReader.GetByte();
            switch (eventData.type)
            {
            case ENetworkEvent.ConnectEvent:
                // This must received at clients only, then create new kcp here
                clientConnId = recvReader.GetUInt();
                int remotePort = recvReader.GetInt();
                eventData.connectionId = clientConnId;
                eventQueue.Enqueue(eventData);
                kcpHandles[clientConnId] = CreateKcp(clientConnId, clientSetting);
                kcpHandles[clientConnId].remoteEndPoint = new IPEndPoint(eventData.endPoint.Address, remotePort);
                break;

            case ENetworkEvent.DataEvent:
                // Read remaining data
                eventData.reader = new NetDataReader(recvReader.GetRemainingBytes());
                eventQueue.Enqueue(eventData);
                break;

            case ENetworkEvent.DisconnectEvent:
                // This must received at clients only to force them to stop client
                eventQueue.Enqueue(eventData);
                break;
            }
        }
Ejemplo n.º 3
0
        private void RecvConnection()
        {
            if (connectSocket == null ||
                (!connectSocket.IsBound && !connectSocket.Connected))
            {
                return;
            }

            if (!connectSocket.Poll(0, SelectMode.SelectRead))
            {
                return;
            }

            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
            int      recvLength;

            try
            {
                recvLength = connectSocket.Receive(recvBuffer, 0, recvBuffer.Length, SocketFlags.None);
                endPoint   = connectSocket.RemoteEndPoint;
            }
            catch (SocketException ex)
            {
                HandleError(endPoint, ex.SocketErrorCode);
                return;
            }
            // Handle data directly for connect socket, not have to set kcp input (and read later)
            TransportEventData eventData = default(TransportEventData);

            eventData.endPoint = (IPEndPoint)endPoint;
            HandleRecvData(recvBuffer, recvLength, eventData);
        }
Ejemplo n.º 4
0
        public override void OnServerReceive(TransportEventData eventData)
        {
            CentralServerPeerInfo tempPeerInfo;

            switch (eventData.type)
            {
            case ENetworkEvent.ConnectEvent:
                Logging.Log(LogTag, "OnPeerConnected peer.ConnectionId: " + eventData.connectionId);
                ConnectionIds.Add(eventData.connectionId);
                break;

            case ENetworkEvent.DataEvent:
                ReadPacket(eventData.connectionId, eventData.reader);
                break;

            case ENetworkEvent.DisconnectEvent:
                Logging.Log(LogTag, "OnPeerDisconnected peer.ConnectionId: " + eventData.connectionId + " disconnectInfo.Reason: " + eventData.disconnectInfo.Reason);
                ConnectionIds.Remove(eventData.connectionId);
                // Remove disconnect map spawn server
                MapSpawnServerPeers.Remove(eventData.connectionId);
                // Remove disconnect map server
                if (MapServerPeers.TryGetValue(eventData.connectionId, out tempPeerInfo))
                {
                    MapServerPeersByMapId.Remove(tempPeerInfo.extra);
                    MapServerPeersByInstanceId.Remove(tempPeerInfo.extra);
                    MapServerPeers.Remove(eventData.connectionId);
                    RemoveMapUsers(eventData.connectionId);
                }
                break;

            case ENetworkEvent.ErrorEvent:
                Logging.LogError(LogTag, "OnPeerNetworkError endPoint: " + eventData.endPoint + " socketErrorCode " + eventData.socketError + " errorMessage " + eventData.errorMessage);
                break;
            }
        }
Ejemplo n.º 5
0
        public override void OnClientReceive(TransportEventData eventData)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            switch (eventData.type)
            {
            case ENetworkEvent.ConnectEvent:
                Logging.Log(LogTag, "OnClientConnected");
                OnConnectedToClusterServer();
                break;

            case ENetworkEvent.DataEvent:
                ReadPacket(-1, eventData.reader);
                break;

            case ENetworkEvent.DisconnectEvent:
                Logging.Log(LogTag, "OnClientDisconnected peer. disconnectInfo.Reason: " + eventData.disconnectInfo.Reason);
                StopClient();
                OnDisconnectedFromClusterServer().Forget();
                break;

            case ENetworkEvent.ErrorEvent:
                Logging.LogError(LogTag, "OnClientNetworkError endPoint: " + eventData.endPoint + " socketErrorCode " + eventData.socketError + " errorMessage " + eventData.errorMessage);
                break;
            }
#endif
        }
Ejemplo n.º 6
0
        private void RecvData()
        {
            if (dataSocket == null)
            {
                return;
            }

            if (!dataSocket.Poll(0, SelectMode.SelectRead))
            {
                return;
            }

            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
            int      recvLength;

            try
            {
                recvLength = dataSocket.ReceiveFrom(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, ref endPoint);
            }
            catch (SocketException ex)
            {
                HandleError(endPoint, ex.SocketErrorCode);
                return;
            }

            recvReader.Clear();
            recvReader.SetSource(recvBuffer, 0, recvLength);
            uint connectionId = recvReader.GetUInt();
            // Have to find which kcp send this data, then set its input
            KCPHandle kcpHandle = null;

            if (kcpHandles.TryGetValue(connectionId, out kcpHandle))
            {
                kcpHandle.remoteEndPoint = endPoint;
                kcpHandle.kcp.Input(new Span <byte>(recvBuffer, 0, recvLength));

                byte[] kcpData;
                while ((recvLength = kcpHandle.kcp.PeekSize()) > 0)
                {
                    kcpData = new byte[recvLength];
                    if (kcpHandle.kcp.Recv(kcpData) >= 0)
                    {
                        TransportEventData eventData = default(TransportEventData);
                        eventData.connectionId = connectionId;
                        eventData.endPoint     = (IPEndPoint)endPoint;
                        HandleRecvData(kcpData, recvLength, eventData);
                    }
                }
            }
        }
    public bool ServerReceive(out TransportEventData eventData)
    {
        eventData = default(TransportEventData);
        if (server == null)
        {
            return(false);
        }
        Event tempNetEvent;

        byte[] tempBuffers;
        server.Service(0, out tempNetEvent);
        switch (tempNetEvent.Type)
        {
        case EventType.None:
            return(false);

        case EventType.Connect:
            eventData.type                    = ENetworkEvent.ConnectEvent;
            eventData.connectionId            = tempNetEvent.Peer.ID;
            serverPeers[tempNetEvent.Peer.ID] = tempNetEvent.Peer;
            break;

        case EventType.Disconnect:
            eventData.type         = ENetworkEvent.DisconnectEvent;
            eventData.connectionId = tempNetEvent.Peer.ID;
            serverPeers.Remove(tempNetEvent.Peer.ID);
            break;

        case EventType.Timeout:
            eventData.type           = ENetworkEvent.DisconnectEvent;
            eventData.connectionId   = tempNetEvent.Peer.ID;
            eventData.disconnectInfo = new DisconnectInfo()
            {
                Reason = DisconnectReason.Timeout
            };
            serverPeers.Remove(tempNetEvent.Peer.ID);
            break;

        case EventType.Receive:
            tempBuffers = new byte[tempNetEvent.Packet.Length];
            tempNetEvent.Packet.CopyTo(tempBuffers);

            eventData.type         = ENetworkEvent.DataEvent;
            eventData.connectionId = tempNetEvent.Peer.ID;
            eventData.reader       = new NetDataReader(tempBuffers);
            tempNetEvent.Packet.Dispose();
            break;
        }
        return(true);
    }
Ejemplo n.º 8
0
 public bool ClientReceive(out TransportEventData eventData)
 {
     eventData = default(TransportEventData);
     if (Client == null)
     {
         return(false);
     }
     if (clientEventQueue.Count == 0)
     {
         return(false);
     }
     eventData = clientEventQueue.Dequeue();
     return(true);
 }
Ejemplo n.º 9
0
        public bool ServerReceive(out TransportEventData eventData)
        {
            eventData = default(TransportEventData);
            if (serverPeer == null)
            {
                return(false);
            }

            serverPeer.Recv();

            if (serverPeer.eventQueue.Count == 0)
            {
                return(false);
            }

            return(serverPeer.eventQueue.TryDequeue(out eventData));
        }
Ejemplo n.º 10
0
 public bool ServerReceive(out TransportEventData eventData)
 {
     eventData = default(TransportEventData);
     if (Server == null)
     {
         return(false);
     }
     if (serverEventQueue.Count == 0)
     {
         return(false);
     }
     eventData = serverEventQueue.Dequeue();
     if (eventData.type == ENetworkEvent.DataEvent && eventData.reader == null)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 11
0
        public override bool Receive(out TransportEventData eventData)
        {
            eventData = default(TransportEventData);
            if (netManager == null)
            {
                return(false);
            }

            netManager.PollEvents();
            if (qEventQueue.Count == 0)
            {
                return(false);
            }
            eventData = qEventQueue.Dequeue();



            return(true);
        }
Ejemplo n.º 12
0
        private void HandleError(EndPoint endPoint, SocketError socketErrorCode)
        {
            switch (socketErrorCode)
            {
            case SocketError.Interrupted:
            case SocketError.NotSocket:
            case SocketError.ConnectionReset:
            case SocketError.MessageSize:
            case SocketError.TimedOut:
                // Ignored error
                break;

            default:
                // Store error event to queue
                TransportEventData eventData = default(TransportEventData);
                eventData.type        = ENetworkEvent.ErrorEvent;
                eventData.endPoint    = (IPEndPoint)endPoint;
                eventData.socketError = socketErrorCode;
                eventQueue.Enqueue(eventData);
                break;
            }
        }
        public override void OnClientReceive(TransportEventData eventData)
        {
            switch (eventData.type)
            {
            case ENetworkEvent.ConnectEvent:
                Debug.Log("CentralAppServerRegister::OnPeerConnected.");
                OnCentralServerConnected();
                break;

            case ENetworkEvent.DataEvent:
                ReadPacket(eventData.connectionId, eventData.reader);
                break;

            case ENetworkEvent.DisconnectEvent:
                Debug.Log("CentralAppServerRegister::OnPeerDisconnected. disconnectInfo.Reason: " + eventData.disconnectInfo.Reason);
                OnCentralServerDisconnected(eventData.disconnectInfo);
                break;

            case ENetworkEvent.ErrorEvent:
                Debug.LogError("CentralAppServerRegister::OnNetworkError endPoint: " + eventData.endPoint + " socketErrorCode " + eventData.socketError);
                break;
            }
        }
Ejemplo n.º 14
0
        public override void OnClientReceive(TransportEventData eventData)
        {
            switch (eventData.type)
            {
            case ENetworkEvent.ConnectEvent:
                Logging.Log(LogTag, "OnPeerConnected.");
                OnCentralServerConnected();
                break;

            case ENetworkEvent.DataEvent:
                ReadPacket(eventData.connectionId, eventData.reader);
                break;

            case ENetworkEvent.DisconnectEvent:
                Logging.Log(LogTag, "OnPeerDisconnected. disconnectInfo.Reason: " + eventData.disconnectInfo.Reason);
                StopClient();
                OnCentralServerDisconnected(eventData.disconnectInfo).Forget();
                break;

            case ENetworkEvent.ErrorEvent:
                Logging.LogError(LogTag, "OnNetworkError endPoint: " + eventData.endPoint + " socketErrorCode " + eventData.socketError);
                break;
            }
        }
Ejemplo n.º 15
0
 public abstract bool Receive(out TransportEventData eventData);
Ejemplo n.º 16
0
    public TransportEventData GetDisconnectEvent(ushort connectionId, bool localDisconnect, SocketError error)
    {
        TransportEventData result = new TransportEventData()
        {
            type         = ENetworkEvent.DisconnectEvent,
            connectionId = connectionId,
        };

        if (localDisconnect)
        {
            result.disconnectInfo = new DisconnectInfo()
            {
                Reason          = DisconnectReason.DisconnectPeerCalled,
                SocketErrorCode = error,
            };
        }
        else
        {
            switch (error)
            {
            case SocketError.ConnectionReset:
                result.disconnectInfo = new DisconnectInfo()
                {
                    Reason          = DisconnectReason.ConnectionFailed,
                    SocketErrorCode = error,
                };
                break;

            case SocketError.TimedOut:
                result.disconnectInfo = new DisconnectInfo()
                {
                    Reason          = DisconnectReason.Timeout,
                    SocketErrorCode = error,
                };
                break;

            case SocketError.HostUnreachable:
                result.disconnectInfo = new DisconnectInfo()
                {
                    Reason          = DisconnectReason.HostUnreachable,
                    SocketErrorCode = error,
                };
                break;

            case SocketError.NetworkUnreachable:
                result.disconnectInfo = new DisconnectInfo()
                {
                    Reason          = DisconnectReason.NetworkUnreachable,
                    SocketErrorCode = error,
                };
                break;

            case SocketError.ConnectionAborted:
                result.disconnectInfo = new DisconnectInfo()
                {
                    Reason          = DisconnectReason.RemoteConnectionClose,
                    SocketErrorCode = error,
                };
                break;

            case SocketError.ConnectionRefused:
                result.disconnectInfo = new DisconnectInfo()
                {
                    Reason          = DisconnectReason.ConnectionRejected,
                    SocketErrorCode = error,
                };
                break;

            default:
                result.disconnectInfo = new DisconnectInfo()
                {
                    SocketErrorCode = error,
                };
                break;
            }
        }
        return(result);
    }