⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pas

📁 乐都SQL版传奇全套代码,绝对可编译
💻 PAS
📖 第 1 页 / 共 3 页
字号:
begin
  boGateReady := True;
  nSessionCount := 0;
  dwKeepAliveTick := GetTickCount();
  ResUserSessionArray();
  boServerReady := True;
end;

procedure TFrmMain.ClientSocketDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  UserSession       : pTUserSession;
  nIndex            : Integer;
begin
  for nIndex := 0 to GATEMAXSESSION - 1 do
  begin
    UserSession := @g_SessionArray[nIndex];
    if UserSession.Socket <> nil then
      UserSession.Socket.Close;
    UserSession.Socket := nil;
    UserSession.sRemoteIPaddr := '';
    UserSession.SocketHandle := -1;
  end;
  ResUserSessionArray();
  ClientSockeMsgList.Clear;
  boGateReady := False;
  nSessionCount := 0;
end;

procedure TFrmMain.ClientSocketError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  Socket.Close;
  ErrorCode := 0;
  boServerReady := False;
end;

procedure TFrmMain.ClientSocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  sRecvMsg          : string;
begin
  sRecvMsg := Socket.ReceiveText;
  ClientSockeMsgList.Add(sRecvMsg);
end;

procedure TFrmMain.SendTimerTimer(Sender: TObject);
var
  nIndex            : Integer;
  UserSession       : pTUserSession;
begin
  if ServerSocket.Active then
  begin
    n456A30 := ServerSocket.Socket.ActiveConnections;
  end;
  if boSendHoldTimeOut then
  begin
    //LbHold.Caption := IntToStr(n456A30) + '#';
    if (GetTickCount - dwSendHoldTick) > 3 * 1000 then
      boSendHoldTimeOut := False;
  end
  else
  begin
   // LbHold.Caption := IntToStr(n456A30);
  end;
  if boGateReady and (not boKeepAliveTimcOut) then
  begin
    for nIndex := 0 to GATEMAXSESSION - 1 do
    begin
      UserSession := @g_SessionArray[nIndex];
      if UserSession.Socket <> nil then
      begin
        if (GetTickCount - UserSession.dwUserTimeOutTick) > 60 * 60 * 1000 then
        begin
          UserSession.Socket.Close;
          UserSession.Socket := nil;
          UserSession.SocketHandle := -1;
          UserSession.MsgList.Clear;
          UserSession.sRemoteIPaddr := '';
        end;
      end;
    end;
  end;
  if not boGateReady and (boServiceStart) then
  begin
    if (GetTickCount - dwReConnectServerTick) > 1000 {30 * 1000} then
    begin
      dwReConnectServerTick := GetTickCount();
      ClientSocket.Active := False;
      ClientSocket.Port := ServerPort;
      ClientSocket.Host := ServerAddr;
      ClientSocket.Active := True;
    end;
  end;
end;

procedure TFrmMain.TimerTimer(Sender: TObject);
begin
  if ServerSocket.Active then
  begin
    StatusBar.Panels[0].Text := IntToStr(ServerSocket.Port);
    if boSendHoldTimeOut then
      StatusBar.Panels[2].Text := IntToStr(nSessionCount) + '/#' +
        IntToStr(ServerSocket.Socket.ActiveConnections)
    else
      StatusBar.Panels[2].Text := IntToStr(nSessionCount) + '/' +
        IntToStr(ServerSocket.Socket.ActiveConnections);
  end
  else
  begin
    StatusBar.Panels[0].Text := '????';
    StatusBar.Panels[2].Text := '????';
  end;
//  Label2.Caption := IntToStr(dwDecodeMsgTime);
  if not boGateReady then
  begin
    //StatusBar.Panels[1].Text:='---]    [---';
    StatusBar.Panels[1].Text := '未连接';
  end
  else
  begin
    if boKeepAliveTimcOut then
    begin
      //StatusBar.Panels[1].Text:='---]$$$$[---';
      StatusBar.Panels[1].Text := '超时';
    end
    else
    begin
      //StatusBar.Panels[1].Text:='-----][-----';
      StatusBar.Panels[1].Text := '已连接';
   //   LbLack.Caption := IntToStr(n456A2C) + '/' + IntToStr(nSendMsgCount);
    end;
  end;
end;

procedure TFrmMain.DecodeTimerTimer(Sender: TObject);
var
  sProcessMsg       : string;
  sSocketMsg        : string;
  sSocketHandle     : string;
  nSocketIndex      : Integer;
  nMsgCount         : Integer;
  nSendRetCode      : Integer;
  nSocketHandle     : Integer;
  dwDecodeTick      : LongWord;
  dwDecodeTime      : LongWord;
  sRemoteIPaddr     : string;
  UserSession       : pTUserSession;
  IPaddr            : pTSockaddr;
