|
@@ -11,6 +11,206 @@
|
|
|
|
|
|
**********************************************************************}
|
|
**********************************************************************}
|
|
|
|
|
|
|
|
+{******************************************************************************
|
|
|
|
+ Text File Writeln/ReadLn Support
|
|
|
|
+******************************************************************************}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+Procedure OpenSock(var F:Text);
|
|
|
|
+begin
|
|
|
|
+ if textrec(f).handle=UnusedHandle then
|
|
|
|
+ textrec(f).mode:=fmclosed
|
|
|
|
+ else
|
|
|
|
+ case textrec(f).userdata[1] of
|
|
|
|
+ S_OUT : textrec(f).mode:=fmoutput;
|
|
|
|
+ S_IN : textrec(f).mode:=fminput;
|
|
|
|
+ else
|
|
|
|
+ textrec(f).mode:=fmclosed;
|
|
|
|
+ end;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+procedure iosock(var f:text);
|
|
|
|
+
|
|
|
|
+var r:sizeint;
|
|
|
|
+ def_error:word;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ with textrec(f) do
|
|
|
|
+ begin
|
|
|
|
+ case mode of
|
|
|
|
+ fmoutput:
|
|
|
|
+ begin
|
|
|
|
+ repeat
|
|
|
|
+{$ifdef use_readwrite}
|
|
|
|
+ r:=fpwrite(handle,bufptr^,bufpos);
|
|
|
|
+{$else}
|
|
|
|
+ r:=send(handle,bufptr^,bufpos,0);
|
|
|
|
+{$endif}
|
|
|
|
+ until (r<>-1) or (errno<>EsysEINTR);
|
|
|
|
+ bufend:=r;
|
|
|
|
+ def_error:=101; {File write error.}
|
|
|
|
+ end;
|
|
|
|
+ fminput:
|
|
|
|
+ begin
|
|
|
|
+ repeat
|
|
|
|
+{$ifdef use_readwrite}
|
|
|
|
+ r:=fpread(handle,bufptr^,bufsize);
|
|
|
|
+{$else}
|
|
|
|
+ r:=recv(handle,bufptr^,bufsize,0);
|
|
|
|
+{$endif}
|
|
|
|
+ until (r<>-1) or (errno<>EsysEINTR);
|
|
|
|
+ bufend:=r;
|
|
|
|
+ def_error:=100; {File read error.}
|
|
|
|
+ end;
|
|
|
|
+ end;
|
|
|
|
+ if r=-1 then
|
|
|
|
+ case errno of
|
|
|
|
+ EsysEBADF:
|
|
|
|
+{ EsysENOTSOCK:} {Why is this constant not defined? (DM)}
|
|
|
|
+ inoutres:=6; {Invalid file handle.}
|
|
|
|
+ EsysEFAULT:
|
|
|
|
+ inoutres:=217;
|
|
|
|
+ EsysEINVAL:
|
|
|
|
+ inoutres:=218;
|
|
|
|
+ else
|
|
|
|
+ inoutres:=def_error;
|
|
|
|
+ end;
|
|
|
|
+ bufpos:=0;
|
|
|
|
+ end;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+Procedure FlushSock(var F:Text);
|
|
|
|
+begin
|
|
|
|
+ if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
|
|
|
|
+ begin
|
|
|
|
+ IOSock(f);
|
|
|
|
+ textrec(f).bufpos:=0;
|
|
|
|
+ end;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+Procedure CloseSock(var F:text);
|
|
|
|
+begin
|
|
|
|
+ { Nothing special has to be done here }
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+Procedure Sock2Text(Sock:Longint;Var SockIn,SockOut:Text);
|
|
|
|
+{
|
|
|
|
+ Set up two Pascal Text file descriptors for reading and writing)
|
|
|
|
+}
|
|
|
|
+begin
|
|
|
|
+{ First the reading part.}
|
|
|
|
+ Assign(SockIn,'.');
|
|
|
|
+ Textrec(SockIn).Handle:=Sock;
|
|
|
|
+ Textrec(Sockin).userdata[1]:=S_IN;
|
|
|
|
+ TextRec(SockIn).OpenFunc:=@OpenSock;
|
|
|
|
+ TextRec(SockIn).InOutFunc:=@IOSock;
|
|
|
|
+ TextRec(SockIn).FlushFunc:=@FlushSock;
|
|
|
|
+ TextRec(SockIn).CloseFunc:=@CloseSock;
|
|
|
|
+ TextRec(SockIn).Mode := fmInput;
|
|
|
|
+{ Now the writing part. }
|
|
|
|
+ Assign(SockOut,'.');
|
|
|
|
+ Textrec(SockOut).Handle:=Sock;
|
|
|
|
+ Textrec(SockOut).userdata[1]:=S_OUT;
|
|
|
|
+ TextRec(SockOut).OpenFunc:=@OpenSock;
|
|
|
|
+ TextRec(SockOut).InOutFunc:=@IOSock;
|
|
|
|
+ TextRec(SockOut).FlushFunc:=@FlushSock;
|
|
|
|
+ TextRec(SockOut).CloseFunc:=@CloseSock;
|
|
|
|
+ TextRec(SockOut).Mode := fmOutput;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+{******************************************************************************
|
|
|
|
+ Untyped File
|
|
|
|
+******************************************************************************}
|
|
|
|
+
|
|
|
|
+Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);
|
|
|
|
+begin
|
|
|
|
+{Input}
|
|
|
|
+ Assign(SockIn,'.');
|
|
|
|
+ FileRec(SockIn).Handle:=Sock;
|
|
|
|
+ FileRec(SockIn).RecSize:=1;
|
|
|
|
+ FileRec(Sockin).userdata[1]:=S_IN;
|
|
|
|
+ FileRec(SockIn).Mode := fmInput;
|
|
|
|
+
|
|
|
|
+{Output}
|
|
|
|
+ Assign(SockOut,'.');
|
|
|
|
+ FileRec(SockOut).Handle:=Sock;
|
|
|
|
+ FileRec(SockOut).RecSize:=1;
|
|
|
|
+ FileRec(SockOut).userdata[1]:=S_OUT;
|
|
|
|
+ FileRec(SockOut).Mode := fmOutput;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+{******************************************************************************
|
|
|
|
+ InetSock
|
|
|
|
+******************************************************************************}
|
|
|
|
+
|
|
|
|
+Function DoAccept(Sock:longint;Var addr:TInetSockAddr):longint;
|
|
|
|
+
|
|
|
|
+Var AddrLen : Longint;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ AddrLEn:=SizeOf(Addr);
|
|
|
|
+ DoAccept:=Accept(Sock,Addr,AddrLen);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+Function DoConnect(Sock:longint;const addr: TInetSockAddr): Boolean;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ DoConnect:=Connect(Sock,Addr,SizeOF(TInetSockAddr));
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+Function Connect(Sock:longint;const addr: TInetSockAddr;var SockIn,SockOut:text):Boolean;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ Connect:=DoConnect(Sock,addr);
|
|
|
|
+ If Connect then
|
|
|
|
+ Sock2Text(Sock,SockIn,SockOut);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ Connect:=DoConnect(Sock,addr);
|
|
|
|
+ If Connect then
|
|
|
|
+ Sock2File(Sock,SockIn,SockOut);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
|
|
|
|
+var
|
|
|
|
+ s : longint;
|
|
|
|
+begin
|
|
|
|
+ S:=DoAccept(Sock,addr);
|
|
|
|
+ if S>0 then
|
|
|
|
+ begin
|
|
|
|
+ Sock2Text(S,SockIn,SockOut);
|
|
|
|
+ Accept:=true;
|
|
|
|
+ end
|
|
|
|
+ else
|
|
|
|
+ Accept:=false;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File):Boolean;
|
|
|
|
+var
|
|
|
|
+ s : longint;
|
|
|
|
+begin
|
|
|
|
+ S:=DoAccept(Sock,addr);
|
|
|
|
+ if S>0 then
|
|
|
|
+ begin
|
|
|
|
+ Sock2File(S,SockIn,SockOut);
|
|
|
|
+ Accept:=true;
|
|
|
|
+ end
|
|
|
|
+ else
|
|
|
|
+ Accept:=false;
|
|
|
|
+end;
|
|
|
|
+
|
|
type thostaddr= packed array[1..4] of byte;
|
|
type thostaddr= packed array[1..4] of byte;
|
|
|
|
|
|
function htonl( host : longint):longint; inline;
|
|
function htonl( host : longint):longint; inline;
|