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

📄 main.pas

📁 传奇服务端代码 RunGate.rar 通讯代码
💻 PAS
📖 第 1 页 / 共 4 页
字号:
      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.FormCreate(Sender: TObject);
begin
  TempLogList:=TStringList.Create;
  dwLoopCheckTick:=GetTickCount();
end;

procedure TFrmMain.FormDestroy(Sender: TObject);
begin
  BlockIPList.SaveToFile('.\BlockIPList.txt');
  TempLogList.Free;
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.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.TimerTimer(Sender: TObject);
begin
  if ServerSocket.Active then begin
    StatusBar.Panels[0].Text:=IntToStr(ServerSocket.Port);
    POPMENU_PORT.Caption:=IntToStr(ServerSocket.Port);
    if boSendHoldTimeOut then begin
      StatusBar.Panels[2].Text:=IntToStr(SessionCount) + '/#' + IntToStr(ServerSocket.Socket.ActiveConnections);
      POPMENU_CONNCOUNT.Caption:=IntToStr(SessionCount) + '/#' + IntToStr(ServerSocket.Socket.ActiveConnections);
    end else begin
      StatusBar.Panels[2].Text:=IntToStr(SessionCount) + '/' + IntToStr(ServerSocket.Socket.ActiveConnections);
      POPMENU_CONNCOUNT.Caption:=IntToStr(SessionCount) + '/' + IntToStr(ServerSocket.Socket.ActiveConnections);
    end;
  end else begin
    StatusBar.Panels[0].Text:='Port';
    StatusBar.Panels[2].Text:='Status';
    POPMENU_CONNCOUNT.Caption:='Count';
  end;
end;



procedure TFrmMain.RestSessionArray;
var
  i:Integer;
  tSession:pTSessionInfo;
begin
  for i:=0 to GATEMAXSESSION - 1 do begin
    tSession:=@SessionArray[i];
    tSession.Socket          := nil;
    tSession.sSocData        := '';
    tSession.sSendData       := '';
    tSession.nUserListIndex  := 0;
    tSession.nPacketIdx      := -1;
    tSession.nPacketErrCount := 0;
    tSession.boStartLogon    := True;
    tSession.boSendLock      := False;
    tSession.boOverNomSize   := False;
    tSession.nOverNomSizeCount   := 0;
    tSession.dwSendLatestTime:= GetTickCount();
    tSession.boSendAvailable := True;
    tSession.boSendCheck     := False;
    tSession.nCheckSendLength:= 0;
    tSession.nReceiveLength  := 0;
    tSession.dwReceiveTick   := GetTickCount();
    tSession.nSckHandle      := -1;
    tSession.dwSayMsgTick    :=GetTickCount();
  end;
end;

procedure TFrmMain.ServerSocketClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  nSockIdx:integer;
  sRemoteAddress:String;
  UserSession:pTSessionInfo;
begin
  Socket.nIndex:=-1;
  sRemoteAddress:=Socket.RemoteAddress;
  if boGateReady then begin
    try
      for nSockIdx:=0 to GATEMAXSESSION - 1 do begin
        UserSession:=@SessionArray[nSockIdx];
        if UserSession.Socket = nil then begin
           UserSession.Socket:=Socket;
           UserSession.sSocData:='';
           UserSession.sSendData:='';
           UserSession.nUserListIndex:=0;
           UserSession.nPacketIdx:=-1;
           UserSession.nPacketErrCount:=0;
           UserSession.boStartLogon:=True;
           UserSession.boSendLock:=False;
           UserSession.dwSendLatestTime:=GetTickCount();
           UserSession.boSendAvailable:=True;
           UserSession.boSendCheck:=False;
           UserSession.nCheckSendLength:=0;
           UserSession.nReceiveLength:=0;
           UserSession.dwReceiveTick:=GetTickCount();
           UserSession.nSckHandle:=Socket.SocketHandle;
           UserSession.sRemoteAddr:=sRemoteAddress;
           UserSession.boOverNomSize:=False;
           UserSession.nOverNomSizeCount:=0;
           UserSession.dwSayMsgTick:=GetTickCount();
           Socket.nIndex:=nSockIdx;
           Inc(SessionCount);
           Break;
        end;
      end;
    finally

    end;
    if nSockIdx < GATEMAXSESSION then begin
      SendServerMsg(GM_OPEN,nSockIdx,Socket.SocketHandle, 0,Length(Socket.RemoteAddress)+ 1,PChar(Socket.RemoteAddress));
      Socket.nIndex:=nSockIdx;
      AddMainLogMsg('Connected: ' + sRemoteAddress,5);
    end else begin
      Socket.nIndex:= -1;
      Socket.Close;
      AddMainLogMsg('Connected: ' + sRemoteAddress,1);
    end;
  end else begin
    Socket.nIndex:= -1;
    Socket.Close;
    AddMainLogMsg('Connected: ' + sRemoteAddress,1);
  end;