begin
  ShowMainLogMsg();
  if boDecodeLock or (not boGateReady) then
    exit;

  try
    dwDecodeTick := GetTickCount();
    boDecodeLock := True;
    sProcessMsg := '';
    while (True) do
    begin
      if ClientSockeMsgList.Count <= 0 then
        break;
      sProcessMsg := sProcMsg + ClientSockeMsgList.Strings[0];
      sProcMsg := '';
      ClientSockeMsgList.Delete(0);
      while (True) do
      begin
        if TagCount(sProcessMsg, '$') < 1 then
          break;
        sProcessMsg := ArrestStringEx(sProcessMsg, '%', '$', sSocketMsg);
        if sSocketMsg = '' then
          break;
        if sSocketMsg[1] = '+' then
        begin
          if sSocketMsg[2] = '-' then
          begin
            CloseSocket(Str_ToInt(Copy(sSocketMsg, 3, Length(sSocketMsg) - 2),
              0));
            Continue;
          end
          else
          begin                         //0x004521B7
            dwKeepAliveTick := GetTickCount();
            boKeepAliveTimcOut := False;
            Continue;
          end;
        end;                            //0x004521CD
        sSocketMsg := GetValidStr3(sSocketMsg, sSocketHandle, ['/']);
        nSocketHandle := Str_ToInt(sSocketHandle, -1);
        if nSocketHandle < 0 then
          Continue;
        for nSocketIndex := 0 to GATEMAXSESSION - 1 do
        begin
          if g_SessionArray[nSocketIndex].SocketHandle = nSocketHandle then
          begin
            g_SessionArray[nSocketIndex].MsgList.Add(sSocketMsg);
            break;
          end;
        end;
      end;                              //0x00452246
    end;                                //0x452252
    //if sProcessMsg <> '' then ClientSockeMsgList.Add(sProcessMsg);
    if sProcessMsg <> '' then
      sProcMsg := sProcessMsg;

    nSendMsgCount := 0;
    n456A2C := 0;
    StringList318.Clear;
    for nSocketIndex := 0 to GATEMAXSESSION - 1 do
    begin
      if g_SessionArray[nSocketIndex].SocketHandle <= -1 then
        Continue;

      //踢除超时无数据传输连接
      if (GetTickCount - g_SessionArray[nSocketIndex].dwConnctCheckTick) >
        dwKeepConnectTimeOut then
      begin
        sRemoteIPaddr := g_SessionArray[nSocketIndex].sRemoteIPaddr;
        case BlockMethod of             //
          mDisconnect:
            begin
              g_SessionArray[nSocketIndex].Socket.Close;
            end;
          mBlock:
            begin
              New(IPaddr);
              IPaddr.nIPaddr := inet_addr(PChar(sRemoteIPaddr));
              TempBlockIPList.Add(IPaddr);
              CloseConnect(sRemoteIPaddr);
            end;
          mBlockList:
            begin
              New(IPaddr);
              IPaddr.nIPaddr := inet_addr(PChar(sRemoteIPaddr));
              BlockIPList.Add(IPaddr);
              CloseConnect(sRemoteIPaddr);
            end;
        end;
        MainOutMessage('端口空连接攻击: ' + sRemoteIPaddr, 1);
        Continue;
      end;

      while (True) do
      begin
        ;
        if g_SessionArray[nSocketIndex].MsgList.Count <= 0 then
          break;
        UserSession := @g_SessionArray[nSocketIndex];
        nSendRetCode := SendUserMsg(UserSession,
          UserSession.MsgList.Strings[0]);
        if (nSendRetCode >= 0) then
        begin
          if nSendRetCode = 1 then
          begin
            UserSession.dwConnctCheckTick := GetTickCount();
            UserSession.MsgList.Delete(0);
            Continue;
          end;
          if UserSession.MsgList.Count > 100 then
          begin
            nMsgCount := 0;
            while nMsgCount <> 51 do
            begin
              UserSession.MsgList.Delete(0);
              Inc(nMsgCount);
            end;
          end;
          Inc(n456A2C, UserSession.MsgList.Count);
          MainOutMessage(UserSession.sIP +
            ' : ' +
            IntToStr(UserSession.MsgList.Count), 5);
          Inc(nSendMsgCount);
        end
        else
        begin                           //0x004523A4
          UserSession.SocketHandle := -1;
          UserSession.Socket := nil;
          UserSession.MsgList.Clear;
        end;
      end;
    end;
    if (GetTickCount - dwSendKeepAliveTick) > 2 * 1000 then
    begin
      dwSendKeepAliveTick := GetTickCount();
      if boGateReady then
        ClientSocket.Socket.SendText('%--$');
    end;
    if (GetTickCount - dwKeepAliveTick) > 10 * 1000 then
    begin
      boKeepAliveTimcOut := True;
      ClientSocket.Close;
    end;
  finally
    boDecodeLock := False;
  end;
  dwDecodeTime := GetTickCount - dwDecodeTick;
  if dwDecodeMsgTime < dwDecodeTime then
    dwDecodeMsgTime := dwDecodeTime;
  if dwDecodeMsgTime > 50 then
    Dec(dwDecodeMsgTime, 50);
