/// <summary> /// 接受处理回调 /// </summary> /// <param name="e">操作对象</param> private void ProcessReceive(SocketAsyncEventArgs e) { AsyncUserToken userToken = (AsyncUserToken)e.UserToken; int offset = userToken.ReceiveEventArgs.Offset; int count = userToken.ReceiveEventArgs.BytesTransferred; int endOffset = offset + count; byte[] Buffer = userToken.ReceiveEventArgs.Buffer; if (count > 0 && userToken.ReceiveEventArgs.SocketError == SocketError.Success) { Interlocked.Add(ref TotalBytesRead, (uint)e.BytesTransferred); try { Block block = new Block(Buffer, offset); BaseProtocol baseProtocol = new BaseProtocol(); do { baseProtocol.Analyze(block); if (baseProtocol.PacketId == 0) { if (baseProtocol.PacketSize > 3) { if (userToken.StartLogin) { Login login = baseProtocol.Resolve <Login>(); if (Program.WhiteList) { UserModel userModel = databaseManager.FindPlayer(login.Name); if (userModel == null) { userToken.Kick(Program.NoFind); break; } else { if (userModel.End_at < DateTime.Now) { userToken.Kick(Program.IsEnd); break; } } userToken.EndTime = userModel.End_at; } userToken.Tunnel(this); userToken.PlayerName = login.Name; userToken.tunnel.Login(login.Name, userToken.ProtocolVersion, userToken.IsForge); Online.Add(userToken.PlayerName, userToken); } else { Handshake handshake = baseProtocol.Resolve <Handshake>(); userToken.ProtocolVersion = handshake.ProtocolVersion; if (handshake.NextState == NextState.login) { if (handshake.ServerAddress.IndexOf("FML") > 0) { userToken.IsForge = true; } userToken.StartLogin = true; } else { userToken.StartLogin = false; } } } else { // 开始处理本次收到的数据包 SocketAsyncEventArgs sendPacket = new SocketAsyncEventArgs(); Response response = new Response("1.8.9", userToken.ProtocolVersion); response.players.online = Online.Count; response.players.max = Program.MaxConnections; response.players.sample = new List <SampleItem>(); response.description.text = Program.QueryConfig.Motd; response.favicon = ""; using (Block temp = new Block()) { temp.WriteInt(0); temp.WriteString(JsonSerializer.Serialize(response), true); byte[] buffer = temp.GetBytes(); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.WriteInt(buffer.Length); memoryStream.Write(buffer); sendPacket.SetBuffer(memoryStream.GetBuffer(), 0, (int)memoryStream.Position); userToken.ServerSocket.SendAsync(sendPacket); } // Console.Out.WriteLine("New player."); } } } else if (baseProtocol.PacketId == 1) { SocketAsyncEventArgs sendPacket = new SocketAsyncEventArgs(); Pong pong = baseProtocol.Resolve <Pong>(); using (Block temp = new Block()) { temp.WriteInt(1); temp.WriteLong(pong.Payload); byte[] buffer = temp.GetBytes(); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.WriteInt(buffer.Length); memoryStream.Write(buffer); sendPacket.SetBuffer(memoryStream.GetBuffer(), 0, (int)memoryStream.Position); } // Console.Out.WriteLine("New Ping."); userToken.ServerSocket.SendAsync(sendPacket); } } } while (baseProtocol.block.step < endOffset); } catch (Exception ex) { } // 准备下次接收数据 bool willRaiseEvent = userToken.ServerSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求 if (!willRaiseEvent) { ProcessReceive(userToken.ReceiveEventArgs); } } else { CloseClientSocket(e); } }