|
@@ -43,10 +43,10 @@ type
|
|
FSocketOptions : TSocketOptions;
|
|
FSocketOptions : TSocketOptions;
|
|
Procedure GetSockOptions;
|
|
Procedure GetSockOptions;
|
|
Procedure SetSocketOptions(Value : TSocketOptions);
|
|
Procedure SetSocketOptions(Value : TSocketOptions);
|
|
- Public
|
|
|
|
|
|
+ Public
|
|
Constructor Create (AHandle : Longint);virtual;
|
|
Constructor Create (AHandle : Longint);virtual;
|
|
function Seek(Offset: Longint; Origin: Word): Longint; override;
|
|
function Seek(Offset: Longint; Origin: Word): Longint; override;
|
|
- Property SocketOptions : TSocketOptions Read FSocketOptions
|
|
|
|
|
|
+ Property SocketOptions : TSocketOptions Read FSocketOptions
|
|
Write SetSocketOptions;
|
|
Write SetSocketOptions;
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -54,7 +54,7 @@ type
|
|
TConnectQuery = Procedure (Sender : TObject; ASocket : Longint; Var Allow : Boolean) of Object;
|
|
TConnectQuery = Procedure (Sender : TObject; ASocket : Longint; Var Allow : Boolean) of Object;
|
|
|
|
|
|
TSocketServer = Class(TObject)
|
|
TSocketServer = Class(TObject)
|
|
- Private
|
|
|
|
|
|
+ Private
|
|
FOnIdle : TNotifyEvent;
|
|
FOnIdle : TNotifyEvent;
|
|
FNonBlocking : Boolean;
|
|
FNonBlocking : Boolean;
|
|
FSocket : longint;
|
|
FSocket : longint;
|
|
@@ -65,7 +65,7 @@ type
|
|
FOnConnect : TConnectEvent;
|
|
FOnConnect : TConnectEvent;
|
|
FOnConnectQuery : TConnectQuery;
|
|
FOnConnectQuery : TConnectQuery;
|
|
Procedure DoOnIdle;
|
|
Procedure DoOnIdle;
|
|
- Protected
|
|
|
|
|
|
+ Protected
|
|
FSockType : Longint;
|
|
FSockType : Longint;
|
|
FBound : Boolean;
|
|
FBound : Boolean;
|
|
Procedure DoConnect(ASocket : TSocketStream); Virtual;
|
|
Procedure DoConnect(ASocket : TSocketStream); Virtual;
|
|
@@ -74,7 +74,7 @@ type
|
|
Function Accept: Longint;Virtual;Abstract;
|
|
Function Accept: Longint;Virtual;Abstract;
|
|
Function SockToStream (ASocket : Longint) : TSocketStream;Virtual;Abstract;
|
|
Function SockToStream (ASocket : Longint) : TSocketStream;Virtual;Abstract;
|
|
Procedure Close; Virtual;
|
|
Procedure Close; Virtual;
|
|
- Public
|
|
|
|
|
|
+ Public
|
|
Constructor Create(ASocket : Longint);
|
|
Constructor Create(ASocket : Longint);
|
|
Destructor Destroy; Override;
|
|
Destructor Destroy; Override;
|
|
Procedure Listen;
|
|
Procedure Listen;
|
|
@@ -91,7 +91,7 @@ type
|
|
Property Socket : Longint Read FSocket;
|
|
Property Socket : Longint Read FSocket;
|
|
Property SockType : Longint Read FSockType;
|
|
Property SockType : Longint Read FSockType;
|
|
end;
|
|
end;
|
|
-
|
|
|
|
|
|
+
|
|
TInetServer = Class(TSocketServer)
|
|
TInetServer = Class(TSocketServer)
|
|
Protected
|
|
Protected
|
|
FAddr : TINetSockAddr;
|
|
FAddr : TINetSockAddr;
|
|
@@ -103,9 +103,9 @@ type
|
|
Procedure Bind; Override;
|
|
Procedure Bind; Override;
|
|
Property Port : Word Read FPort;
|
|
Property Port : Word Read FPort;
|
|
end;
|
|
end;
|
|
-
|
|
|
|
|
|
+
|
|
TUnixServer = Class(TSocketServer)
|
|
TUnixServer = Class(TSocketServer)
|
|
- Private
|
|
|
|
|
|
+ Private
|
|
FUnixAddr : TUnixSockAddr;
|
|
FUnixAddr : TUnixSockAddr;
|
|
FFileName : String;
|
|
FFileName : String;
|
|
Protected
|
|
Protected
|
|
@@ -116,26 +116,26 @@ type
|
|
Constructor Create(AFileName : String);
|
|
Constructor Create(AFileName : String);
|
|
Procedure Bind; Override;
|
|
Procedure Bind; Override;
|
|
Property FileName : String Read FFileName;
|
|
Property FileName : String Read FFileName;
|
|
- end;
|
|
|
|
|
|
+ end;
|
|
|
|
|
|
TInetSocket = Class(TSocketStream)
|
|
TInetSocket = Class(TSocketStream)
|
|
Private
|
|
Private
|
|
FHost : String;
|
|
FHost : String;
|
|
FPort : Word;
|
|
FPort : Word;
|
|
Protected
|
|
Protected
|
|
- Procedure DoConnect(ASocket : longint); Virtual;
|
|
|
|
|
|
+ Procedure DoConnect(ASocket : longint); Virtual;
|
|
Public
|
|
Public
|
|
Constructor Create(ASocket : longint); Override;
|
|
Constructor Create(ASocket : longint); Override;
|
|
Constructor Create(const AHost: String; APort: Word);
|
|
Constructor Create(const AHost: String; APort: Word);
|
|
Property Host : String Read FHost;
|
|
Property Host : String Read FHost;
|
|
Property Port : Word Read FPort;
|
|
Property Port : Word Read FPort;
|
|
end;
|
|
end;
|
|
-
|
|
|
|
|
|
+
|
|
TUnixSocket = Class(TSocketStream)
|
|
TUnixSocket = Class(TSocketStream)
|
|
Private
|
|
Private
|
|
FFileName : String;
|
|
FFileName : String;
|
|
Protected
|
|
Protected
|
|
- Procedure DoConnect(ASocket : longint); Virtual;
|
|
|
|
|
|
+ Procedure DoConnect(ASocket : longint); Virtual;
|
|
Public
|
|
Public
|
|
Constructor Create(ASocket : Longint);
|
|
Constructor Create(ASocket : Longint);
|
|
Constructor Create(AFileName : String);
|
|
Constructor Create(AFileName : String);
|
|
@@ -144,13 +144,29 @@ type
|
|
|
|
|
|
Implementation
|
|
Implementation
|
|
|
|
|
|
-uses inet,Unix;
|
|
|
|
|
|
+uses
|
|
|
|
+{$ifdef linux}
|
|
|
|
+ {$ifdef ver1_0}
|
|
|
|
+ Linux,
|
|
|
|
+ {$else}
|
|
|
|
+ Unix,
|
|
|
|
+ {$endif}
|
|
|
|
+{$endif}
|
|
|
|
+{$ifdef freebsd}
|
|
|
|
+ {$ifdef ver1_0}
|
|
|
|
+ Linux,
|
|
|
|
+ {$else}
|
|
|
|
+ Unix,
|
|
|
|
+ {$endif}
|
|
|
|
+{$endif}
|
|
|
|
+ inet
|
|
|
|
+ ;
|
|
|
|
|
|
Const
|
|
Const
|
|
SocketWouldBlock = -2;
|
|
SocketWouldBlock = -2;
|
|
|
|
|
|
{ ---------------------------------------------------------------------
|
|
{ ---------------------------------------------------------------------
|
|
- ESocketError
|
|
|
|
|
|
+ ESocketError
|
|
---------------------------------------------------------------------}
|
|
---------------------------------------------------------------------}
|
|
|
|
|
|
resourcestring
|
|
resourcestring
|
|
@@ -161,7 +177,7 @@ resourcestring
|
|
strSocketConnectFailed = 'Connect to %s failed.';
|
|
strSocketConnectFailed = 'Connect to %s failed.';
|
|
strSocketAcceptFailed = 'Could not accept a client connection: %s';
|
|
strSocketAcceptFailed = 'Could not accept a client connection: %s';
|
|
strSocketAcceptWouldBlock = 'Accept would block on socket: %d';
|
|
strSocketAcceptWouldBlock = 'Accept would block on socket: %d';
|
|
-
|
|
|
|
|
|
+
|
|
constructor ESocketError.Create(ACode: TSocketErrorType; const MsgArgs: array of const);
|
|
constructor ESocketError.Create(ACode: TSocketErrorType; const MsgArgs: array of const);
|
|
var
|
|
var
|
|
s: String;
|
|
s: String;
|
|
@@ -203,13 +219,13 @@ end;
|
|
Function TSocketStream.Seek(Offset: Longint; Origin: Word): Longint;
|
|
Function TSocketStream.Seek(Offset: Longint; Origin: Word): Longint;
|
|
|
|
|
|
begin
|
|
begin
|
|
- Result:=0;
|
|
|
|
|
|
+ Result:=0;
|
|
end;
|
|
end;
|
|
|
|
|
|
{ ---------------------------------------------------------------------
|
|
{ ---------------------------------------------------------------------
|
|
TSocketServer
|
|
TSocketServer
|
|
---------------------------------------------------------------------}
|
|
---------------------------------------------------------------------}
|
|
-
|
|
|
|
|
|
+
|
|
Constructor TSocketServer.Create(ASocket : Longint);
|
|
Constructor TSocketServer.Create(ASocket : Longint);
|
|
|
|
|
|
begin
|
|
begin
|
|
@@ -228,7 +244,7 @@ Procedure TSocketServer.Close;
|
|
begin
|
|
begin
|
|
If FSocket<>-1 Then
|
|
If FSocket<>-1 Then
|
|
FileClose(FSocket);
|
|
FileClose(FSocket);
|
|
- FSocket:=-1;
|
|
|
|
|
|
+ FSocket:=-1;
|
|
end;
|
|
end;
|
|
|
|
|
|
Procedure TSocketServer.Listen;
|
|
Procedure TSocketServer.Listen;
|
|
@@ -246,12 +262,12 @@ Var
|
|
NoConnections,
|
|
NoConnections,
|
|
NewSocket : longint;
|
|
NewSocket : longint;
|
|
Stream : TSocketStream;
|
|
Stream : TSocketStream;
|
|
-
|
|
|
|
|
|
+
|
|
begin
|
|
begin
|
|
Listen;
|
|
Listen;
|
|
Repeat
|
|
Repeat
|
|
Repeat
|
|
Repeat
|
|
- Try
|
|
|
|
|
|
+ Try
|
|
NewSocket:=Accept;
|
|
NewSocket:=Accept;
|
|
If NewSocket>=0 then
|
|
If NewSocket>=0 then
|
|
begin
|
|
begin
|
|
@@ -270,9 +286,9 @@ begin
|
|
NewSocket:=-1;
|
|
NewSocket:=-1;
|
|
end;
|
|
end;
|
|
else
|
|
else
|
|
- Raise;
|
|
|
|
- end;
|
|
|
|
- Until (NewSocket>=0) or (Not NonBlocking);
|
|
|
|
|
|
+ Raise;
|
|
|
|
+ end;
|
|
|
|
+ Until (NewSocket>=0) or (Not NonBlocking);
|
|
Until Not (FAccepting) or ((FMaxConnections<>-1) and (NoConnections>=FMaxConnections));
|
|
Until Not (FAccepting) or ((FMaxConnections<>-1) and (NoConnections>=FMaxConnections));
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -285,7 +301,7 @@ end;
|
|
Procedure TSocketServer.DoOnIdle;
|
|
Procedure TSocketServer.DoOnIdle;
|
|
|
|
|
|
begin
|
|
begin
|
|
- If Assigned(FOnIdle) then
|
|
|
|
|
|
+ If Assigned(FOnIdle) then
|
|
FOnIdle(Self);
|
|
FOnIdle(Self);
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -324,20 +340,20 @@ begin
|
|
S:=Sockets.Socket(AF_INET,SOCK_STREAM,0);
|
|
S:=Sockets.Socket(AF_INET,SOCK_STREAM,0);
|
|
If S=-1 Then
|
|
If S=-1 Then
|
|
Raise ESocketError.Create(seCreationFailed,[Format('%d',[APort])]);
|
|
Raise ESocketError.Create(seCreationFailed,[Format('%d',[APort])]);
|
|
- Inherited Create(S);
|
|
|
|
|
|
+ Inherited Create(S);
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
|
|
Procedure TInetServer.Bind;
|
|
Procedure TInetServer.Bind;
|
|
|
|
|
|
-
|
|
|
|
|
|
+
|
|
begin
|
|
begin
|
|
Faddr.family := AF_INET;
|
|
Faddr.family := AF_INET;
|
|
Faddr.port := ShortHostToNet(FPort);
|
|
Faddr.port := ShortHostToNet(FPort);
|
|
Faddr.addr := 0;
|
|
Faddr.addr := 0;
|
|
if not Sockets.Bind(FSocket, FAddr, Sizeof(FAddr)) then
|
|
if not Sockets.Bind(FSocket, FAddr, Sizeof(FAddr)) then
|
|
raise ESocketError.Create(seBindFailed, [IntToStr(FPort)]);
|
|
raise ESocketError.Create(seBindFailed, [IntToStr(FPort)]);
|
|
- FBound:=True;
|
|
|
|
|
|
+ FBound:=True;
|
|
end;
|
|
end;
|
|
|
|
|
|
Function TInetServer.SockToStream (ASocket : Longint) : TSocketStream;
|
|
Function TInetServer.SockToStream (ASocket : Longint) : TSocketStream;
|
|
@@ -356,7 +372,7 @@ begin
|
|
L:=SizeOf(FAddr);
|
|
L:=SizeOf(FAddr);
|
|
Result:=Sockets.Accept(Socket,Faddr,L);
|
|
Result:=Sockets.Accept(Socket,Faddr,L);
|
|
If Result<0 then
|
|
If Result<0 then
|
|
- If SocketError=Sys_EWOULDBLOCK then
|
|
|
|
|
|
+ If SocketError=Sys_EWOULDBLOCK then
|
|
Raise ESocketError.Create(seAcceptWouldBlock,[socket])
|
|
Raise ESocketError.Create(seAcceptWouldBlock,[socket])
|
|
else
|
|
else
|
|
Raise ESocketError.Create(seAcceptFailed,[socket]);
|
|
Raise ESocketError.Create(seAcceptFailed,[socket]);
|
|
@@ -365,36 +381,36 @@ end;
|
|
{ ---------------------------------------------------------------------
|
|
{ ---------------------------------------------------------------------
|
|
TUnixServer
|
|
TUnixServer
|
|
---------------------------------------------------------------------}
|
|
---------------------------------------------------------------------}
|
|
-
|
|
|
|
|
|
+
|
|
Constructor TUnixServer.Create(AFileName : String);
|
|
Constructor TUnixServer.Create(AFileName : String);
|
|
|
|
|
|
Var S : Longint;
|
|
Var S : Longint;
|
|
-
|
|
|
|
|
|
+
|
|
begin
|
|
begin
|
|
FFileName:=AFileName;
|
|
FFileName:=AFileName;
|
|
S:=Sockets.Socket(AF_UNIX,SOCK_STREAM,0);
|
|
S:=Sockets.Socket(AF_UNIX,SOCK_STREAM,0);
|
|
- If S=-1 then
|
|
|
|
|
|
+ If S=-1 then
|
|
Raise ESocketError.Create(seCreationFailed,[AFileName])
|
|
Raise ESocketError.Create(seCreationFailed,[AFileName])
|
|
else
|
|
else
|
|
- Inherited Create(S);
|
|
|
|
|
|
+ Inherited Create(S);
|
|
end;
|
|
end;
|
|
-
|
|
|
|
-Procedure TUnixServer.Close;
|
|
|
|
|
|
+
|
|
|
|
+Procedure TUnixServer.Close;
|
|
begin
|
|
begin
|
|
Inherited Close;
|
|
Inherited Close;
|
|
DeleteFile(FFileName);
|
|
DeleteFile(FFileName);
|
|
FFileName:='';
|
|
FFileName:='';
|
|
end;
|
|
end;
|
|
-
|
|
|
|
|
|
+
|
|
Procedure TUnixServer.Bind;
|
|
Procedure TUnixServer.Bind;
|
|
-
|
|
|
|
|
|
+
|
|
var
|
|
var
|
|
AddrLen : longint;
|
|
AddrLen : longint;
|
|
begin
|
|
begin
|
|
Str2UnixSockAddr(FFilename,FUnixAddr,AddrLen);
|
|
Str2UnixSockAddr(FFilename,FUnixAddr,AddrLen);
|
|
If Not Sockets.Bind(Socket,FUnixAddr,AddrLen) then
|
|
If Not Sockets.Bind(Socket,FUnixAddr,AddrLen) then
|
|
Raise ESocketError.Create(seBindFailed,[FFileName]);
|
|
Raise ESocketError.Create(seBindFailed,[FFileName]);
|
|
- FBound:=True;
|
|
|
|
|
|
+ FBound:=True;
|
|
end;
|
|
end;
|
|
|
|
|
|
Function TUnixServer.Accept : Longint;
|
|
Function TUnixServer.Accept : Longint;
|
|
@@ -405,7 +421,7 @@ begin
|
|
L:=Length(FFileName);
|
|
L:=Length(FFileName);
|
|
Result:=Sockets.Accept(Socket,FUnixAddr,L);
|
|
Result:=Sockets.Accept(Socket,FUnixAddr,L);
|
|
If Result<0 then
|
|
If Result<0 then
|
|
- If SocketError=Sys_EWOULDBLOCK then
|
|
|
|
|
|
+ If SocketError=Sys_EWOULDBLOCK then
|
|
Raise ESocketError.Create(seAcceptWouldBlock,[socket])
|
|
Raise ESocketError.Create(seAcceptWouldBlock,[socket])
|
|
else
|
|
else
|
|
Raise ESocketError.Create(seAcceptFailed,[socket]);
|
|
Raise ESocketError.Create(seAcceptFailed,[socket]);
|
|
@@ -427,7 +443,7 @@ Constructor TInetSocket.Create(ASocket : Longint);
|
|
begin
|
|
begin
|
|
Inherited Create(ASocket);
|
|
Inherited Create(ASocket);
|
|
end;
|
|
end;
|
|
-
|
|
|
|
|
|
+
|
|
Constructor TInetSocket.Create(const AHost: String; APort: Word);
|
|
Constructor TInetSocket.Create(const AHost: String; APort: Word);
|
|
|
|
|
|
Var
|
|
Var
|
|
@@ -470,7 +486,7 @@ Constructor TUnixSocket.Create(ASocket : Longint);
|
|
begin
|
|
begin
|
|
Inherited Create(ASocket);
|
|
Inherited Create(ASocket);
|
|
end;
|
|
end;
|
|
-
|
|
|
|
|
|
+
|
|
Constructor TUnixSocket.Create(AFileName : String);
|
|
Constructor TUnixSocket.Create(AFileName : String);
|
|
|
|
|
|
Var S : Longint;
|
|
Var S : Longint;
|
|
@@ -492,12 +508,15 @@ begin
|
|
If Not Connect(ASocket,UnixAddr,AddrLen) then
|
|
If Not Connect(ASocket,UnixAddr,AddrLen) then
|
|
Raise ESocketError.Create(seConnectFailed,[FFilename]);
|
|
Raise ESocketError.Create(seConnectFailed,[FFilename]);
|
|
end;
|
|
end;
|
|
-
|
|
|
|
|
|
+
|
|
end.
|
|
end.
|
|
|
|
|
|
{
|
|
{
|
|
$Log$
|
|
$Log$
|
|
- Revision 1.4 2001-01-21 20:45:09 marco
|
|
|
|
|
|
+ Revision 1.5 2001-04-08 11:26:03 peter
|
|
|
|
+ * update so it can be compiled by both 1.0.x and 1.1
|
|
|
|
+
|
|
|
|
+ Revision 1.4 2001/01/21 20:45:09 marco
|
|
* Rename fest II FCL version.
|
|
* Rename fest II FCL version.
|
|
|
|
|
|
Revision 1.3 2000/11/17 13:40:53 sg
|
|
Revision 1.3 2000/11/17 13:40:53 sg
|