home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / musica / getcddb / source / mytcpf~1.bb2 < prev    next >
Text File  |  2000-01-07  |  5KB  |  178 lines

  1. ;MyTCPFuncs.bb2
  2.  
  3. ; Blitz TCP functions as mentioned below by Paul Burkey modified to use the 
  4. ; assemblified bsdsocket.library functions and all unused functions removed. 
  5.  
  6. ;-----------------------------------------------------------
  7. ; Standard Blitz TCP Functions V1.8 by Paul Burkey (c)1997-1998
  8. ; Compiled with help from Ercole Spiteri and Anton Reinauer
  9. ; Contact me at burkey@bigfoot.com
  10. ;-----------------------------------------------------------
  11. ;History
  12. ;-------
  13. ;<16.2.97> Version 1.8
  14. ;Added NLPrintTCP{} for easy send string with carrage return and newline.
  15. ;Removed need for 3rd Party libs (only bsdsocket.library needed)
  16. ;
  17. ;<24.12.97> Version 1.7
  18. ;ReadTCP{} Updated with extra safety and Speed
  19. ;
  20. ;<18.9.97> Version 1.6
  21. ;Added PrintTCP{}  for an easy "send string" command.
  22. ;Added NPrintTCP{} for easy send string with carrage return
  23. ;CheckTCP{} merged into the ConnectTCP{} function.
  24. ;
  25. ;---------------
  26. ; Function List
  27. ;---------------
  28. ;
  29. ;ReadTCP{}                       ; Similar to Edit$() - recives data via TCP connection
  30. ;ReadMemTCP{ReadAdd.l,MaxSize.l} ; Similar to ReamMem - recives data via TCP connection
  31. ;WriteTCP{ad.l,size.w}           ; Similar to WriteMem - sends data via TCP connection
  32. ;ConnectTCP{host$,port.w}        ; Connect to a remote machine (Full error checking)
  33. ;PrintTCP{text$}                 ; Similar to Print - sends data via TCP connection
  34. ;NPrintTCP{text$}                ; Similar to NPrint - sends data via TCP connection
  35. ;NLPrintTCP{text$}               ; Similar to Print+CR+LF - sends data via TCP connection
  36. ;CloseTCP{}                      ; Closes TCP Connection
  37.  
  38. ;XINCLUDE "GetCDDB_MiscFuncs.bb2"
  39. ;---------------------------------
  40. ; TCP library Variables/Constants
  41. ;---------------------------------
  42.  
  43. #TCPBuflen=$2048                ;Maximum data size to read at any time
  44. TCPmem.l=AllocMem(#TCPBuflen,0) ;Allocate the temp buffer used for all TCP reads
  45. #FIONREAD=$4004667f             ;FIONREAD request
  46.  
  47. ;---------------------------------
  48. ; Standard TCP library structures
  49. ;---------------------------------
  50.  
  51. NEWTYPE.list
  52.   *ItemA.b
  53.   *ItemB.b
  54. End NEWTYPE
  55. NEWTYPE.inaddr
  56.   s_addr.l
  57. End NEWTYPE
  58. NEWTYPE.sockaddrin
  59.   sin_len.b
  60.   sin_family.b
  61.   sin_port.w
  62.   sin_addr.inaddr
  63.   sin_zero.b[8]
  64. End NEWTYPE
  65. NEWTYPE.hostent
  66.   *h_name.b
  67.   *h_aliases.list
  68.   h_addrtype.l
  69.   h_lenght.l
  70.   *h_addr_list.list
  71. End NEWTYPE
  72.  
  73. ;---------------------------------
  74. ; Standard TCP Blitz Functions
  75. ;---------------------------------
  76.  
  77. .ReadTCP
  78. Function .s ReadTCP{}
  79.   SHARED sock.l,TCPmem.l
  80.   ;
  81.   ; This Function reads data from the server the result is passed back in a
  82.   ; string. If there is no messages then it will return an empty string =""
  83.   ;
  84.   sockread.l=0                                ;Clear Readmask
  85.   sockread.l BitSet sock.l                    ;Set Readmask on our socket
  86.   e.l=IoctlSocket_(sock.l,#FIONREAD,TCPmem.l) ;How much data is there?
  87.   f.l=Peek.l(TCPmem.l)                        ;Place value in f
  88.   If f>0
  89.     If f>#TCPBuflen Then f=#TCPBuflen         ;Don't read more than #TCPBuflen
  90.     c=recv_(sock.l,TCPmem.l,f,0)              ;Read all Data
  91.     c$=String$(" ",f)                         ;Reserve String
  92.     CopyMem_ TCPmem.l,&c$,f                   ;Copy Data to string
  93.     Function Return c$
  94.   Else
  95.     Function Return ""
  96.   EndIf
  97. End Function
  98.  
  99.  
  100. .WriteMemTCP
  101. Statement WriteMemTCP{ad.l,size.w}
  102.   SHARED sock.l
  103.   ;
  104.   ; This routine writes data via TCP.
  105.   ;
  106.   sockwrite.l=0                           ;Clear Writemask
  107.   sockwrite.l BitSet sock.l               ;set Writemask on our socket
  108.   g.l=WaitSelect_(2,0,&sockwrite.l,0,0,0) ;Wait until server is ready to read our data
  109.   c.l=send_(sock.l,ad,size,0)             ;Send data to server
  110. End Statement
  111.  
  112.  
  113.  
  114. .ConnectTCP
  115. Function .b ConnectTCP{host$,port.w}
  116.   SHARED sock.l
  117.   SHARED SocketBase.l
  118.   ;
  119.   ; Check if Miami/AmiTCP stack is available
  120.   ; Connect to host at specified port
  121.   ; Return true or False if Connection is made
  122.  
  123.   SocketBase.l=OpenLibrary_("bsdsocket.library",0)
  124.   If SocketBase=0
  125.     Function Return 0
  126.   Else
  127.     CloseLibrary_(SocketBase)
  128.     sock.l=socket_(2,1,0)
  129.     *a.hostent=gethostbyname_(host$)
  130.     If *a=0
  131.       Function Return 0   ; host not found (or internal TCP error)
  132.     Else
  133.       ;
  134.       ; Copy Details to our Sockaddrin structure
  135.       ;
  136.       CopyMem_ *a\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a\h_lenght
  137.       host.sockaddrin\sin_port=port       ;Set port number
  138.       host.sockaddrin\sin_family=2        ;Set type to AT_INET
  139.       StructLength.l=SizeOf .sockaddrin   ;Get lenght of structure sockaddrin
  140.       If connect_(sock.l,host.sockaddrin,StructLength)=-1
  141.         CloseSocket_(sock.l)
  142.         Function Return 0
  143.       Else
  144.         Function Return -1
  145.       EndIf
  146.     EndIf
  147.   EndIf
  148. End Function
  149.  
  150.  
  151.  
  152. .PrintTCP
  153. Statement PrintTCP{text$}
  154.   ;
  155.   ; Send String via TCP
  156.   ;
  157.   WriteMemTCP{&text$,Len(text$)}
  158. End Statement
  159.  
  160. .CloseTCP
  161. Statement CloseTCP{}
  162.   SHARED sock.l
  163.   SHARED SocketBase.l
  164.   ;
  165.   ; This is a simple close socket command
  166.   ; Provided for the shear hell of it :)
  167.   ;
  168.   CloseSocket_(sock.l)
  169.  
  170.   If SocketBase.l
  171.     CloseLibrary_(SocketBase)
  172.   EndIf
  173. End Statement
  174.  
  175.  
  176.  
  177.  
  178.