home *** CD-ROM | disk | FTP | other *** search
/ PC World Plus! (NZ) 2001 June / HDC50.iso / Runimage / Delphi50 / Doc / SCKTCOMP.INT < prev    next >
Text File  |  1999-08-11  |  13KB  |  354 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {       Windows socket components                       }
  6. {                                                       }
  7. {       Copyright (c) 1997,99 Inprise Corporation       }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit ScktComp;
  12.  
  13. interface
  14.  
  15. uses SysUtils, Windows, Messages, Classes, WinSock, SyncObjs;
  16.  
  17. const
  18.   CM_SOCKETMESSAGE = WM_USER + $0001;
  19.   CM_DEFERFREE = WM_USER + $0002;
  20.   CM_LOOKUPCOMPLETE = WM_USER + $0003;
  21.  
  22. type
  23.   ESocketError = class(Exception);
  24.  
  25.   TCMSocketMessage = record
  26.     Msg: Cardinal;
  27.     Socket: TSocket;
  28.     SelectEvent: Word;
  29.     SelectError: Word;
  30.     Result: Longint;
  31.   end;
  32.  
  33.   TCMLookupComplete = record
  34.     Msg: Cardinal;
  35.     LookupHandle: THandle;
  36.     AsyncBufLen: Word;
  37.     AsyncError: Word;
  38.     Result: Longint;
  39.   end;
  40.  
  41.   TCustomWinSocket = class;
  42.   TCustomSocket = class;
  43.   TServerAcceptThread = class;
  44.   TServerClientThread = class;
  45.   TServerWinSocket = class;
  46.   TServerClientWinSocket = class;
  47.  
  48.   TServerType = (stNonBlocking, stThreadBlocking);
  49.   TClientType = (ctNonBlocking, ctBlocking);
  50.   TAsyncStyle = (asRead, asWrite, asOOB, asAccept, asConnect, asClose);
  51.   TAsyncStyles = set of TAsyncStyle;
  52.   TSocketEvent = (seLookup, seConnecting, seConnect, seDisconnect, seListen,
  53.     seAccept, seWrite, seRead);
  54.   TLookupState = (lsIdle, lsLookupAddress, lsLookupService);
  55.   TErrorEvent = (eeGeneral, eeSend, eeReceive, eeConnect, eeDisconnect, eeAccept);
  56.  
  57.   TSocketEventEvent = procedure (Sender: TObject; Socket: TCustomWinSocket;
  58.     SocketEvent: TSocketEvent) of object;
  59.   TSocketErrorEvent = procedure (Sender: TObject; Socket: TCustomWinSocket;
  60.     ErrorEvent: TErrorEvent; var ErrorCode: Integer) of object;
  61.   TGetSocketEvent = procedure (Sender: TObject; Socket: TSocket;
  62.     var ClientSocket: TServerClientWinSocket) of object;
  63.   TGetThreadEvent = procedure (Sender: TObject; ClientSocket: TServerClientWinSocket;
  64.     var SocketThread: TServerClientThread) of object;
  65.   TSocketNotifyEvent = procedure (Sender: TObject; Socket: TCustomWinSocket) of object;
  66.  
  67.   TCustomWinSocket = class
  68.   protected
  69.     procedure AsyncInitSocket(const Name, Address, Service: string; Port: Word;
  70.       QueueSize: Integer; Client: Boolean);
  71.     procedure DoOpen;
  72.     procedure DoListen(QueueSize: Integer);
  73.     function InitSocket(const Name, Address, Service: string; Port: Word;
  74.       Client: Boolean): TSockAddrIn;
  75.     procedure Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent); dynamic;
  76.     procedure Error(Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  77.       var ErrorCode: Integer); dynamic;
  78.     procedure SetAsyncStyles(Value: TASyncStyles);
  79.   public
  80.     constructor Create(ASocket: TSocket);
  81.     destructor Destroy; override;
  82.     procedure Close;
  83.     procedure DefaultHandler(var Message); override;
  84.     procedure Lock;
  85.     procedure Unlock;
  86.     procedure Listen(const Name, Address, Service: string; Port: Word;
  87.       QueueSize: Integer; Block: Boolean = True);
  88.     procedure Open(const Name, Address, Service: string; Port: Word; Block: Boolean = True);
  89.     procedure Accept(Socket: TSocket); virtual;
  90.     procedure Connect(Socket: TSocket); virtual;
  91.     procedure Disconnect(Socket: TSocket); virtual;
  92.     procedure Read(Socket: TSocket); virtual;
  93.     procedure Write(Socket: TSocket); virtual;
  94.     function LookupName(const name: string): TInAddr;
  95.     function LookupService(const service: string): Integer;
  96.  
  97.     function ReceiveLength: Integer;
  98.     function ReceiveBuf(var Buf; Count: Integer): Integer;
  99.     function ReceiveText: string;
  100.     function SendBuf(var Buf; Count: Integer): Integer;
  101.     function SendStream(AStream: TStream): Boolean;
  102.     function SendStreamThenDrop(AStream: TStream): Boolean;
  103.     function SendText(const S: string): Integer;
  104.  
  105.     property LocalHost: string;
  106.     property LocalAddress: string;
  107.     property LocalPort: Integer;
  108.  
  109.     property RemoteHost: string;
  110.     property RemoteAddress: string;
  111.     property RemotePort: Integer;
  112.     property RemoteAddr: TSockAddrIn;
  113.  
  114.     property Connected: Boolean;
  115.     property Addr: TSockAddrIn;
  116.     property ASyncStyles: TAsyncStyles;
  117.     property Handle: HWnd;
  118.     property SocketHandle: TSocket;
  119.     property LookupState: TLookupState;
  120.  
  121.     property OnSocketEvent: TSocketEventEvent;
  122.     property OnErrorEvent: TSocketErrorEvent;
  123.  
  124.     property Data: Pointer;
  125.   end;
  126.  
  127.   TClientWinSocket = class(TCustomWinSocket)
  128.   protected
  129.     procedure SetClientType(Value: TClientType);
  130.   public
  131.     procedure Connect(Socket: TSocket); override;
  132.     property ClientType: TClientType;
  133.   end;
  134.  
  135.   TServerClientWinSocket = class(TCustomWinSocket)
  136.   public
  137.     constructor Create(Socket: TSocket; ServerWinSocket: TServerWinSocket);
  138.     destructor Destroy; override;
  139.  
  140.     property ServerWinSocket: TServerWinSocket;
  141.   end;
  142.  
  143.   TThreadNotifyEvent = procedure (Sender: TObject;
  144.     Thread: TServerClientThread) of object;
  145.  
  146.   TServerWinSocket = class(TCustomWinSocket)
  147.   protected
  148.     function DoCreateThread(ClientSocket: TServerClientWinSocket): TServerClientThread; virtual;
  149.     procedure Listen(var Name, Address, Service: string; Port: Word;
  150.       QueueSize: Integer);
  151.     procedure SetServerType(Value: TServerType);
  152.     procedure SetThreadCacheSize(Value: Integer);
  153.     procedure ThreadEnd(AThread: TServerClientThread); dynamic;
  154.     procedure ThreadStart(AThread: TServerClientThread); dynamic;
  155.     function GetClientSocket(Socket: TSocket): TServerClientWinSocket; dynamic;
  156.     function GetServerThread(ClientSocket: TServerClientWinSocket): TServerClientThread; dynamic;
  157.     procedure ClientRead(Socket: TCustomWinSocket); dynamic;
  158.     procedure ClientWrite(Socket: TCustomWinSOcket); dynamic;
  159.     procedure ClientConnect(Socket: TCustomWinSOcket); dynamic;
  160.     procedure ClientDisconnect(Socket: TCustomWinSOcket); dynamic;
  161.     procedure ClientErrorEvent(Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  162.       var ErrorCode: Integer); dynamic;
  163.   public
  164.     constructor Create(ASocket: TSocket);
  165.     destructor Destroy; override;
  166.     procedure Accept(Socket: TSocket); override;
  167.     procedure Disconnect(Socket: TSocket); override;
  168.     function GetClientThread(ClientSocket: TServerClientWinSocket): TServerClientThread;
  169.     property ActiveConnections: Integer;
  170.     property ActiveT;
  171.     property Connections[Index: Integer]: TCustomWinSocket;
  172.     property IdleT;
  173.     property ServerType: TServerType;
  174.     property T;
  175.     property OnGetSocket: TGetSocketEvent;
  176.     property OnGetT;
  177.     property OnT;
  178.     property OnT;
  179.     property OnClientConnect: TSocketNotifyEvent;
  180.     property OnClientDisconnect: TSocketNotifyEvent;
  181.     property OnClientRead: TSocketNotifyEvent;
  182.     property OnClientWrite: TSocketNotifyEvent;
  183.     property OnClientError: TSocketErrorEvent;
  184.   end;
  185.  
  186.   TServerAcceptThread = class(TThread)
  187.   public
  188.     constructor Create(CreateSuspended: Boolean; ASocket: TServerWinSocket);
  189.     procedure Execute; override;
  190.  
  191.     property ServerSocket: TServerWinSocket;
  192.   end;
  193.  
  194.   TServerClientThread = class(TThread)
  195.   protected
  196.     procedure DoTerminate; override;
  197.     procedure Execute; override;
  198.     procedure ClientExecute; virtual;
  199.     procedure Event(SocketEvent: TSocketEvent); virtual;
  200.     procedure Error(ErrorEvent: TErrorEvent; var ErrorCode: Integer); virtual;
  201.     procedure HandleException; virtual;
  202.     procedure ReActivate(ASocket: TServerClientWinSocket);
  203.     function StartConnect: Boolean;
  204.     function EndConnect: Boolean;
  205.   public
  206.     constructor Create(CreateSuspended: Boolean; ASocket: TServerClientWinSocket);
  207.     destructor Destroy; override;
  208.  
  209.     property ClientSocket: TServerClientWinSocket;
  210.     property ServerSocket: TServerWinSocket;
  211.     property KeepInCache: Boolean;
  212.     property Data: Pointer;
  213.   end;
  214.  
  215.   TAbstractSocket = class(TComponent)
  216.   protected
  217.     procedure Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent);
  218.       virtual; abstract;
  219.     procedure Error(Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  220.       var ErrorCode: Integer); virtual; abstract;
  221.     procedure DoActivate(Value: Boolean); virtual; abstract;
  222.     procedure InitSocket(Socket: TCustomWinSocket);
  223.     procedure Loaded; override;
  224.     procedure SetActive(Value: Boolean);
  225.     procedure SetAddress(Value: string);
  226.     procedure SetHost(Value: string);
  227.     procedure SetPort(Value: Integer);
  228.     procedure SetService(Value: string);
  229.     property Active: Boolean;
  230.     property Address: string;
  231.     property Host: string;
  232.     property Port: Integer;
  233.     property Service: string;
  234.   public
  235.     procedure Open;
  236.     procedure Close;
  237.   end;
  238.  
  239.   TCustomSocket = class(TAbstractSocket)
  240.   protected
  241.     procedure Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent); override;
  242.     procedure Error(Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  243.       var ErrorCode: Integer); override;
  244.     property OnLookup: TSocketNotifyEvent;
  245.     property OnConnecting: TSocketNotifyEvent;
  246.     property OnConnect: TSocketNotifyEvent;
  247.     property OnDisconnect: TSocketNotifyEvent;
  248.     property OnListen: TSocketNotifyEvent;
  249.     property OnAccept: TSocketNotifyEvent;
  250.     property OnRead: TSocketNotifyEvent;
  251.     property OnWrite: TSocketNotifyEvent;
  252.     property OnError: TSocketErrorEvent;
  253.   end;
  254.  
  255.   TWinSocketStream = class(TStream)
  256.   public
  257.     constructor Create(ASocket: TCustomWinSocket; TimeOut: Longint);
  258.     destructor Destroy; override;
  259.     function WaitForData(Timeout: Longint): Boolean;
  260.     function Read(var Buffer; Count: Longint): Longint; override;
  261.     function Write(const Buffer; Count: Longint): Longint; override;
  262.     function Seek(Offset: Longint; Origin: Word): Longint; override;
  263.     property TimeOut: Longint;
  264.   end;
  265.  
  266.   TClientSocket = class(TCustomSocket)
  267.   protected
  268.     procedure DoActivate(Value: Boolean); override;
  269.     function GetClientType: TClientType;
  270.     procedure SetClientType(Value: TClientType);
  271.   public
  272.     constructor Create(AOwner: TComponent); override;
  273.     destructor Destroy; override;
  274.     property Socket: TClientWinSocket;
  275.   published
  276.     property Active;
  277.     property Address;
  278.     property ClientType: TClientType;
  279.     property Host;
  280.     property Port;
  281.     property Service;
  282.     property OnLookup;
  283.     property OnConnecting;
  284.     property OnConnect;
  285.     property OnDisconnect;
  286.     property OnRead;
  287.     property OnWrite;
  288.     property OnError;
  289.   end;
  290.  
  291.   TCustomServerSocket = class(TCustomSocket)
  292.   protected
  293.     FServerSocket: TServerWinSocket;
  294.     procedure DoActivate(Value: Boolean); override;
  295.     function GetServerType: TServerType;
  296.     function GetGetThreadEvent: TGetThreadEvent;
  297.     function GetGetSocketEvent: TGetSocketEvent;
  298.     function GetThreadCacheSize: Integer;
  299.     function GetOnThreadStart: TThreadNotifyEvent;
  300.     function GetOnThreadEnd: TThreadNotifyEvent;
  301.     function GetOnClientEvent(Index: Integer): TSocketNotifyEvent;
  302.     function GetOnClientError: TSocketErrorEvent;
  303.     procedure SetServerType(Value: TServerType);
  304.     procedure SetGetThreadEvent(Value: TGetThreadEvent);
  305.     procedure SetGetSocketEvent(Value: TGetSocketEvent);
  306.     procedure SetThreadCacheSize(Value: Integer);
  307.     procedure SetOnThreadStart(Value: TThreadNotifyEvent);
  308.     procedure SetOnThreadEnd(Value: TThreadNotifyEvent);
  309.     procedure SetOnClientEvent(Index: Integer; Value: TSocketNotifyEvent);
  310.     procedure SetOnClientError(Value: TSocketErrorEvent);
  311.     property ServerType: TServerType;
  312.     property T;
  313.     property OnGetT;
  314.     property OnGetSocket: TGetSocketEvent;
  315.     property OnT;
  316.     property OnT;
  317.     property OnClientConnect: TSocketNotifyEvent index 2;
  318.     property OnClientDisconnect: TSocketNotifyEvent index 3;
  319.     property OnClientRead: TSocketNotifyEvent index 0;
  320.     property OnClientWrite: TSocketNotifyEvent index 1;
  321.     property OnClientError: TSocketErrorEvent;
  322.   public
  323.     destructor Destroy; override;
  324.   end;
  325.  
  326.   TServerSocket = class(TCustomServerSocket)
  327.   public
  328.     constructor Create(AOwner: TComponent); override;
  329.     property Socket: TServerWinSocket;
  330.   published
  331.     property Active;
  332.     property Port;
  333.     property Service;
  334.     property ServerType;
  335.     property ThreadCacheSize default 10;
  336.     property OnListen;
  337.     property OnAccept;
  338.     property OnGetThread;
  339.     property OnGetSocket;
  340.     property OnThreadStart;
  341.     property OnThreadEnd;
  342.     property OnClientConnect;
  343.     property OnClientDisconnect;
  344.     property OnClientRead;
  345.     property OnClientWrite;
  346.     property OnClientError;
  347.   end;
  348.  
  349.   TSocketErrorProc = procedure (ErrorCode: Integer);
  350.  
  351. function SetErrorProc(ErrorProc: TSocketErrorProc): TSocketErrorProc;
  352.  
  353. implementation
  354.