<commit_before>unit diocp_ex_websocketclient;

interface

uses
  diocp_tcp_client, diocp_ex_http_common, SysUtils, Classes, utils_url,
  utils_websocket, utils_strings, diocp_core_rawWinSocket;

type
  TDiocpWebSocketContext = class(TIocpRemoteContext)
  private
    FSendPingTick:Cardinal;
    FURL: TURL;
    FHeaderBuilder: THttpHeaderBuilder;
    /// <summary>
    ///  WebSocketյ
    /// </summary>
    FWebSocketContentBuffer: TDBufferBuilder;

    FWsFrame: TDiocpWebSocketFrame;
    FHttpBuffer: THttpBuffer;
    FOnRecv: TNotifyEvent;
    FOnShakeHand: TNotifyEvent;

    FRecvShakeHand:Byte;
    FOnDisconnectedEvent: TNotifyEvent;
    procedure PostWebSocketRequest;
  protected
    procedure OnConnected; override;
    procedure OnDisconnected; override;
    procedure OnRecvBuffer(buf: Pointer; len: Cardinal; ErrCode: WORD); override;
    procedure DoRecv();
  public
    constructor Create; override;
    destructor Destroy; override;
  public
    procedure Open(WsUrl:string);

    procedure SendPing;

    procedure CheckSendPing(pvInterval: Cardinal = 20000);

    procedure SendBuffer(buf: Pointer; len: Cardinal; opcode: Byte);

    procedure SendText(const s:string);

    property HeaderBuilder: THttpHeaderBuilder read FHeaderBuilder;
    property HttpBuffer: THttpBuffer read FHttpBuffer;
    property OnDisconnectedEvent: TNotifyEvent read FOnDisconnectedEvent write
        FOnDisconnectedEvent;


    property WebSocketContentBuffer: TDBufferBuilder read FWebSocketContentBuffer;
    property OnRecv: TNotifyEvent read FOnRecv write FOnRecv;
    property OnShakeHand: TNotifyEvent read FOnShakeHand write FOnShakeHand;


  end;

  TDiocpWebSocketTcpClient = class(TDiocpTcpClient)
  public
    constructor Create(AOwner: TComponent); override;
  end;


procedure DoInitializeWebSocketClient;
procedure DoFinalizeWebSocketClient;

function NewWsClient: TDiocpWebSocketContext;

  

implementation

var
  __webtcpClient:TDiocpWebSocketTcpClient;

procedure DoInitializeWebSocketClient;
begin
   __webtcpClient := TDiocpWebSocketTcpClient.Create(nil);
   __webtcpClient.Open;
end;

procedure DoFinalizeWebSocketClient;
begin
  __webtcpClient.Close;
  __webtcpClient.Free;
end;

function NewWsClient: TDiocpWebSocketContext;
begin
  Result := TDiocpWebSocketContext(__webtcpClient.Add);
end;

constructor TDiocpWebSocketContext.Create;
begin
  inherited Create;
  FHeaderBuilder := THttpHeaderBuilder.Create();
  FURL := TURL.Create();
  FWsFrame := TDiocpWebSocketFrame.Create();
  FHttpBuffer := THttpBuffer.Create();
  FWebSocketContentBuffer := TDBufferBuilder.Create();
end;

destructor TDiocpWebSocketContext.Destroy;
begin
  FreeAndNil(FWebSocketContentBuffer);
  FreeAndNil(FHttpBuffer);
  FreeAndNil(FWsFrame);
  FreeAndNil(FURL);
  FreeAndNil(FHeaderBuilder);<commit_msg>* 避免多次执行<commit_after>unit diocp_ex_websocketclient;

interface

uses
  diocp_tcp_client, diocp_ex_http_common, SysUtils, Classes, utils_url,
  utils_websocket, utils_strings, diocp_core_rawWinSocket, diocp_core_engine;

type
  TDiocpWebSocketContext = class(TIocpRemoteContext)
  private
    FSendPingTick:Cardinal;
    FURL: TURL;
    FHeaderBuilder: THttpHeaderBuilder;
    /// <summary>
    ///  WebSocketյ
    /// </summary>
    FWebSocketContentBuffer: TDBufferBuilder;

    FWsFrame: TDiocpWebSocketFrame;
    FHttpBuffer: THttpBuffer;
    FOnRecv: TNotifyEvent;
    FOnShakeHand: TNotifyEvent;

    FRecvShakeHand:Byte;
    FOnDisconnectedEvent: TNotifyEvent;
    procedure PostWebSocketRequest;
  protected
    procedure OnConnected; override;
    procedure OnDisconnected; override;
    procedure OnRecvBuffer(buf: Pointer; len: Cardinal; ErrCode: WORD); override;
    procedure DoRecv();
  public
    constructor Create; override;
    destructor Destroy; override;
  public
    procedure Open(WsUrl:string);

    procedure SendPing;

    procedure CheckSendPing(pvInterval: Cardinal = 20000);

    procedure SendBuffer(buf: Pointer; len: Cardinal; opcode: Byte);

    procedure SendText(const s:string);

    property HeaderBuilder: THttpHeaderBuilder read FHeaderBuilder;
    property HttpBuffer: THttpBuffer read FHttpBuffer;
    property OnDisconnectedEvent: TNotifyEvent read FOnDisconnectedEvent write
        FOnDisconnectedEvent;


    property WebSocketContentBuffer: TDBufferBuilder read FWebSocketContentBuffer;
    property OnRecv: TNotifyEvent read FOnRecv write FOnRecv;
    property OnShakeHand: TNotifyEvent read FOnShakeHand write FOnShakeHand;


  end;

  TDiocpWebSocketTcpClient = class(TDiocpTcpClient)
  public
    constructor Create(AOwner: TComponent); override;
  end;


procedure DoInitializeWebSocketClient;
procedure DoFinalizeWebSocketClient;

function NewWsClient: TDiocpWebSocketContext;

  

implementation

var
  __webtcpClient:TDiocpWebSocketTcpClient;

procedure DoInitializeWebSocketClient;
begin
  if __webtcpClient = nil then
  begin
     __webtcpClient := TDiocpWebSocketTcpClient.Create(nil);
     __webtcpClient.Open;
  end;
end;

procedure DoFinalizeWebSocketClient;
begin
  if __webtcpClient <> nil then
  begin
    Assert(__defaultDiocpEngine <> nil, 'iocp engine is null');

    __webtcpClient.Close;
    __webtcpClient.Free;
    __webtcpClient := nil;
  end;
end;

function NewWsClient: TDiocpWebSocketContext;
begin
  Result := TDiocpWebSocketContext(__webtcpClient.Add);
end;

constructor TDiocpWebSocketContext.Create;
begin
  inherited Create;
  FHeaderBuilder := THttpHeaderBuilder.Create();
  FURL := TURL.Create();
  FWsFrame := TDiocpWebSocketFrame.Create();
  FHttpBuffer := THttpBuffer.Create();
  FWebSocketContentBuffer := TDBufferBuilder.Create();
end;

destructor TDiocpWebSocketContext.Destroy;
begin
  FreeAndNil(FWebSocketContentBuffer);
  FreeAndNil(FHttpBuffer);
  FreeAndNil(FWsFrame);
  FreeAndNil(FURL);
  FreeAndNil(FHeaderBuilder);
{'fraction_non_alphanumeric': 0.0705073086844368, 'fraction_numerical': 0.0017196904557179708, 'mean_word_length': 5.226980728051392, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6420704', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1848, 'n_words': 383}