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

📄 idsrvclient.pas

📁 翎风世界..传奇服务端..DELPHI源代码 包括DBServer,LogDataServer,LoginGate,LoginSrv,M2Server等..内容齐全.
💻 PAS
📖 第 1 页 / 共 2 页
字号:
    on e: Exception do begin
      MainOutMessage(format(sExceptionMsg,[0]));
      MainOutMessage(E.Message);
    end;
end;

end;
procedure TFrmIDSoc.ClearEmptySession;
var
  I: Integer;
  SessInfo:pTSessInfo;
begin
  m_SessionList.Lock;
  try
    for I := m_SessionList.Count - 1 downto 0 do begin
      SessInfo:=m_SessionList.Items[I];
      if SessInfo.nRefCount <= 0 then begin
        Dispose(SessInfo);
        m_SessionList.Delete(I);
        Continue;
      end;
      {
      if GetTickCount - SessInfo.dwActiveTick > 10 * 60 * 1000 then begin
        Dispose(SessInfo);
        m_SessionList.Delete(I);
        Continue;
      end;
      }
    end;
  finally
    m_SessionList.UnLock;
  end;
end;

procedure TFrmIDSoc.ClearSession;
var
  I: Integer;
begin
  m_SessionList.Lock;
  try
    for I := 0 to m_SessionList.Count - 1 do begin
      Dispose(pTSessInfo(m_SessionList.Items[I]));
    end;
    m_SessionList.Clear;
  finally
    m_SessionList.UnLock;
  end;

end;

function TFrmIDSoc.GetAdmission(sAccount,sIPaddr:String;nSessionID:Integer;var nPayMode:Integer;var nPayMent:Integer):pTSessInfo;//0048DE80
var
  I        :Integer;
  nCount   :Integer;
  SessInfo :pTSessInfo;
  boFound  :Boolean;
ResourceString
  sGetFailMsg = '[Exception] Failed to get admission (%s/%s/%d)';
begin

//  Result:=3;
//  exit;
  boFound:=False;
  Result:=nil;
  nPayMent:=0;
  nPayMode:=0;
  m_SessionList.Lock;
  try

    for i:=0 to m_SessionList.Count -1 do begin
      SessInfo:=m_SessionList.Items[i];

      if (SessInfo.nSessionID = nSessionID) and
         (SessInfo.sAccount = sAccount) {and (SessInfo.sIPaddr = sIPaddr)} then begin
        //if SessInfo.nSessionStatus <> 0 then break;
        //SessInfo.nSessionStatus:=1;

        case SessInfo.nPayMent of
          2: nPayMent:=3;
          1: nPayMent:=2;
          0: nPayMent:=1;
        end;
        Result:=SessInfo;
        nPayMode:=SessInfo.nPayMode;
        boFound:=True;

        break;
      end;
    end;

  finally
    m_SessionList.UnLock;
  end;

  if g_Config.boViewAdmissionFailure and not boFound then begin
    MainOutMessage(format(sGetFailMsg,[sAccount,sIPaddr,nSessionID]));
  end;

end;

procedure TFrmIDSoc.SetTotalHumanCount(sData: String);//0048E014
begin
  g_nTotalHumCount:=Str_ToInt(sData,0)
end;

procedure TFrmIDSoc.GetCancelAdmissionA(sData:String);//0048E06C
var
  nSessionID:Integer;
  sSessionID:String;
  sAccount:String;
ResourceString
  sExceptionMsg = '[Exception] FrmIdSoc::GetCancelAdmissionA';
begin
  try
    sSessionID:=GetValidStr3(sData,sAccount,['/']);
    nSessionID:=Str_ToInt(sSessionID,0);
    if not g_Config.boTestServer then begin
      UserEngine.HumanExpire(sAccount);
      DelSession(nSessionID);
    end;
  except
    MainOutMessage(sExceptionMsg);
  end;
end;


procedure TFrmIDSoc.GetServerLoad(sData: String);//0048E174
var
  sC,s10,s14,s18,s1C:String;
begin
    sData:=GetValidStr3(sData,sC,['/']);
    sData:=GetValidStr3(sData,s10,['/']);
    sData:=GetValidStr3(sData,s14,['/']);
    sData:=GetValidStr3(sData,s18,['/']);
    sData:=GetValidStr3(sData,s1C,['/']);
    nCurrentMonthly:=Str_ToInt(sC,0);
    nLastMonthlyTotalUsage:=Str_ToInt(s10,0);
    nTotalTimeUsage:=Str_ToInt(s14,0);
    nGrossTotalCnt:=Str_ToInt(s18,0);
    nGrossResetCnt:=Str_ToInt(s1C,0);
