Esempio n. 1
0
        public void UserConnectService(object _serverUserContext)
        {
            User       user       = (User)_serverUserContext;
            Socket     socket     = user.socket;
            NetContext netContext = user.netContext;

            serverContext.Log(string.Format("connected. {0} {1}", socket.RemoteEndPoint, socket.LocalEndPoint));
            PlayerInfo playerInfo        = null;
            DateTime   lastNetActionTime = DateTime.Now;

            try
            {
                netContext.Init(socket);
                Task.Factory.StartNew(netContext.ReceiveService, TaskCreationOptions.LongRunning);
                netContext.SendStringCompressed((int)NetMessageType.License, serverContext.License);

                playerInfo             = new PlayerInfo();
                playerInfo.currentUser = user;
                playerInfo.PlayerGuid  = Guid.NewGuid();
                world.playerEnterQueue.Enqueue(playerInfo);
                while (true)
                {
                    netContext.receiveEvent.WaitOne();
                    if (!socket.Connected)
                    {
                        return;
                    }
                    var readList = netContext.receivedBlocks.GetReadList();
                    foreach (var block in readList)
                    {
                        switch (block.header)
                        {
                        case (int)NetMessageType.Move:
                            playerInfo.PositionI = VectorExt.Int3FromBytes(new Span <byte>(block.data, 0, 12));
                            playerInfo.PositionF = VectorExt.Vector3FromBytes(new Span <byte>(block.data, 12, 12));
                            playerInfo.Rotation  = VectorExt.QuaternionFromBytes(new Span <byte>(block.data, 24, 16));
                            break;
                        }
                    }
                    DateTime now = DateTime.Now;
                    playerInfo.lastActivateTime = now;
                    user.SendPacks();

                    //if (now - lastNetActionTime > TimeSpan.FromMilliseconds(10))
                    //{
                    //    lastNetActionTime = now;
                    //    foreach (var chunkPosition in world.VisibleChunks(playerInfo))
                    //    {
                    //        var chunk1 = world.GetChunk(chunkPosition);
                    //        if (playerInfo.visibleChunks.TryGetValue(chunkPosition, out var timeStamp1))
                    //        {
                    //            if (chunk1 != null && chunk1.stamp > timeStamp1)
                    //            {
                    //                playerInfo.visibleChunks[chunkPosition] = chunk1.stamp;
                    //                chunk1.Send(netContext);
                    //            }
                    //        }
                    //        else if (chunk1 != null)
                    //        {
                    //            playerInfo.visibleChunks[chunkPosition] = chunk1.stamp;
                    //            chunk1.Send(netContext);
                    //        }
                    //    }
                    //    playerInfo.RemoveUnusedChunks();
                    //}
                }
            }
            catch (Exception e)
            {
                user.Log(e);
            }
            finally
            {
                serverContext.Log(string.Format("connection close. {0} {1}", socket.RemoteEndPoint, socket.LocalEndPoint));
                if (playerInfo != null)
                {
                    world.playerLeaveQueue.Enqueue(playerInfo);
                }
                socket.Close();
            }
        }