12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271 |
- { lNet v0.5.1
- CopyRight (C) 2004-2006 Ales Katona
- This library is Free software; you can rediStribute it and/or modify it
- under the terms of the GNU Library General Public License as published by
- the Free Software Foundation; either version 2 of the License, or (at your
- option) any later version.
- This program is diStributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
- for more details.
- You should have received a Copy of the GNU Library General Public License
- along with This library; if not, Write to the Free Software Foundation,
- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
- This license has been modified. See File LICENSE.ADDON for more inFormation.
- Should you find these sources without a LICENSE File, please contact
- me at [email protected]
- }
- unit lNet;
- {$mode objfpc}{$H+}{$T-}
- {$interfaces corba}
- interface
- uses
- Classes, lEvents,
- {$i sys/osunits.inc}
- const
- { Address constants }
- LADDR_ANY = '0.0.0.0';
- LADDR_BR = '255.255.255.255';
- LADDR_LO = '127.0.0.1';
- { ICMP }
- LICMP_ECHOREPLY = 0;
- LICMP_UNREACH = 3;
- LICMP_ECHO = 8;
- LICMP_TIME_EXCEEDED = 11;
- { Protocols }
- LPROTO_IP = 0;
- LPROTO_ICMP = 1;
- LPROTO_IGMP = 2;
- LPROTO_TCP = 6;
- LPROTO_UDP = 17;
- LPROTO_IPV6 = 41;
- LPROTO_ICMPV6 = 58;
- LPROTO_RAW = 255;
- LPROTO_MAX = 256;
- type
- PLIPHeader = ^TLIPHeader;
- TLIPHeader = record
- VerLen : Byte;
- TOS : Byte;
- TotalLen : Word;
- Identifer : Word;
- FragOffsets : Word;
- TTL : Byte;
- Protocol : Byte;
- CheckSum : Word;
- SourceIp : DWord;
- DestIp : DWord;
- Options : DWord;
- end; // TLIPHeader
- TLSocket = class;
- TLComponent = class;
-
- { Callback Event procedure for errors }
- TLSocketErrorEvent = procedure(const msg: string; aSocket: TLSocket) of object;
- { Callback Event procedure for others }
- TLSocketEvent = procedure(aSocket: TLSocket) of object;
- { Callback Event procedure for progress reports}
- TLSocketProgressEvent = procedure (aSocket: TLSocket; const Bytes: Integer) of object;
- { Base socket class, Holds Address and socket info, perForms basic
- socket operations, uses select always to figure out if it can work (slow) }
- { TLSocket }
- TLSocket = class(TLHandle)
- protected
- FAddress: TInetSockAddr;
- FPeerAddress: TInetSockAddr;
- FConnected: Boolean;
- FConnecting: Boolean;
- FNextSock: TLSocket;
- FPrevSock: TLSocket;
- FIgnoreShutdown: Boolean;
- FCanSend: Boolean;
- FCanReceive: Boolean;
- FServerSocket: Boolean;
- FOnFree: TLSocketEvent;
- FBlocking: Boolean;
- FListenBacklog: Integer;
- FProtocol: Integer;
- FSocketType: Integer;
- FCreator: TLComponent;
- protected
- function DoSend(const TheData; const TheSize: Integer): Integer;
-
- function SetupSocket(const APort: Word; const Address: string): Boolean; virtual;
-
- function GetLocalPort: Word;
- function GetPeerPort: Word;
- function GetPeerAddress: string;
- function GetLocalAddress: string;
- function CanSend: Boolean; virtual;
- function CanReceive: Boolean; virtual;
-
- procedure SetBlocking(const aValue: Boolean);
- procedure SetOptions; virtual;
-
- function Bail(const msg: string; const ernum: Integer): Boolean;
-
- procedure LogError(const msg: string; const ernum: Integer); virtual;
- public
- constructor Create; override;
- destructor Destroy; override;
-
- function Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean;
- function Accept(const SerSock: Integer): Boolean;
-
- function Connect(const Address: string; const APort: Word): Boolean;
-
- function Send(const aData; const aSize: Integer): Integer; virtual;
- function SendMessage(const msg: string): Integer;
-
- function Get(var aData; const aSize: Integer): Integer; virtual;
- function GetMessage(out msg: string): Integer;
-
- procedure Disconnect; virtual;
- public
- property Connected: Boolean read FConnected;
- property Connecting: Boolean read FConnecting;
- property Blocking: Boolean read FBlocking write SetBlocking;
- property ListenBacklog: Integer read FListenBacklog write FListenBacklog;
- property Protocol: Integer read FProtocol write FProtocol;
- property SocketType: Integer read FSocketType write FSocketType;
- property PeerAddress: string read GetPeerAddress;
- property PeerPort: Word read GetPeerPort;
- property LocalAddress: string read GetLocalAddress;
- property LocalPort: Word read GetLocalPort;
- property NextSock: TLSocket read FNextSock write FNextSock;
- property PrevSock: TLSocket read FPrevSock write FPrevSock;
- property Creator: TLComponent read FCreator;
- end;
- TLSocketClass = class of TLSocket;
- { this is the socket used by TLConnection }
-
- TLActionEnum = (acConnect, acAccept, acSend, acReceive, acError);
- { Base interface common to ALL connections }
-
- ILComponent = interface
- procedure Disconnect;
- procedure CallAction;
-
- property SocketClass: TLSocketClass;
- property Host: string;
- property Port: Word;
- end;
-
- { Interface for protools with direct send/get capabilities }
- ILDirect = interface
- function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer;
- function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer;
- function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer;
- function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer;
- end;
-
- { Interface for all servers }
-
- ILServer = interface
- function Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean;
- end;
- { Interface for all clients }
-
- ILClient = interface
- function Connect(const Address: string; const APort: Word): Boolean; overload;
- function Connect: Boolean; overload;
- end;
-
- { TLComponent }
- TLComponent = class(TComponent, ILComponent)
- protected
- FHost: string;
- FPort: Word;
- FCreator: TLComponent;
- public
- constructor Create(aOwner: TComponent); override;
- procedure Disconnect; virtual; abstract;
- procedure CallAction; virtual; abstract;
- public
- SocketClass: TLSocketClass;
- property Host: string read FHost write FHost;
- property Port: Word read FPort write FPort;
- property Creator: TLComponent read FCreator write FCreator;
- end;
-
- { TLConnection
- Common ancestor for TLBaseTcp and TLUdp classes. Holds Event properties
- and common variables. }
- TLConnection = class(TLComponent, ILDirect, ILServer, ILClient)
- protected
- FTimeVal: TTimeVal;
- FOnReceive: TLSocketEvent;
- FOnAccept: TLSocketEvent;
- FOnConnect: TLSocketEvent;
- FOnDisconnect: TLSocketEvent;
- FOnCanSend: TLSocketEvent;
- FOnError: TLSocketErrorEvent;
- FRootSock: TLSocket;
- FIterator: TLSocket;
- FID: Integer; // internal number for server
- FEventer: TLEventer;
- FEventerClass: TLEventerClass;
- FTimeout: DWord;
- FListenBacklog: Integer;
- protected
- function InitSocket(aSocket: TLSocket): TLSocket; virtual;
-
- function GetConnected: Boolean; virtual; abstract;
- function GetCount: Integer; virtual;
- function GetItem(const i: Integer): TLSocket;
-
- function GetTimeout: DWord;
- procedure SetTimeout(const AValue: DWord);
-
- procedure SetEventer(Value: TLEventer);
-
- procedure ConnectAction(aSocket: TLHandle); virtual;
- procedure AcceptAction(aSocket: TLHandle); virtual;
- procedure ReceiveAction(aSocket: TLHandle); virtual;
- procedure SendAction(aSocket: TLHandle); virtual;
- procedure ErrorAction(aSocket: TLHandle; const msg: string); virtual;
-
- procedure ConnectEvent(aSocket: TLHandle); virtual;
- procedure DisconnectEvent(aSocket: TLHandle); virtual;
- procedure AcceptEvent(aSocket: TLHandle); virtual;
- procedure ReceiveEvent(aSocket: TLHandle); virtual;
- procedure CanSendEvent(aSocket: TLHandle); virtual;
- procedure ErrorEvent(const msg: string; aSocket: TLHandle); virtual;
- procedure EventerError(const msg: string; Sender: TLEventer);
-
- procedure RegisterWithEventer; virtual;
-
- procedure FreeSocks; virtual;
- public
- constructor Create(aOwner: TComponent); override;
- destructor Destroy; override;
-
- function Connect(const Address: string; const APort: Word): Boolean; virtual; overload;
- function Connect: Boolean; virtual; overload;
-
- function Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean; virtual; abstract; overload;
- function Listen: Boolean; virtual; overload;
-
- function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
- function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
-
- function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
- function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
-
- function IterNext: Boolean; virtual; abstract;
- procedure IterReset; virtual; abstract;
- public
- property OnError: TLSocketErrorEvent read FOnError write FOnError;
- property OnReceive: TLSocketEvent read FOnReceive write FOnReceive;
- property OnDisconnect: TLSocketEvent read FOnDisconnect write FOnDisconnect;
- property OnCanSend: TLSocketEvent read FOnCanSend write FOnCanSend;
- property Socks[index: Integer]: TLSocket read GetItem; default;
- property Count: Integer read GetCount;
- property Connected: Boolean read GetConnected;
- property ListenBacklog: Integer read FListenBacklog write FListenBacklog;
- property Iterator: TLSocket read FIterator;
- property Timeout: DWord read GetTimeout write SetTimeout;
- property Eventer: TLEventer read FEventer write SetEventer;
- property EventerClass: TLEventerClass read FEventerClass write FEventerClass;
- end;
-
- { UDP Client/Server class. Provided to enable usage of UDP sockets }
- { TLUdp }
- TLUdp = class(TLConnection)
- protected
- function InitSocket(aSocket: TLSocket): TLSocket; override;
-
- function GetConnected: Boolean; override;
-
- procedure ReceiveAction(aSocket: TLHandle); override;
- procedure SendAction(aSocket: TLHandle); override;
- procedure ErrorAction(aSocket: TLHandle; const msg: string); override;
-
- function Bail(const msg: string): Boolean;
-
- procedure SetAddress(const Address: string);
- public
- constructor Create(aOwner: TComponent); override;
-
- function Connect(const Address: string; const APort: Word): Boolean; override;
- function Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean; override;
-
- function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
- function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; override;
-
- function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; override;
- function SendMessage(const msg: string; const Address: string): Integer; overload;
-
- function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
- function Send(const aData; const aSize: Integer; const Address: string): Integer; overload;
-
- function IterNext: Boolean; override;
- procedure IterReset; override;
- procedure Disconnect; override;
- procedure CallAction; override;
- end;
-
- { TCP Client/Server class. Provided to enable usage of TCP sockets }
- { TLTcp }
- TLTcp = class(TLConnection)
- protected
- FCount: Integer;
- function InitSocket(aSocket: TLSocket): TLSocket; override;
- function GetConnected: Boolean; override;
- function GetConnecting: Boolean;
- procedure ConnectAction(aSocket: TLHandle); override;
- procedure AcceptAction(aSocket: TLHandle); override;
- procedure ReceiveAction(aSocket: TLHandle); override;
- procedure SendAction(aSocket: TLHandle); override;
- procedure ErrorAction(aSocket: TLHandle; const msg: string); override;
- function Bail(const msg: string; aSocket: TLSocket): Boolean;
- procedure SocketDisconnect(aSocket: TLSocket);
- public
- constructor Create(aOwner: TComponent); override;
- function Connect(const Address: string; const APort: Word): Boolean; override;
- function Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean; override;
- function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
- function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; override;
- function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
- function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; override;
- function IterNext: Boolean; override;
- procedure IterReset; override;
- procedure CallAction; override;
- procedure Disconnect; override;
- public
- property Connecting: Boolean read GetConnecting;
- property OnAccept: TLSocketEvent read FOnAccept write FOnAccept;
- property OnConnect: TLSocketEvent read FOnConnect write FOnConnect;
- end;
-
- implementation
- uses
- lCommon;
-
- //********************************TLSocket*************************************
- constructor TLSocket.Create;
- begin
- inherited Create;
- FHandle := INVALID_SOCKET;
- FBlocking := False;
- FListenBacklog := LDEFAULT_BACKLOG;
- FServerSocket := False;
- FPrevSock := nil;
- FNextSock := nil;
- FCanSend := True;
- FCanReceive := False;
- FConnected := False;
- FConnecting := False;
- FIgnoreShutdown := False;
- FSocketType := SOCK_STREAM;
- FProtocol := LPROTO_TCP;
- end;
- destructor TLSocket.Destroy;
- begin
- if Assigned(FOnFree) then
- FOnFree(Self);
- Disconnect;
- inherited Destroy;
- end;
- procedure TLSocket.Disconnect;
- var
- WasConnected: Boolean;
- begin
- WasConnected := FConnected;
- FDispose := True;
- FCanSend := True;
- FCanReceive := True;
- FIgnoreWrite := True;
- if FConnected or FConnecting then begin
- FConnected := False;
- FConnecting := False;
- if (FSocketType = SOCK_STREAM) and (not FIgnoreShutdown) and WasConnected then
- if fpShutDown(FHandle, 2) <> 0 then
- LogError('Shutdown error', LSocketError);
- if CloseSocket(FHandle) <> 0 then
- LogError('Closesocket error', LSocketError);
- FHandle := INVALID_SOCKET;
- end;
- end;
- procedure TLSocket.LogError(const msg: string; const ernum: Integer);
- begin
- if Assigned(FOnError) then
- if ernum > 0 then
- FOnError(Self, msg + '[' + IntToStr(ernum) + ']: ' + LStrError(ernum))
- else
- FOnError(Self, msg);
- end;
- function TLSocket.Bail(const msg: string; const ernum: Integer): Boolean;
- begin
- Result := False; // return the result for the caller
- Disconnect;
- LogError(msg, ernum);
- end;
- function TLSocket.GetPeerAddress: string;
- begin
- Result := '';
- if FSocketType = SOCK_STREAM then
- Result := NetAddrtoStr(FAddress.Addr)
- else
- Result := NetAddrtoStr(FPeerAddress.Addr);
- end;
- function TLSocket.GetLocalAddress: string;
- var
- a: TSockAddr;
- l: Integer;
- begin
- l := SizeOf(a);
- fpGetSockName(FHandle, @a, @l);
- Result := HostAddrToStr(LongWord(a.sin_addr));
- end;
- function TLSocket.CanSend: Boolean;
- begin
- Result := FCanSend and FConnected;
- end;
- function TLSocket.CanReceive: Boolean;
- begin
- Result := FCanReceive and FConnected;
- end;
- procedure TLSocket.SetBlocking(const aValue: Boolean);
- begin
- FBlocking := aValue;
- if FHandle >= 0 then // we already set our socket
- if not lCommon.SetBlocking(FHandle, aValue) then
- Bail('Error on SetBlocking', LSocketError);
- end;
- procedure TLSocket.SetOptions;
- begin
- SetBlocking(FBlocking);
- end;
- function TLSocket.GetMessage(out msg: string): Integer;
- begin
- Result := 0;
- SetLength(msg, BUFFER_SIZE);
- SetLength(msg, Get(PChar(msg)^, Length(msg)));
- Result := Length(msg);
- end;
- function TLSocket.Get(var aData; const aSize: Integer): Integer;
- var
- AddressLength: Integer = SizeOf(FAddress);
- begin
- Result := 0;
- if CanReceive then begin
- if FSocketType = SOCK_STREAM then
- Result := sockets.fpRecv(FHandle, @aData, aSize, LMSG)
- else
- Result := sockets.fpRecvfrom(FHandle, @aData, aSize, LMSG, @FPeerAddress, @AddressLength);
- if Result = 0 then
- Disconnect;
- if Result = SOCKET_ERROR then begin
- if IsBlockError(LSocketError) then begin
- FCanReceive := False;
- IgnoreRead := False;
- end else Bail('Receive Error', LSocketError);
- Result := 0;
- end;
- end;
- end;
- function TLSocket.DoSend(const TheData; const TheSize: Integer): Integer;
- var
- AddressLength: Integer;
- begin
- AddressLength := SizeOf(FPeerAddress);
- if FSocketType = SOCK_STREAM then
- Result := sockets.fpsend(FHandle, @TheData, TheSize, LMSG)
- else
- Result := sockets.fpsendto(FHandle, @TheData, TheSize, LMSG, @FPeerAddress, AddressLength);
- end;
- function TLSocket.SetupSocket(const APort: Word; const Address: string): Boolean;
- var
- Done: Boolean;
- Arg: Integer;
- begin
- Result := false;
- if not FConnected and not FConnecting then begin
- Done := true;
- FHandle := fpSocket(AF_INET, FSocketType, FProtocol);
- if FHandle = INVALID_SOCKET then
- Bail('Socket error', LSocketError);
- SetOptions;
- if FSocketType = SOCK_DGRAM then begin
- Arg := 1;
- if fpsetsockopt(FHandle, SOL_SOCKET, SO_BROADCAST, @Arg, Sizeof(Arg)) = SOCKET_ERROR then
- Bail('SetSockOpt error', LSocketError);
- end;
-
- FillAddressInfo(FAddress, AF_INET, Address, aPort);
- FillAddressInfo(FPeerAddress, AF_INET, LADDR_BR, aPort);
- Result := Done;
- end;
- end;
- function TLSocket.GetLocalPort: Word;
- begin
- Result := FAddress.sin_port;
- end;
- function TLSocket.GetPeerPort: Word;
- begin
- Result := ntohs(FPeerAddress.sin_port);
- end;
- function TLSocket.Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean;
- begin
- if not Connected then begin
- Result := false;
- SetupSocket(APort, AIntf);
- if fpBind(FHandle, psockaddr(@FAddress), SizeOf(FAddress)) = SOCKET_ERROR then
- Bail('Error on bind', LSocketError)
- else
- Result := true;
- if (FSocketType = SOCK_STREAM) and Result then
- if fpListen(FHandle, FListenBacklog) = SOCKET_ERROR then
- Result := Bail('Error on Listen', LSocketError)
- else
- Result := true;
- end;
- end;
- function TLSocket.Accept(const sersock: Integer): Boolean;
- var
- AddressLength: tsocklen = SizeOf(FAddress);
- begin
- Result := false;
- if not Connected then begin
- FHandle := fpAccept(sersock, psockaddr(@FAddress), @AddressLength);
- if FHandle <> INVALID_SOCKET then begin
- SetOptions;
- Result := true;
- FConnected := true;
- end else
- Bail('Error on accept', LSocketError);
- end;
- end;
- function TLSocket.Connect(const Address: string; const aPort: Word): Boolean;
- begin
- Result := False;
- if Connected or FConnecting then
- Disconnect;
- if SetupSocket(APort, Address) then begin
- fpConnect(FHandle, psockaddr(@FAddress), SizeOf(FAddress));
- FConnecting := True;
- Result := FConnecting;
- end;
- end;
- function TLSocket.SendMessage(const msg: string): Integer;
- begin
- Result := Send(PChar(msg)^, Length(msg));
- end;
- function TLSocket.Send(const aData; const aSize: Integer): Integer;
- begin
- Result := 0;
- if not FServerSocket then begin
- if aSize <= 0 then
- Bail('Send error: wrong size (Size <= 0)', -1);
- if CanSend then begin
- Result := DoSend(aData, aSize);
- if Result = SOCKET_ERROR then begin
- if IsBlockError(LSocketError) then begin
- FCanSend := False;
- IgnoreWrite := False;
- end else
- Bail('Send error', LSocketError);
- Result := 0;
- end;
- end;
- end;
- end;
- //*******************************TLConnection*********************************
- constructor TLConnection.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- FHost := '';
- FPort := 0;
- FListenBacklog := LDEFAULT_BACKLOG;
- FTimeout := 0;
- SocketClass := TLSocket;
- FOnReceive := nil;
- FOnError := nil;
- FOnDisconnect := nil;
- FOnCanSend := nil;
- FOnConnect := nil;
- FOnAccept := nil;
- FTimeVal.tv_sec := 0;
- FTimeVal.tv_usec := 0;
- FIterator := nil;
- FEventer := nil;
- FEventerClass := BestEventerClass;
- end;
- destructor TLConnection.Destroy;
- begin
- FreeSocks;
- if Assigned(FEventer) then
- FEventer.DeleteRef;
- inherited Destroy;
- end;
- function TLConnection.Connect(const Address: string; const APort: Word
- ): Boolean;
- begin
- FHost := Address;
- FPort := aPort;
- Result := False;
- end;
- function TLConnection.Connect: Boolean;
- begin
- Result := Connect(FHost, FPort);
- end;
- function TLConnection.Listen: Boolean;
- begin
- Result := Listen(FPort, FHost);
- end;
- function TLConnection.InitSocket(aSocket: TLSocket): TLSocket;
- begin
- aSocket.OnRead := @ReceiveAction;
- aSocket.OnWrite := @SendAction;
- aSocket.OnError := @ErrorAction;
- aSocket.ListenBacklog := FListenBacklog;
- aSocket.FCreator := FCreator;
- Result := aSocket;
- end;
- function TLConnection.GetCount: Integer;
- begin
- Result := 1;
- end;
- function TLConnection.GetItem(const i: Integer): TLSocket;
- var
- Tmp: TLSocket;
- Jumps: Integer;
- begin
- Result := nil;
- Tmp := FRootSock;
- Jumps := 0;
- while Assigned(Tmp.NextSock) and (Jumps < i) do begin
- Tmp := Tmp.NextSock;
- Inc(Jumps);
- end;
- if Jumps = i then
- Result := Tmp;
- end;
- function TLConnection.GetTimeout: DWord;
- begin
- if Assigned(FEventer) then
- Result := FEventer.Timeout
- else
- Result := FTimeout;
- end;
- procedure TLConnection.ConnectAction(aSocket: TLHandle);
- begin
- end;
- procedure TLConnection.AcceptAction(aSocket: TLHandle);
- begin
- end;
- procedure TLConnection.ReceiveAction(aSocket: TLHandle);
- begin
- end;
- procedure TLConnection.SendAction(aSocket: TLHandle);
- begin
- end;
- procedure TLConnection.ErrorAction(aSocket: TLHandle; const msg: string);
- begin
- end;
- procedure TLConnection.ConnectEvent(aSocket: TLHandle);
- begin
- if Assigned(FOnConnect) then
- FOnConnect(TLSocket(aSocket));
- end;
- procedure TLConnection.DisconnectEvent(aSocket: TLHandle);
- begin
- if Assigned(FOnDisconnect) then
- FOnDisconnect(TLSocket(aSocket));
- end;
- procedure TLConnection.AcceptEvent(aSocket: TLHandle);
- begin
- if Assigned(FOnAccept) then
- FOnAccept(TLSocket(aSocket));
- end;
- procedure TLConnection.ReceiveEvent(aSocket: TLHandle);
- begin
- if Assigned(FOnReceive) then
- FOnReceive(TLSocket(aSocket));
- end;
- procedure TLConnection.CanSendEvent(aSocket: TLHandle);
- begin
- if Assigned(FOnCanSend) then
- FOnCanSend(TLSocket(aSocket));
- end;
- procedure TLConnection.ErrorEvent(const msg: string; aSocket: TLHandle);
- begin
- if Assigned(FOnError) then
- FOnError(msg, TLSocket(aSocket));
- end;
- procedure TLConnection.SetTimeout(const AValue: DWord);
- begin
- if Assigned(FEventer) then
- FEventer.Timeout := aValue;
- FTimeout := aValue;
- end;
- procedure TLConnection.SetEventer(Value: TLEventer);
- begin
- if Assigned(FEventer) then
- FEventer.DeleteRef;
- FEventer := Value;
- FEventer.AddRef;
- end;
- procedure TLConnection.EventerError(const msg: string; Sender: TLEventer);
- begin
- ErrorEvent(msg, nil);
- end;
- procedure TLConnection.RegisterWithEventer;
- begin
- if not Assigned(FEventer) then begin
- FEventer := FEventerClass.Create;
- FEventer.OnError := @EventerError;
- end;
- if Assigned(FRootSock) then
- FEventer.AddHandle(FRootSock);
- if (FEventer.Timeout = 0) and (FTimeout > 0) then
- FEventer.Timeout := FTimeout
- else
- FTimeout := FEventer.Timeout;
- end;
- procedure TLConnection.FreeSocks;
- var
- Tmp, Tmp2: TLSocket;
- begin
- Tmp := FRootSock;
- while Assigned(Tmp) do begin
- Tmp2 := Tmp;
- Tmp := Tmp.NextSock;
- Tmp2.Free;
- end;
- end;
- //*******************************TLUdp*********************************
- constructor TLUdp.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- FTimeVal.tv_usec := 0;
- FTimeVal.tv_sec := 0;
- end;
- procedure TLUdp.Disconnect;
- begin
- if Assigned(FRootSock) then begin
- FRootSock.Disconnect;
- FreeAndNil(FRootSock);
- end;
- end;
- function TLUdp.Connect(const Address: string; const APort: Word): Boolean;
- begin
- Result := inherited Connect(Address, aPort);
- if Assigned(FRootSock) and FRootSock.Connected then
- Disconnect;
- FRootSock := InitSocket(SocketClass.Create);
- FIterator := FRootSock;
- Result := FRootSock.SetupSocket(APort, LADDR_ANY);
-
- FillAddressInfo(FRootSock.FPeerAddress, AF_INET, Address, aPort);
- FRootSock.FConnected := true;
- if Result then
- RegisterWithEventer;
- end;
- function TLUdp.Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean;
- begin
- Result := False;
- if Assigned(FRootSock) and FRootSock.Connected then
- Disconnect;
- FRootSock := InitSocket(SocketClass.Create);
- FIterator := FRootSock;
-
- if FRootSock.Listen(APort, AIntf) then begin
- FillAddressInfo(FRootSock.FPeerAddress, AF_INET, LADDR_BR, aPort);
-
- FRootSock.FConnected := True;
- RegisterWithEventer;
- end;
- Result := FRootSock.Connected;
- end;
- function TLUdp.Bail(const msg: string): Boolean;
- begin
- Result := False;
- Disconnect;
- ErrorEvent(msg, FRootSock);
- end;
- procedure TLUdp.SetAddress(const Address: string);
- var
- n: Integer;
- s: string;
- p: Word;
- begin
- n := Pos(':', Address);
- if n > 0 then begin
- s := Copy(Address, 1, n-1);
- p := Word(StrToInt(Copy(Address, n+1, Length(Address))));
- FillAddressInfo(FRootSock.FPeerAddress, AF_INET, s, p);
- end else
- FillAddressInfo(FRootSock.FPeerAddress, AF_INET, Address,
- FRootSock.PeerPort);
- end;
- function TLUdp.InitSocket(aSocket: TLSocket): TLSocket;
- begin
- Result := FRootSock;
- if not Assigned(FRootSock) then begin
- Result := inherited InitSocket(aSocket);
- aSocket.SocketType := SOCK_DGRAM;
- aSocket.Protocol := LPROTO_UDP;
- end;
- end;
- procedure TLUdp.ReceiveAction(aSocket: TLHandle);
- begin
- with TLSocket(aSocket) do begin
- FCanReceive := True;
- ReceiveEvent(aSocket);
- end;
- end;
- procedure TLUdp.SendAction(aSocket: TLHandle);
- begin
- with TLSocket(aSocket) do begin
- FCanSend := True;
- IgnoreWrite := True;
- CanSendEvent(aSocket);
- end;
- end;
- procedure TLUdp.ErrorAction(aSocket: TLHandle; const msg: string);
- begin
- Bail(msg);
- end;
- function TLUdp.IterNext: Boolean;
- begin
- Result := False;
- end;
- procedure TLUdp.IterReset;
- begin
- end;
- procedure TLUdp.CallAction;
- begin
- if Assigned(FEventer) then
- FEventer.CallAction;
- end;
- function TLUdp.GetConnected: Boolean;
- begin
- Result := False;
- if Assigned(FRootSock) then
- Result := FRootSock.Connected;
- end;
- function TLUdp.Get(var aData; const aSize: Integer; aSocket: TLSocket): Integer;
- begin
- Result := 0;
- if Assigned(FRootSock) then
- Result := FRootSock.Get(aData, aSize);
- end;
- function TLUdp.GetMessage(out msg: string; aSocket: TLSocket): Integer;
- begin
- Result := 0;
- if Assigned(FRootSock) then
- Result := FRootSock.GetMessage(msg);
- end;
- function TLUdp.SendMessage(const msg: string; aSocket: TLSocket = nil): Integer;
- begin
- Result := 0;
- if Assigned(FRootSock) then
- Result := FRootSock.SendMessage(msg)
- end;
- function TLUdp.SendMessage(const msg: string; const Address: string): Integer;
- begin
- Result := 0;
- if Assigned(FRootSock) then begin
- SetAddress(Address);
- Result := FRootSock.SendMessage(msg)
- end;
- end;
- function TLUdp.Send(const aData; const aSize: Integer; aSocket: TLSocket): Integer;
- begin
- Result := 0;
- if Assigned(FRootSock) then
- Result := FRootSock.Send(aData, aSize)
- end;
- function TLUdp.Send(const aData; const aSize: Integer; const Address: string
- ): Integer;
- begin
- Result := 0;
- if Assigned(FRootSock) then begin
- SetAddress(Address);
- Result := FRootSock.Send(aData, aSize);
- end;
- end;
- //******************************TLTcp**********************************
- constructor TLTcp.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- FIterator := nil;
- FCount := 0;
- FRootSock := nil;
- end;
- function TLTcp.Connect(const Address: string; const APort: Word): Boolean;
- begin
- Result := inherited Connect(Address, aPort);
-
- if Assigned(FRootSock) then
- Disconnect;
-
- FRootSock := InitSocket(SocketClass.Create);
- Result := FRootSock.Connect(Address, aPort);
-
- if Result then begin
- Inc(FCount);
- FIterator := FRootSock;
- RegisterWithEventer;
- end else begin
- FreeAndNil(FRootSock);
- FIterator := nil;
- end;
- end;
- function TLTcp.Listen(const APort: Word; const AIntf: string = LADDR_ANY): Boolean;
- begin
- Result := false;
-
- if Assigned(FRootSock) then
- Disconnect;
-
- FRootSock := InitSocket(SocketClass.Create);
- FRootSock.FIgnoreShutdown := True;
- if FRootSock.Listen(APort, AIntf) then begin
- FRootSock.FConnected := True;
- FRootSock.FServerSocket := True;
- RegisterWithEventer;
- Result := true;
- end;
- end;
- function TLTcp.Bail(const msg: string; aSocket: TLSocket): Boolean;
- begin
- Result := False;
-
- ErrorEvent(msg, aSocket);
- if Assigned(aSocket) then
- aSocket.Disconnect
- else
- Disconnect;
- end;
- procedure TLTcp.SocketDisconnect(aSocket: TLSocket);
- begin
- if aSocket = FIterator then begin
- if Assigned(FIterator.NextSock) then
- FIterator := FIterator.NextSock
- else if Assigned(FIterator.PrevSock) then
- FIterator := FIterator.PrevSock
- else FIterator := nil; // NOT iterreset, not reorganized yet
- if Assigned(FIterator) and FIterator.FServerSocket then
- FIterator := nil;
- end;
- if aSocket = FRootSock then
- FRootSock := aSocket.NextSock;
- if Assigned(aSocket.PrevSock) then
- aSocket.PrevSock.NextSock := aSocket.NextSock;
- if Assigned(aSocket.NextSock) then
- aSocket.NextSock.PrevSock := aSocket.PrevSock;
- Dec(FCount);
- end;
- function TLTcp.InitSocket(aSocket: TLSocket): TLSocket;
- begin
- Result := inherited InitSocket(aSocket);
- aSocket.SocketType := SOCK_STREAM;
- aSocket.Protocol := LPROTO_TCP;
- aSocket.FOnFree := @SocketDisconnect;
- end;
- function TLTcp.IterNext: Boolean;
- begin
- Result := False;
- if Assigned(FIterator.NextSock) then begin
- FIterator := FIterator.NextSock;
- Result := True;
- end else IterReset;
- end;
- procedure TLTcp.IterReset;
- begin
- if Assigned(FRootSock) and FRootSock.FServerSocket then
- FIterator := FRootSock.NextSock
- else
- FIterator := FRootSock;
- end;
- procedure TLTcp.Disconnect;
- begin
- FreeSocks;
- FRootSock := nil;
- FCount := 0;
- FIterator := nil;
- end;
- procedure TLTcp.CallAction;
- begin
- if Assigned(FEventer) then
- FEventer.CallAction;
- end;
- procedure TLTcp.ConnectAction(aSocket: TLHandle);
- var
- a: TInetSockAddr;
- l: Longint;
- begin
- with TLSocket(aSocket) do begin
- l := SizeOf(a);
- if Sockets.fpGetPeerName(FHandle, @a, @l) <> 0 then
- Self.Bail('Error on connect: connection refused', TLSocket(aSocket))
- else begin
- FConnected := True;
- FConnecting := False;
- ConnectEvent(aSocket);
- end;
- end;
- end;
- procedure TLTcp.AcceptAction(aSocket: TLHandle);
- var
- Tmp: TLSocket;
- begin
- Tmp := InitSocket(SocketClass.Create);
- if Tmp.Accept(FRootSock.FHandle) then begin
- if Assigned(FRootSock.FNextSock) then begin
- Tmp.FNextSock := FRootSock.FNextSock;
- FRootSock.FNextSock.FPrevSock := Tmp;
- end;
- FRootSock.FNextSock := Tmp;
- Tmp.FPrevSock := FRootSock;
- if not Assigned(FIterator) then
- FIterator := Tmp;
- Inc(FCount);
- FEventer.AddHandle(Tmp);
- AcceptEvent(Tmp);
- end else Tmp.Free;
- end;
- procedure TLTcp.ReceiveAction(aSocket: TLHandle);
- begin
- if (TLSocket(aSocket) = FRootSock) and TLSocket(aSocket).FServerSocket then
- AcceptAction(aSocket)
- else with TLSocket(aSocket) do begin
- if Connected then begin
- FCanReceive := True;
- ReceiveEvent(aSocket);
- if not Connected then begin
- DisconnectEvent(aSocket);
- aSocket.Free;
- end;
- end;
- end;
- end;
- procedure TLTcp.SendAction(aSocket: TLHandle);
- begin
- with TLSocket(aSocket) do begin
- if Connecting then
- ConnectAction(aSocket);
- FCanSend := True;
- IgnoreWrite := True;
- CanSendEvent(aSocket);
- end;
- end;
- procedure TLTcp.ErrorAction(aSocket: TLHandle; const msg: string);
- begin
- with TLSocket(aSocket) do begin
- if Connecting then
- Self.Bail('Error on connect: connection refused' , TLSocket(aSocket))
- else
- Self.Bail(msg, TLSocket(aSocket));
- end;
- end;
- function TLTcp.GetConnected: Boolean;
- var
- Tmp: TLSocket;
- begin
- Result := False;
- Tmp := FRootSock;
- while Assigned(Tmp) do begin
- if Tmp.Connected then begin
- Result := True;
- Exit;
- end else Tmp := Tmp.NextSock;
- end;
- end;
- function TLTcp.GetConnecting: Boolean;
- begin
- Result := False;
- if Assigned(FRootSock) then
- Result := FRootSock.Connecting;
- end;
- function TLTcp.Get(var aData; const aSize: Integer; aSocket: TLSocket): Integer;
- begin
- Result := 0;
- if not Assigned(aSocket) then
- aSocket := FIterator;
- if Assigned(aSocket) then
- Result := aSocket.Get(aData, aSize);
- end;
- function TLTcp.GetMessage(out msg: string; aSocket: TLSocket): Integer;
- begin
- Result := 0;
- if not Assigned(aSocket) then
- aSocket := FIterator;
- if Assigned(aSocket) then
- Result := aSocket.GetMessage(msg);
- end;
- function TLTcp.Send(const aData; const aSize: Integer; aSocket: TLSocket): Integer;
- begin
- Result := 0;
- if not Assigned(aSocket) then
- aSocket := FIterator;
- if Assigned(aSocket) and (aSize > 0) then
- Result := aSocket.Send(aData, aSize);
- end;
- function TLTcp.SendMessage(const msg: string; aSocket: TLSocket): Integer;
- begin
- Result := Send(PChar(msg)^, Length(msg), aSocket);
- end;
- { TLComponent }
- constructor TLComponent.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- FCreator := Self;
- end;
- end.
|