end;
procedure TFrmIDSoc.IDSocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  g_Config.boIDSocketConnected:=True;
  MainOutMessage('LoginServer Connected(' + Socket.RemoteAddress + ':' +  IntToStr(Socket.RemotePort) + ')');
end;

procedure TFrmIDSoc.IDSocketDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  if g_Config.boIDSocketConnected then begin
    ClearSession();
    g_Config.boIDSocketConnected:=False;
    MainOutMessage('LoginServer Disconnected(' + Socket.RemoteAddress + ':' +  IntToStr(Socket.RemotePort) + ')');
  end;
end;
{$IF IDSOCKETMODE = TIMERENGINE}
procedure TFrmIDSoc.Close;
begin
  Timer1.Enabled:=False;
  IDSocket.Active:=False;
end;
{$ELSE}
procedure TFrmIDSoc.Close;
var
  ThreadInfo:pTThreadInfo;
begin
  ThreadInfo:=@g_Config.IDSocketThread;
  ThreadInfo.boTerminaled:=True;
  if WaitForSingleObject(ThreadInfo.hThreadHandle,1000) <> 0 then begin
    SuspendThread(ThreadInfo.hThreadHandle);
  end;
end;
{$IFEND}
function TFrmIDSoc.GetSessionCount: Integer;
begin
  Result:=0;
  m_SessionList.Lock;
  try
    Result:=m_SessionList.Count;
  finally
    m_SessionList.UnLock;
  end;
end;

procedure TFrmIDSoc.GetSessionList(List:TList);
var
  I: Integer;
begin
  m_SessionList.Lock;
  try
    for I := 0 to m_SessionList.Count - 1 do begin
      List.Add(m_SessionList.Items[I]);
    end;
  finally
    m_SessionList.UnLock;
  end;
end;

procedure IDSocketRead(Config:pTM2Config);
var
  dwReceiveTimeTick:LongWord;
  nReceiveTime:Integer;
  sRecvText:String;
  nRecvLen:Integer;
  nRet:Integer;
begin
  if Config.IDSocket = INVALID_SOCKET then exit;

  dwReceiveTimeTick:=GetTickCount();
  nRet:=ioctlsocket(Config.IDSocket, FIONREAD, nRecvLen);
  if (nRet = SOCKET_ERROR) or (nRecvLen = 0) then begin
    nRet:=WSAGetLastError;
    Config.IDSocket:=INVALID_SOCKET;
    Sleep(100);
    Config.boIDSocketConnected:=False;
    exit;
  end;
  SetLength(sRecvText,nRecvLen);
  nRecvLen:=recv(Config.IDSocket,Pointer(sRecvText)^,nRecvLen,0);
  SetLength(sRecvText, nRecvLen);

  Inc(Config.nIDSocketRecvIncLen,nRecvLen);
  if (nRecvLen <> SOCKET_ERROR) and (nRecvLen > 0) then begin
    if nRecvLen > Config.nIDSocketRecvMaxLen then Config.nIDSocketRecvMaxLen:=nRecvLen;
    EnterCriticalSection(Config.UserIDSection);
    try
      Config.sIDSocketRecvText:=Config.sIDSocketRecvText + sRecvText;
    finally
      LeaveCriticalSection(Config.UserIDSection);
    end;
    FrmIDSoc.Run;
  end;
  Inc(Config.nIDSocketRecvCount);
  nReceiveTime:=GetTickCount - dwReceiveTimeTick;
  if Config.nIDReceiveMaxTime < nReceiveTime then Config.nIDReceiveMaxTime:=nReceiveTime;
end;
procedure IDSocketProcess(Config:pTM2Config;ThreadInfo:pTThreadInfo);
var
  s          :TSocket;
  name       :sockaddr_in;
  HostEnt    :PHostEnt;
  argp       :LongInt;
  readfds    :TFDSet;
  timeout    :TTimeVal;
  nRet       :Integer;
  boRecvData :BOOL;
  nRunTime   :Integer;
  dwRunTick  :LongWord;
ResourceString                    
  sIDServerConnected = 'LoginServer Connected(%s:%d)';