end;

procedure TFrmMain.CloseSocket(nSocketHandle: Integer);
var
  nIndex            : Integer;
  UserSession       : pTUserSession;
begin
  for nIndex := 0 to GATEMAXSESSION - 1 do
  begin
    UserSession := @g_SessionArray[nIndex];
    if (UserSession.Socket <> nil) and (UserSession.SocketHandle = nSocketHandle)
      then
    begin
      UserSession.Socket.Close;
      break;
    end;
  end;
end;

function TFrmMain.SendUserMsg(UserSession: pTUserSession; sSendMsg: string):
  Integer;
begin
  Result := -1;
  if UserSession.Socket <> nil then
  begin
    if not UserSession.bo0C then
    begin
      if not UserSession.boSendAvailable and (GetTickCount >
        UserSession.dwSendLockTimeOut) then
      begin
        UserSession.boSendAvailable := True;
        UserSession.nCheckSendLength := 0;
        boSendHoldTimeOut := True;
        dwSendHoldTick := GetTickCount();
      end;                              //004525DD
      if UserSession.boSendAvailable then
      begin
        if UserSession.nCheckSendLength >= 250 then
        begin
          if not UserSession.boSendCheck then
          begin
            UserSession.boSendCheck := True;
            sSendMsg := '*' + sSendMsg;
          end;
          if UserSession.nCheckSendLength >= 512 then
          begin
            UserSession.boSendAvailable := False;
            UserSession.dwSendLockTimeOut := GetTickCount + 3 * 1000;
          end;
        end;                            //00452620
        UserSession.Socket.SendText(sSendMsg);
        Inc(UserSession.nSendMsgLen, Length(sSendMsg));
        Inc(UserSession.nCheckSendLength, Length(sSendMsg));
        Result := 1;
      end
      else
      begin                             //0x0045264A
        Result := 0;
      end;
    end
    else
    begin                               //0x00452651
      Result := 0;
    end;
  end;
end;

procedure TFrmMain.LoadConfig;
var
  Conf              : TIniFile;
  sConfigFileName   : string;
begin
  sConfigFileName := '.\Config.ini';
  Conf := TIniFile.Create(sConfigFileName);
  TitleName := Conf.ReadString(GateClass, 'Title', TitleName);
  ServerPort := Conf.ReadInteger(GateClass, 'ServerPort', ServerPort);
  ServerAddr := Conf.ReadString(GateClass, 'ServerAddr', ServerAddr);
  GatePort := Conf.ReadInteger(GateClass, 'GatePort', GatePort);
  GateAddr := Conf.ReadString(GateClass, 'GateAddr', GateAddr);
  nShowLogLevel := Conf.ReadInteger(GateClass, 'ShowLogLevel', nShowLogLevel);

  BlockMethod := TBlockIPMethod(Conf.ReadInteger(GateClass, 'BlockMethod',
    Integer(BlockMethod)));

  if Conf.ReadInteger(GateClass, 'KeepConnectTimeOut', -1) <= 0 then
    Conf.WriteInteger(GateClass, 'KeepConnectTimeOut', dwKeepConnectTimeOut);

  nMaxConnOfIPaddr := Conf.ReadInteger(GateClass, 'MaxConnOfIPaddr',
    nMaxConnOfIPaddr);
  dwKeepConnectTimeOut := Conf.ReadInteger(GateClass, 'KeepConnectTimeOut',
    dwKeepConnectTimeOut);
  g_boDynamicIPDisMode := Conf.ReadBool(GateClass, 'DynamicIPDisMode',
    g_boDynamicIPDisMode);
  Conf.Free;
  LoadBlockIPFile();
end;

procedure TFrmMain.StartService;
begin
  try
    MainOutMessage('乐都在线:http://www.hh8.net',0);
    MainOutMessage('正在启动服务...', 3);
    // SendGameCenterMsg(SG_STARTNOW,g_sNowStartGate);
    boServiceStart := True;
    boGateReady := False;
    boServerReady := False;
    nSessionCount := 0;
    MENU_CONTROL_START.Enabled := False;
    MENU_CONTROL_STOP.Enabled := True;

    Caption := GateName + ' - ' + TitleName;
    dwReConnectServerTick := GetTickCount - 25 * 1000;
    boKeepAliveTimcOut := False;
    nSendMsgCount := 0;
    n456A2C := 0;
    dwSendKeepAliveTick := GetTickCount();
    boSendHoldTimeOut := False;
    dwSendHoldTick := GetTickCount();

    CurrIPaddrList := TList.Create;
    BlockIPList := TList.Create;
    TempBlockIPList := TList.Create;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -