home *** CD-ROM | disk | FTP | other *** search
- ;-----------------------------------------------------------
- ; UDP_Funcs V1.4 23/04/99 based on
- ; Standard Blitz TCP Functions V1.5 by Paul Burkey (c)1997
- ; Compiled with help from Ercole Spiteri and Anton Reinauer
- ; You must have the bsdsocket.library setup in Blitz
- ; by Anton Reinauer anton@ww.co.nz
- ;-----------------------------------------------------------
-
- ;---------------------------------
- ; Standard UDP Blitz Functions
- ;---------------------------------
-
- .Get_Host_By_Name ; get a host structure by name (if exists), from a name server
- Function Get_Host_By_Name{host$,port.w,player}
- SHARED host(),hostlen()
-
- *a.hostent=gethostbyname_(host$) ; set up destination address to send packets to
- If *a.hostent=0
- Security_Warning{"Couldn't get Localhost name!",#HIGH}
- Function Return False
- EndIf
-
- ;Copy Details to our Sockaddrin structure
-
- bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host(player)\sin_addr,*a.hostent\h_length)
-
- host(player)\sin_port=port ;Set port number
- host(player)\sin_family=2 ;Set type to AT_INET
- hostlen(player)=SizeOf.sockaddrin ;Get length of structure sockaddrin
-
- Function Return True
- End Function
-
- .Get_Host_By_Address ; get a host structure by address (if exists), from a name server
- Function.b Get_Host_By_Address{address.l,port,player}
- SHARED host(),hostlen()
-
- *a.hostent=gethostbyaddr_(address,4,2) ; check wether host exists- we're not being shammed
-
- If *a.hostent=0
- Security_Warning{"Couldn't get Localhost name!",#HIGH}
- Function Return False
- EndIf
-
- bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host(player)\sin_addr,*a.hostent\h_length) ; copy details to player's host
- ; newtype to host
- host(player)\sin_port=port ;Set port number
- host(player)\sin_family=2 ;Set type to AT_INET
- hostlen(player)=SizeOf.sockaddrin ;Get length of structure sockaddrin
- Function Return True
- End Function
-
- .Localhost_Name
- Function.s Localhost_Name{}
- If OpenFile (0,"ENV:HOSTNAME")
- FileInput 0
- While NOT Eof(0)
- a$=a$+Inkey$
- Wend
- CloseFile 0
- WindowInput 0
- Function Return a$
- EndIf
- Function Return "localhost"
- End Function
-
- .Initialise_UDP
- Function Initialise_UDP{port.w}
- SHARED sock.l,host(),hostlen(),port_used,hostname,tcp_stack
- SHARED UDPmem.l
- ;
- ; Open UDP socket and bind it to a port number
- ; Return true or False if successful or not
- ;
-
- If tcp_stack=False ; if we've haven't already Initialised UDP
- CNIF #NO_CONNECTION=1 ; if not connected to the `net- just running locally
- host$=hostname
- CELSE ; are connected to the net or have been since computer was booted up
- host$=Localhost_Name{}
- CEND
-
- lib.l=OpenLibrary_("bsdsocket.library",0) ; check to see if a TCP/IP stack is running
- If lib=0
- Security_Warning{"TCP/IP stack not running!",#HIGH}
- Function Return False
- Else
- CloseLibrary_(lib) ; close lib again or Miami won't exit
- sock.l=socket_(2,2,0) ; open UDP socket
- If sock=-1
- Security_Warning{"Couldn't open UDP socket!",#HIGH}
- Function Return False
- Else
- If Get_Host_By_Name{host$,port,0}=False
- Function Return False
- EndIf
- temp=0
- port_used=port
- Repeat ; for testing on localhost- keep trying one port at a time after #LOCALPORT to find a free port
- host(0)\sin_port=port_used
- If bind_(sock,host(0),hostlen(0))=0 ; bind socket to port so we can
- tcp_stack=True ; note that we have initialised the UDP socket
- UDPmem=AllocMem($2000,0) ;Allocate temp buffer used for all UDP reads
- Function Return True ; receive data on port
- Else
- port_used+1
- temp+1
- EndIf
- Until temp=10
- Security_Warning{"Couldn't Bind a UDP socket!",#HIGH}
- EndIf
- EndIf
- Else
- Function Return True ; if UDP socket is already initialised
- EndIf
- Function Return False
- End Function
-
- .ReadUDP
- Function .s ReadUDP{}
- SHARED sock.l,UDPmem.l,temphost,temphostlen
-
- ; This Function reads data from the socket it is bound to.
- ; If there is no messages then it will return an empty string =""
-
- sockread.l=0 ;Clear Readmask
- sockread.l BitSet sock.l ;Set Readmask on our socket
- e=IoctlSocket_(sock.l,#FIONREAD,UDPmem.l) ;How much data is there?
- f.l=Peek.l(UDPmem.l) ;Place value in f
-
- If f>0 ; any data waiting
- temphostlen=SizeOf .sockaddrin
- c=recvfrom_(sock.l,UDPmem.l,f,0,temphost,temphostlen) ;Read all data waiting on socket
- ;and get host it was sent from in temphost
- If c>0 ; if there any data waiting
- a=0 : c$=""
- Repeat
- c$=c$+Chr$(Peek.b(UDPmem+a)) : a+1 ; build string
- Until a=c
- EndIf
-
- EndIf
-
- Function Return c$
- End Function
-
- .WriteUDP
- Statement WriteUDP{ad.l,size.w,player}
- SHARED sock.l,host(),hostlen(),temphost,temphostlen
-
- ; This routine writes data via UDP
-
- sockwrite.l=0 ;Clear Writemask
- sockwrite.l BitSet sock.l ;set Writemask on our socket
- g=WaitSelect_(2,0,&sockwrite.l,0,0,0) ;Wait until server is ready to read our data
-
- If player>0
- c=sendto_(sock.l,ad,size,0,host(player),hostlen(player)) ;Send data to online host
- Else
- c=sendto_(sock.l,ad,size,0,temphost,temphostlen) ;Send data to host that's not connected
- EndIf
- End Statement
-
- .Close_UDP ; close down UDP socket and read buffer
- Statement Close_UDP{}
- SHARED UDPmem,sock.l,tcp_stack
-
- If UDPmem>0
- FreeMem UDPmem,$2000 ; free UDP read memory buffer
- UDPmem=0
- EndIf
- If sock>=0 ; if we've successfully opened a socket then close it
- CloseSocket_(sock)
- CNIF #COMMS_DEBUG=1
- Comms_Debug{"Socket closed",#LOW}
- CEND
- EndIf
- tcp_stack=False
- End Statement
-
-
-