begin
  s:=INVALID_SOCKET;
  if Config.IDSocket <> INVALID_SOCKET then
    s:= Config.IDSocket;
  dwRunTick:=GetTickCount();
  ThreadInfo.dwRunTick:=dwRunTick;
  boRecvData:=False;
  while True do begin
    if ThreadInfo.boTerminaled then break;
    if not boRecvData then Sleep(1)
    else Sleep(0);
    boRecvData:=False;
    nRunTime:=GetTickCount - ThreadInfo.dwRunTick;
    if ThreadInfo.nRunTime < nRunTime then ThreadInfo.nRunTime:=nRunTime;
    if ThreadInfo.nMaxRunTime < nRunTime then ThreadInfo.nMaxRunTime:=nRunTime;
    if GetTickCount - dwRunTick >= 1000 then begin
      dwRunTick:=GetTickCount();
      if ThreadInfo.nRunTime > 0 then Dec(ThreadInfo.nRunTime);
    end;

    ThreadInfo.dwRunTick:=GetTickCount();
    ThreadInfo.boActived:=True;
    ThreadInfo.nRunFlag:=125;
    if (Config.IDSocket = INVALID_SOCKET) or (s = INVALID_SOCKET) then begin
      if Config.IDSocket <> INVALID_SOCKET then begin
        Config.IDSocket := INVALID_SOCKET;
        Sleep(100);
        ThreadInfo.nRunFlag:=126;
        Config.boIDSocketConnected:=False;
      end;
      if s <> INVALID_SOCKET then begin
        closesocket(s);
        s:=INVALID_SOCKET;
      end;
      if Config.sIDSAddr = '' then Continue;
        
      s:=socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
      if s = INVALID_SOCKET then Continue;

      ThreadInfo.nRunFlag:=127;

      HostEnt:=gethostbyname(PChar(@Config.sIDSAddr[1]));
      if HostEnt = nil then Continue;

      PInteger(@name.sin_addr.S_addr)^:=PInteger(HostEnt.h_addr^)^;
      name.sin_family:=HostEnt.h_addrtype;
      name.sin_port:=htons(Config.nIDSPort);
      name.sin_family:=PF_INET;

      ThreadInfo.nRunFlag:=128;
      if connect(s,name,SizeOf(name)) = SOCKET_ERROR then begin
        nRet:=WSAGetLastError;

        closesocket(s);
        s:=INVALID_SOCKET;
        Continue;
      end;

      argp:=1;
      if ioctlsocket(s,FIONBIO,argp) = SOCKET_ERROR then begin
        closesocket(s);
        s:=INVALID_SOCKET;
        Continue;
      end;
      ThreadInfo.nRunFlag:=129;
      Config.IDSocket:=s;
      Config.boIDSocketConnected:=True;
      MainOutMessage(format(sIDServerConnected,[Config.sIDSAddr,Config.nIDSPort]));
    end;
    readfds.fd_count:=1;
    readfds.fd_array[0]:=s;
    timeout.tv_sec:=0;
    timeout.tv_usec:=20;
    ThreadInfo.nRunFlag:=130;
    nRet:=select(0,@readfds,nil,nil,@timeout);
    if nRet = SOCKET_ERROR then begin
      ThreadInfo.nRunFlag:=131;
      nRet:=WSAGetLastError;
      if nRet = WSAEWOULDBLOCK then begin
        Sleep(10);
        Continue;
      end;
      ThreadInfo.nRunFlag:=132;
      nRet:=WSAGetLastError;
      Config.nIDSocketWSAErrCode:=nRet - WSABASEERR;
      Inc(Config.nIDSocketErrorCount);
      Config.DBSocket:=INVALID_SOCKET;
      Sleep(100);
      Config.boIDSocketConnected:=False;
      closesocket(s);
      s:=INVALID_SOCKET;
      Continue;
    end;
    boRecvData:=True;
    ThreadInfo.nRunFlag:=133;
    while (nRet > 0) do begin
      IDSocketRead(Config);
      Dec(nRet);
    end;
  end;
  if Config.IDSocket <> INVALID_SOCKET then begin
    Config.IDSocket:=INVALID_SOCKET;
    Config.boIDSocketConnected:=False;
  end;
  if s <> INVALID_SOCKET then begin
    closesocket(s);
  end;
end;
procedure IDSocketThread(ThreadInfo:pTThreadInfo);stdcall;
var
  nErrorCount:Integer;
ResourceString
  sExceptionMsg = '[Exception] DBSocketThread ErrorCount = %d';
begin
  nErrorCount:=0;
  while True do begin
    try
      IDSocketProcess(ThreadInfo.Config,ThreadInfo);
      break;
    except
      Inc(nErrorCount);
      if nErrorCount > 10 then break;
      MainOutMessage(format(sExceptionMsg,[nErrorCount]));
    end;
  end;
  ExitThread(0);
end;


end.

⌨️ 快捷键说明

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