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

📄 main.pas

📁 传奇服务端代码 SelGate.rar通讯部分
💻 PAS
📖 第 1 页 / 共 3 页
字号:
  nSockIdx :Integer;
  IPaddr   :pTSockaddr;
begin
  MainOutMessage('服务已停止...',3);
  boServiceStart:=False;
  boGateReady:=False;
  MENU_CONTROL_START.Enabled:=True;
  MENU_CONTROL_STOP.Enabled:=False;
  SendTimer.Enabled:=False;
  for nSockIdx:=0 to GATEMAXSESSION - 1 do begin
    if g_SessionArray[nSockIdx].Socket <> nil then
      g_SessionArray[nSockIdx].Socket.Close;
  end;
  SaveBlockIPList();
  ServerSocket.Close;
  ClientSocket.Close;
  ClientSockeMsgList.Free;
  for I := 0 to CurrIPaddrList.Count - 1 do begin
    IPaddr:=CurrIPaddrList.Items[I];
    Dispose(IPaddr);
  end;
  CurrIPaddrList.Free;
  for I := 0 to BlockIPList.Count - 1 do begin
    IPaddr:=BlockIPList.Items[I];
    Dispose(IPaddr);
  end;
  BlockIPList.Free;
  for I := 0 to TempBlockIPList.Count - 1 do begin
    IPaddr:=TempBlockIPList.Items[I];
    Dispose(IPaddr);
  end;
  TempBlockIPList.Free;
  MainOutMessage('端口已关闭...',3);
end;

procedure TFrmMain.ResUserSessionArray;
var
  UserSession:pTUserSession;
  nIndex:Integer;
begin
  for nIndex:= 0 to GATEMAXSESSION - 1 do begin
    UserSession:=@g_SessionArray[nIndex];
    UserSession.Socket    := nil;
    UserSession.sRemoteIPaddr     := '';
    UserSession.SocketHandle       :=-1;
    UserSession.MsgList.Clear;
  end;
end;
procedure TFrmMain.IniUserSessionArray();
var
  UserSession:pTUserSession;
  nIndex:Integer;
begin
  for nIndex:= 0 to GATEMAXSESSION - 1 do begin
    UserSession:=@g_SessionArray[nIndex];
    UserSession.Socket           := nil;
    UserSession.sRemoteIPaddr    := '';
    UserSession.nSendMsgLen      :=0;
    UserSession.bo0C             :=False;
    UserSession.dw10Tick         :=GetTickCount();
    UserSession.boSendAvailable  :=True;
    UserSession.boSendCheck      :=False;
    UserSession.nCheckSendLength :=0;
    UserSession.n20              :=0;
    UserSession.dwUserTimeOutTick:=GetTickCount();
    UserSession.SocketHandle     :=-1;
    UserSession.MsgList          :=TStringList.Create;
  end;
end;
procedure TFrmMain.StartTimerTimer(Sender: TObject);
begin
  if boStarted then begin
    StartTimer.Enabled:=False;
    StopService();
    boClose:=True;
    Close;
  end else begin
    MENU_VIEW_LOGMSGClick(Sender);
    boStarted:=True;
    StartTimer.Enabled:=False;
    StartService();
  end;
end;

procedure TFrmMain.FormCreate(Sender: TObject);
var
  nX,nY:Integer;
begin
  g_dwGameCenterHandle:=Str_ToInt(ParamStr(1),0);
  nX:=Str_ToInt(ParamStr(2),-1);
  nY:=Str_ToInt(ParamStr(3),-1);
  if (nX >= 0) or (nY >= 0) then begin
    Left:=nX;
    Top:=nY;
  end;
    
  
  TempLogList:=TStringList.Create;
  StringList30C:=TStringList.Create;
  StringList318:=TStringList.Create;
  dwDecodeMsgTime:=0;
  SendGameCenterMsg(SG_FORMHANDLE,IntToStr(Self.Handle));
  IniUserSessionArray();
end;

procedure TFrmMain.MENU_CONTROL_STARTClick(Sender: TObject);
begin
  StartService();
end;

procedure TFrmMain.MENU_CONTROL_STOPClick(Sender: TObject);
begin
  if Application.MessageBox('你想要停止服务吗?',
                            '确定',
                            MB_YESNO + MB_ICONQUESTION ) = IDYES then
    StopService();
end;

procedure TFrmMain.MENU_CONTROL_RECONNECTClick(Sender: TObject);
begin
  dwReConnectServerTick:=0;
end;