end;

procedure TFrmMain.ServerSocketClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  nSockIndex:Integer;
  sRemoteAddr:String;
  UserSession:pTSessionInfo;
begin
  sRemoteAddr:=Socket.RemoteAddress;
  nSockIndex:=Socket.nIndex;
  if (nSockIndex >= 0) and (nSockIndex < GATEMAXSESSION) then begin
    UserSession:=@SessionArray[nSockIndex];
    UserSession.Socket:=nil;
    UserSession.nSckHandle:= -1;
    UserSession.sSocData:='';
    UserSession.sSendData:='';
    Socket.nIndex:= -1;
    Dec(SessionCount);
    if boGateReady then begin
      SendServerMsg(GM_CLOSE,0,Socket.SocketHandle,0,0,nil);
      AddMainLogMsg('Disconnected: ' + Socket.RemoteAddress,5);
    end;
  end;
end;

procedure TFrmMain.ServerSocketClientError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
//  AddMainLogMsg('连接错误: ' + Socket.RemoteAddress,2);
  ErrorCode:=0;
  Socket.Close;
end;

procedure TFrmMain.ServerSocketClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  dwProcessMsgTick,dwProcessMsgTime:LongWord;
  nReviceLen:Integer;
  sReviceMsg:String;
  sRemoteAddress:String;
  nSocketIndex:Integer;
  nPos:Integer;
  UserData:pTSendUserData;
  nMsgCount:Integer;
  UserSession:pTSessionInfo;
begin
  try
    dwProcessMsgTick:=GetTickCount();
    //nReviceLen:=Socket.ReceiveLength;
    sRemoteAddress:=Socket.RemoteAddress;
    nSocketIndex:=Socket.nIndex;
    sReviceMsg:=Socket.ReceiveText;
    nReviceLen:=Length(sReviceMsg);
    if (nSocketIndex >= 0) and (nSocketIndex < GATEMAXSESSION) and (sReviceMsg <> '') and boServerReady then begin
      if nReviceLen > nNomClientPacketSize then begin
        nMsgCount:=TagCount(sReviceMsg,'!');
        if (nMsgCount > nMaxClientMsgCount) or
           (nReviceLen > nMaxClientPacketSize) then begin
          if bokickOverPacketSize then begin
            case BlockMethod of    //
              mDisconnect: begin

               end;
              mBlock: begin
                TempBlockIPList.Add(sRemoteAddress);
                CloseConnect(sRemoteAddress);
              end;
              mBlockList: begin
                BlockIPList.Add(sRemoteAddress);
                CloseConnect(sRemoteAddress);
              end;
            end;
            AddMainLogMsg('Read: IP(' + sRemoteAddress + '),MsgCount(' +IntToStr(nMsgCount) + '),RecvLen(' + IntToStr(nReviceLen) + ')' , 1);
            Socket.Close;
          end;
          exit;
        end;
      end;
      Inc(nReviceMsgSize,Length(sReviceMsg));
      if boShowSckData then AddMainLogMsg(sReviceMsg,0);
      UserSession:=@SessionArray[nSocketIndex];
      if UserSession.Socket = Socket then begin
        nPos:=Pos('*',sReviceMsg);
        if nPos > 0 then begin
          UserSession.boSendAvailable:=True;
          UserSession.boSendCheck:=False;
          UserSession.nCheckSendLength:=0;
          UserSession.dwReceiveTick:=GetTickCount();
          sReviceMsg:=Copy(sReviceMsg,1,nPos -1 ) + Copy(sReviceMsg,nPos +1,Length(sReviceMsg));
        end;//00456DD0
        if (sReviceMsg <> '') and boGateReady and not boCheckServerFail then begin
          New(UserData);
          UserData.nSocketIdx:=nSocketIndex;
          UserData.nSocketHandle:=Socket.SocketHandle;
          UserData.sMsg:=sReviceMsg;
          ReviceMsgList.Add(UserData);
        end;//00456E2A
      end;
    end;
    dwProcessMsgTime:=GetTickCount - dwProcessMsgTick;
    if dwProcessMsgTime > dwProcessClientMsgTime then dwProcessClientMsgTime:=dwProcessMsgTime;
  except
    AddMainLogMsg('[Exception] ClientRead',1);
  end;
