Ejemplo n.º 1
0
        public void Update(long dt)
        {
            this._updateContext.deltaTime = dt;
            this._updateContext.time     += dt;

            this.ProcessReceiveDatas();
            this.CheckClientOverRange();
            this.CheckClientAlive();
            this.UpdateClients();

            int count = this._tokenManager.count;

            for (int i = 0; i < count; i++)
            {
                KCPUserToken token = this._tokenManager[i];
                if (token.disconnectEvent == null)
                {
                    continue;
                }
                this.OnSocketEvent?.Invoke(token.disconnectEvent.Value);
                token.OnDespawn();
                this._rpcManager.OnUserTokenDespawn(token);
                this._tokenManager.Destroy(i);
                --i;
                --count;
            }
            if (this._closeEvent != null)
            {
                this.OnSocketEvent?.Invoke(this._closeEvent.Value);
                this._closeEvent = null;
                this.Stop();
            }
        }
Ejemplo n.º 2
0
        public void Stop()
        {
            Socket socket = this._socket;

            lock (this._receivedDatas)
            {
                if (this._socket == null)
                {
                    return;
                }
                this._socket = null;
                this._receivedDatas.Clear();
            }

            while (this._tokenManager.count > 0)
            {
                KCPUserToken token = this._tokenManager[0];
                this.OnSocketEvent?.Invoke(new SocketEvent(SocketEvent.Type.Disconnect, "Server stoped", SocketError.Shutdown, token));
                token.OnDespawn();
                this._rpcManager.OnUserTokenDespawn(token);
                this._tokenManager.Destroy(0);
            }

            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }
Ejemplo n.º 3
0
        public void Send(ushort tokenId, Packet packet, RPCHandler callback)
        {
            KCPUserToken token = this._tokenManager.Get(tokenId);

            this._rpcManager.Accept(token, packet, callback);
            packet.OnSend();
            token?.Send(packet);
        }
Ejemplo n.º 4
0
 public void Send(IEnumerable <ushort> tokenIds, Packet packet)
 {
     packet.OnSend();
     byte[] data = NetworkHelper.EncodePacket(packet);
     foreach (ushort tokenId in tokenIds)
     {
         KCPUserToken token = this._tokenManager.Get(tokenId);
         token?.Send(data);
     }
 }
Ejemplo n.º 5
0
        private void CheckClientOverRange()
        {
            int over = this._tokenManager.count - this._maxClient;

            for (int i = 0; i < over; i++)
            {
                KCPUserToken token = this._tokenManager[this._tokenManager.count - 1];
                token.MarkToDisconnect("Client overrange", SocketError.SocketError);
            }
        }
Ejemplo n.º 6
0
        private void ProcessReceiveDatas()
        {
            this._receivedDatas.Switch();
            while (!this._receivedDatas.isEmpty)
            {
                ReceivedData receivedData = this._receivedDatas.Pop();
                byte[]       data         = receivedData.data;
                int          offset       = 0;
                int          size         = data.Length;

                if (!this.VerifyConnKey(data, ref offset, ref size))
                {
                    continue;
                }

                if (this.VerifyHandshake(data, ref offset, ref size))
                {
                    KCPUserToken newToken = this._tokenManager.Create();
                    this._rpcManager.OnUserTokenSpawn(newToken);
                    newToken.OnSpawn(this, receivedData.remoteEndPoint, TimeUtils.utcTime);
                    this.OnSocketEvent?.Invoke(new SocketEvent(SocketEvent.Type.Accept,
                                                               $"Client connection accepted, Remote Address: {receivedData.remoteEndPoint}",
                                                               SocketError.Success, newToken));

                    byte[] handshakeAckData   = new byte[NetworkConfig.SIZE_OF_CONN_KEY + NetworkConfig.SIZE_OF_SIGNATURE + NetworkConfig.SIZE_OF_PEER_ID];
                    int    handshakeAckOffset = ByteUtils.Encode32u(handshakeAckData, 0, NetworkConfig.CONN_KEY);
                    handshakeAckOffset += ByteUtils.Encode16u(handshakeAckData, handshakeAckOffset, NetworkConfig.HANDSHAKE_SIGNATURE);
                    handshakeAckOffset += ByteUtils.Encode16u(handshakeAckData, handshakeAckOffset, newToken.id);
                    newToken.SendDirect(handshakeAckData, 0, handshakeAckOffset);
                    continue;
                }

                ushort peerId = 0;
                if (!this.VerifyPeerId(data, ref offset, ref size, ref peerId))
                {
                    continue;
                }

                KCPUserToken token = this._tokenManager.Get(peerId);
                token?.ProcessData(data, offset, size, packet =>
                {
                    if (packet.module == NetworkConfig.INTERNAL_MODULE && packet.command == 0)
                    {
                        token.Send(new PacketHeartBeat((( PacketHeartBeat )packet).localTime));
                    }
                    else
                    {
                        this._rpcManager.Invoke(token, packet);
                        this.OnSocketEvent?.Invoke(new SocketEvent(SocketEvent.Type.Receive, packet, token));
                    }
                });
            }
        }
Ejemplo n.º 7
0
 internal void SendTo(KCPUserToken token, byte[] data, int offset, int size, EndPoint endPoint)
 {
     if (this._socket == null)
     {
         return;
     }
     try
     {
         this._socket.SendTo(data, offset, size, SocketFlags.None, endPoint);
     }
     catch (ObjectDisposedException)
     {
     }
     catch (SocketException e)
     {
         token.MarkToDisconnect(e.ToString(), e.SocketErrorCode);
     }
 }