procedure TFrmMain.MENU_CONTROL_CLEAELOGClick(Sender: TObject);
begin
  if Application.MessageBox('你想要清除记录吗?',
                            '确定',
                            MB_OKCANCEL + MB_ICONQUESTION
                            ) <> IDOK then exit;
  MemoLog.Clear;
end;

procedure TFrmMain.MENU_CONTROL_EXITClick(Sender: TObject);
begin
  Close;
end;

procedure TFrmMain.MENU_VIEW_LOGMSGClick(Sender: TObject);
begin
  MENU_VIEW_LOGMSG.Checked:=not MENU_VIEW_LOGMSG.Checked;
  ShowLogMsg(MENU_VIEW_LOGMSG.Checked);
end;

procedure TFrmMain.ShowLogMsg(boFlag: Boolean);
var
  nHeight:Integer;
begin
  case boFlag of
    True: begin
      nHeight:=Panel.Height;
      Panel.Height:=0;
      MemoLog.Height:=nHeight;
      MemoLog.Top:=Panel.Top;
    end;
    False: begin
      nHeight:=MemoLog.Height;
      MemoLog.Height:=0;
      Panel.Height:=nHeight;
    end;
  end;
end;

procedure TFrmMain.MENU_OPTION_GENERALClick(Sender: TObject);
begin
  CenterDialog(Handle, frmGeneralConfig.Handle);
  with frmGeneralConfig do begin
    EditGateIPaddr.Text:=GateAddr;
    EditGatePort.Text:=IntToStr(GatePort);
    EditServerIPaddr.Text:=ServerAddr;
    EditServerPort.Text:=IntToStr(ServerPort);
    EditTitle.Text:=TitleName;
    TrackBarLogLevel.Position:=nShowLogLevel;
  end;
  frmGeneralConfig.ShowModal;
end;
procedure TFrmMain.CloseConnect(sIPaddr: String);
var
  i:Integer;
  boCheck:Boolean;
begin
  if ServerSocket.Active then
    while (True) do begin
      boCheck:=False;
      for i:= 0 to ServerSocket.Socket.ActiveConnections - 1 do begin
        if sIPaddr=ServerSocket.Socket.Connections[i].RemoteAddress then begin
          ServerSocket.Socket.Connections[i].Close;
          boCheck:=True;
          break;
        end;
      end;
      if not boCheck then break;        
    end;
end;
procedure TFrmMain.MENU_OPTION_IPFILTERClick(Sender: TObject);
var
  i:Integer;
  sIPaddr:String;
begin
  CenterDialog(Handle, frmIPaddrFilter.Handle);
  frmIPaddrFilter.ListBoxActiveList.Clear;
  frmIPaddrFilter.ListBoxTempList.Clear;
  frmIPaddrFilter.ListBoxBlockList.Clear;
  if ServerSocket.Active then
    for i:= 0 to ServerSocket.Socket.ActiveConnections - 1 do begin
      sIPaddr:=ServerSocket.Socket.Connections[i].RemoteAddress;
      if sIPaddr <> '' then
        frmIPaddrFilter.ListBoxActiveList.Items.AddObject(sIPaddr,Tobject(ServerSocket.Socket.Connections[i]));
    end;

  for i:= 0 to TempBlockIPList.Count - 1 do begin
    frmIPaddrFilter.ListBoxTempList.Items.Add(StrPas(inet_ntoa(TInAddr(pTSockaddr(TempBlockIPList.Items[I]).nIPaddr))));
  end;
  for i:= 0 to BlockIPList.Count - 1 do begin
    frmIPaddrFilter.ListBoxBlockList.Items.Add(StrPas(inet_ntoa(TInAddr(pTSockaddr(BlockIPList.Items[I]).nIPaddr))));
  end;
  frmIPaddrFilter.EditMaxConnect.Value:=nMaxConnOfIPaddr;
  case BlockMethod of
    mDisconnect: frmIPaddrFilter.RadioDisConnect.Checked:=True;
    mBlock: frmIPaddrFilter.RadioAddTempList.Checked:=True;
    mBlockList: frmIPaddrFilter.RadioAddBlockList.Checked:=True;
  end;

  frmIPaddrFilter.ShowModal;

end;
function TFrmMain.IsBlockIP(sIPaddr: String): Boolean;
var
  i:Integer;
  IPaddr  :pTSockaddr;
  nIPaddr :Integer;