end;

procedure TFrmMain.MemoLogChange(Sender: TObject);
begin
  if MemoLog.Lines.Count > 500 then MemoLog.Clear;
end;

procedure TFrmMain.SendTimerTimer(Sender: TObject);
var
  i:integer;
  UserSession:pTSessionInfo;
begin
  if (GetTickCount - dwSendHoldTick) > 3000 then begin
    boSendHoldTimeOut:=False;
  end;//457195
  if boGateReady and not boCheckServerFail then begin
    for i:=0 to GATEMAXSESSION - 1 do begin
      UserSession:=@SessionArray[i];
      if UserSession.Socket <> nil then begin
        if (GetTickCount - UserSession.dwReceiveTick) > dwSessionTimeOutTime then begin
          UserSession.Socket.Close;
          UserSession.Socket:=nil;
          UserSession.nSckHandle:=-1;
        end;
      end;
    end;
  end;//0045722F
  if not boGateReady then begin
    StatusBar.Panels[1].Text:='未连接';
    StatusBar.Panels[3].Text:='????';
    POPMENU_CHECKTICK.Caption:='????';
    if ((GetTickCount - dwReConnectServerTime) > 1000{30 * 1000}) and boServiceStart then begin
      dwReConnectServerTime:=GetTickCount();
      ClientSocket.Active:=False;
      ClientSocket.Address:=ServerAddr;
      ClientSocket.Port:=ServerPort;
      ClientSocket.Active:=True;
    end;
  end else begin//00457302
    if boCheckServerFail then begin
      StatusBar.Panels[1].Text:='超间';
    end else begin//00457320
      StatusBar.Panels[1].Text:='已连接';
      LbLack.Caption:=IntToStr(n45AA84) + '/' + IntToStr(n45AA80);
    end;
    dwCheckServerTimeMin:=GetTickCount - dwCheckServerTick;
    if dwCheckServerTimeMax < dwCheckServerTimeMin then dwCheckServerTimeMax:=dwCheckServerTimeMin;

    StatusBar.Panels[3].Text:=IntToStr(dwCheckServerTimeMin) + '/' + IntToStr(dwCheckServerTimeMax);
    POPMENU_CHECKTICK.Caption:=IntToStr(dwCheckServerTimeMin) + '/' + IntToStr(dwCheckServerTimeMax);
  end;
end;

procedure TFrmMain.ClientSocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
//00454C08
begin
  boGateReady:=True;
  dwCheckServerTick:=GetTickCount();
  dwCheckRecviceTick:=GetTickCount();
  RestSessionArray();
  boServerReady:=True;
  dwCheckServerTimeMax:=0;
  dwCheckServerTimeMax:=0;
end;

procedure TFrmMain.ClientSocketDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
//00454CA8
var
  i:Integer;
  UserSession:pTSessionInfo;
begin
  for i:=0 to GATEMAXSESSION - 1 do begin
    UserSession:=@SessionArray[i];
    if UserSession.Socket <> nil then begin
      UserSession.Socket.Close;
      UserSession.Socket:=nil;
      UserSession.nSckHandle:=-1;
    end;
  end;
  RestSessionArray();
  if SocketBuffer <> nil then begin
    FreeMem(SocketBuffer);
  end;
  SocketBuffer:=nil;

  for i:=0 to List_45AA58.Count -1 do begin
    //00454D6F

⌨️ 快捷键说明

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