amiga-bsdsocket-coding/amos_pro_original/BSDSocket.txt

194 lines
4.3 KiB
Plaintext
Raw Normal View History

2023-01-11 13:03:46 +00:00
'BSDSocket.library Procedures
'By John Bintz
'
'Licensed under the MIT License
'Extension 16 is the EasyLife extension
'Open bsdsocket.library
Procedure BSD_OPEN
Lib Open 5,"bsdsocket.library",0
End Proc
'Close bsdsocket.library
Procedure BSD_CLOSE
Lib Close 5
End Proc
'Create New Socket
Procedure SOCK_NEW
Dreg(0)=2
Dreg(1)=1
Dreg(2)=0
WHICH=Lib Call(5,-$1E)
End Proc[WHICH]
'Set Memory Address to Place Errors
Procedure SOCK_ERR[VARADDR]
Areg(0)=VARADDR
Dreg(0)=4
SUCCESS=Lib Call(5,-$A8)
End Proc
'Open A Server Socket on Your Machine at PORT (mymachine.com:PORT)
Procedure SOCK_OPENSERVER[SPORT]
Proc SOCK_NEW : SOCKET=Param
SOCK_GETYOURHOST
A$=Param$+Chr$(0)
Areg(0)=Varptr(A$)
HOSTINFO=Lib Call(5,-$D2)
HOSTADDRESS=Leek(Leek(Leek(HOSTINFO+16)))
A$=Chr$(16)+Chr$(2)+ Extension_16_04EA(SPORT)+ Extension_16_04CE(HOSTADDRESS)+String$(Chr$(0),8)
Dreg(0)=SOCKET
Areg(0)=Varptr(A$)
Dreg(1)=Len(A$)
BIND=Lib Call(5,-$24)
Dreg(0)=SOCKET
Dreg(1)=1
LISTEN=Lib Call(5,-$2A)
SOCK_SETIO[SOCKET]
If BIND=-1
SOCK_CLOSE[SOCKET]
SOCKET=-1
End If
End Proc[SOCKET]
'Set Non-Blocking/Asynchronous IO
Procedure SOCK_SETIO[SOCKET]
IOC_FIONBIO=($80000000 or(4*65536) or 126 or(Asc("f")*256))
IOC_FIOASYNC=($80000000 or(4*65536) or 125 or(Asc("f")*256))
NBIO=1
Dreg(0)=SOCKET : Dreg(1)=IOC_FIONBIO : Areg(0)=Varptr(NBIO)
NOBLOCK=Lib Call(5,-$72)
NBIO=1
Dreg(0)=SOCKET : Dreg(1)=IOC_FIOASYNC : Areg(0)=Varptr(NBIO)
NOBLOCK=Lib Call(5,-$72)
End Proc
'Connect to Another Computer (tcp://HOST$:SPORT/)
Procedure SOCK_OPENCONNECTION[HOST$,SPORT]
Proc SOCK_NEW : SOCKET=Param
A$=HOST$+Chr$(0)
Areg(0)=Varptr(A$)
HOSTINFO=Lib Call(5,-$D2)
HOSTADDRESS=Leek(Leek(Leek(HOSTINFO+16)))
A$=Chr$(16)+Chr$(2)+ Extension_16_04EA(SPORT)+ Extension_16_04CE(HOSTADDRESS)+String$(Chr$(0),8)
Dreg(0)=SOCKET
Areg(0)=Varptr(A$)
Dreg(1)=Len(A$)
CONNECT=Lib Call(5,-$36)
SOCK_SETIO[SOCKET]
If CONNECT=-1
SOCK_CLOSE[SOCKET]
SOCKET=-1
End If
End Proc[SOCKET]
'Check Your Host Port for Connection and Create A New Socket If So
Procedure SOCK_CHECKHOSTPORT[SOCKET]
A$=String$(Chr$(0),200)
FINSIZE=64
Dreg(0)=SOCKET
Areg(0)=Varptr(A$)
Areg(1)=Varptr(FINSIZE)
NEWSOCKET=Lib Call(5,-$30)
SOCK_CONNURL= Extension_16_04DC(Mid$(A$,8,4))
End Proc[NEWSOCKET]
'Send A String to a Socket
Procedure SOCK_SENDSTRING[SOCKET,A$]
If SOCKET<>-1
Dreg(0)=SOCKET
Areg(0)=Varptr(A$)
Dreg(1)=Len(A$)
Dreg(2)=0
NBYTES=Lib Call(5,-$42)
End If
End Proc[NBYTES]
'Read A String from a Socket
Procedure SOCK_READSTRING[SOCKET,MXSIZE]
If SOCKET<>-1
A$=Space$(MXSIZE)
Dreg(0)=SOCKET
Areg(0)=Varptr(A$)
Dreg(1)=MXSIZE
Dreg(2)=0
NBYTES=Lib Call(5,-$4E)
If NBYTES>0
A$=Left$(A$,NBYTES)
Else
A$=""
End If
Else
A$=""
End If
End Proc[A$]
'Send A Memory Buffer to a Socket
Procedure SOCK_SEND[SOCKET,ADR,ADRLEN]
Dreg(0)=SOCKET
Areg(0)=ADR
Dreg(1)=ADRLEN
Dreg(2)=0
NBYTES=Lib Call(5,-$42)
End Proc[NBYTES]
'Read A Memory Buffer from a Socket
Procedure SOCK_READ[SOCKET,ADR,ADRLEN]
Dreg(0)=SOCKET
Areg(0)=ADR
Dreg(1)=ADRLEN
Dreg(2)=0
NBYTES=Lib Call(5,-$4E)
End Proc[NBYTES]
'Get Your Hostname (PPP)
Procedure SOCK_GETYOURHOST
Open In 1,"env:hostname"
A$=Input$(1,Lof(1)) : Close 1
End Proc[A$]
'Close A Socket (Save Memory)
Procedure SOCK_CLOSE[SOCKET]
If SOCKET<>-1
Dreg(0)=SOCKET
SUCCESS=Lib Call(5,-$78)
Print SUCCESS,SOCK_ERRNO
End If
End Proc
'These Commands are especially for FTP, which is weird...
'Open An FTP Connection (ftp://HOST$:SPORT/)
Procedure FTP_OPENCONNECTION[HOST$,SPORT]
Shared _FTP_HOSTSOCK,_FTP_YOURSOCK,_FTP_YOURPORT$
SOCK_OPENCONNECTION[HOST$,SPORT] : _FTP_HOSTSOCK=Param
SOCK_OPENSERVER[5000] : _FTP_YOURSOCK=Param
P$="" : H$=Mid$(Hex$(HOSTADDRESS,8),2)
For I=0 To 3
P$=P$+Str$(Val("$"+Mid$(H$,I*2+1,2)))+","
Next
P$=P$-" "
_FTP_YOURPORT$="PORT "+P$+"19,136"+Chr$(13)+Chr$(10)
End Proc
'Grab Host Socket ID
Procedure FTP_HOSTSOCK
Shared _FTP_HOSTSOCK
End Proc[_FTP_HOSTSOCK]
'Grab Your Socket ID
Procedure FTP_YOURSOCK
Shared _FTP_YOURSOCK
End Proc[_FTP_YOURSOCK]
'Grab PORT String (used to send files via FTP)
Procedure FTP_YOURPORT$
Shared _FTP_YOURPORT$
End Proc[_FTP_YOURPORT$]
'Close an FTP Connection
Procedure FTP_CLOSECONNECTION
Shared _FTP_HOSTSOCK,_FTP_YOURSOCK
SOCK_CLOSE[_FTP_HOSTSOCK]
SOCK_CLOSE[_FTP_YOURSOCK]
End Proc