begin
  Result:=False;
  nIPaddr:=inet_addr(PChar(sIPaddr));
  for I := 0 to TempBlockIPList.Count - 1 do begin
    IPaddr:=TempBlockIPList.Items[I];
    if IPaddr.nIPaddr = nIPaddr then begin
      Result:=True;
      exit;
    end;
  end;
  for I := 0 to BlockIPList.Count - 1 do begin
    IPaddr:=BlockIPList.Items[I];
    if IPaddr.nIPaddr = nIPaddr then begin
      Result:=True;
      exit;
    end;
  end;
end;
{function TFrmMain.IsConnLimited(sIPaddr: String): Boolean;
var
  i,nCount:Integer;
begin
  nCount:=0;
  Result:=False;
  for I := 0 to ServerSocket.Socket.ActiveConnections - 1 do begin
    if CompareText(sIPaddr,ServerSocket.Socket.Connections[i].RemoteAddress) = 0 then Inc(nCount);
  end;
  if nCount > nMaxConnOfIPaddr then begin
    Result:=True;
  end;
end;}
function TFrmMain.IsConnLimited(sIPaddr: String): Boolean;
var
  I       :Integer;
  IPaddr  :pTSockaddr;
  nIPaddr :Integer;
  boDenyConnect:Boolean;
begin
  Result:=False;
  boDenyConnect:=False;
  nIPaddr:=inet_addr(PChar(sIPaddr));
  for I := 0 to CurrIPaddrList.Count - 1 do begin
    IPaddr:=CurrIPaddrList.Items[I];
    if IPaddr.nIPaddr = nIPaddr then begin
      Inc(IPaddr.nCount);
      if GetTickCount - IPaddr.dwIPCountTick1 < 1000 then begin
        Inc(IPaddr.nIPCount1);
        if IPaddr.nIPCount1 >= nIPCountLimit1 then boDenyConnect:=True;
      end else begin
        IPaddr.dwIPCountTick1:=GetTickCount();
        IPaddr.nIPCount1:=0;
      end;
      if GetTickCount - IPaddr.dwIPCountTick2 < 3000 then begin
        Inc(IPaddr.nIPCount2);
        if IPaddr.nIPCount2 >= nIPCountLimit2 then boDenyConnect:=True;
      end else begin
        IPaddr.dwIPCountTick2:=GetTickCount();
        IPaddr.nIPCount2:=0;
      end;
      if IPaddr.nCount > nMaxConnOfIPaddr then boDenyConnect:=True;
        
      Result:=boDenyConnect;
      exit;
    end;
  end;
  New(IPaddr);
  FillChar(IPaddr^,SizeOf(TSockaddr),0);
  IPaddr.nIPaddr:=nIPaddr;
  IPaddr.nCount:=1;
  CurrIPaddrList.Add(IPaddr);
end;
procedure TFrmMain.ShowMainLogMsg;
var
  i:Integer;
begin
  if (GetTickCount - dwShowMainLogTick) < 200 then exit;
  dwShowMainLogTick:=GetTickCount();
  try
    boShowLocked:=True;
    try
      CS_MainLog.Enter;
      for i:= 0 to MainLogMsgList.Count - 1 do begin
        TempLogList.Add(MainLogMsgList.Strings[i]);
      end;
      MainLogMsgList.Clear;
    finally
      CS_MainLog.Leave;
    end;
    for i:= 0 to TempLogList.Count - 1 do begin
      MemoLog.Lines.Add(TempLogList.Strings[i]);
    end;
    TempLogList.Clear;
  finally
    boShowLocked:=False;
  end;
end;
procedure TFrmMain.MyMessage(var MsgData: TWmCopyData);
var
  sData:String;
  wIdent:Word;
begin
  wIdent:=HiWord(MsgData.From);
  sData:=StrPas(MsgData.CopyDataStruct^.lpData);
  case wIdent of  
    GS_QUIT: begin
    showmessage('quit');
      if boServiceStart then begin
        StartTimer.Enabled:=True;
      end else begin
        boClose:=True;
        Close();
      end;
    end;
    1: ;
    2: ;
    3: ;
  end;   
end;


{---- Adjust global SVN revision ----}
procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
  ini: TMemIniFile;
begin
  ini := TMemIniFile.Create(PosFile);
  ini.WriteInteger('LoginGate','Left',frmMain.Left);
  ini.WriteInteger('LoginGate','Top',frmMain.Top);
  ini.UpdateFile;
  ini.Free; 
end;

procedure TFrmMain.CnTrayIconDblClick(Sender: TObject);
begin
 show;
end;

initialization
  SVNRevision('$Id: Main.pas 536 2006-12-22 17:11:50Z damian $');
end.

⌨️ 快捷键说明

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