?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ;;; TPA SAFE TcpIP operations
  2.     module TcpIP
  3. START_RESOLVE = 6
  4. GET_DNS_RESULT = 7
  5. TCP_WAIT = 29
  6.  
  7. OPEN_TCP = 13
  8. CLOSE_TCP = 14
  9. ABORT_TCP = 15
  10. STATE_TCP = 16
  11. SEND_TCP = 17
  12. RECV_TCP = 18
  13.  
  14. ; C - init successful
  15. ; Closes all connections
  16. init:
  17.     call UnApi.preinit
  18.     ld hl, api
  19.     call UnApi.initApi
  20.     ret nc ; If error
  21.     call closeAll
  22.     scf
  23.     ret
  24.    
  25.  
  26. ; HL - domain string
  27. ; C - success flag
  28. ; L.H.E.D. - IP addr(if no error)
  29. resolveIp:
  30.     ld de, dnsbuff
  31. .loop
  32.     ld a, (hl)
  33.     ld (de), a
  34.     inc hl
  35.     inc de
  36.     and a
  37.     jr nz, .loop
  38.     ld (de), a
  39.    
  40.     ld hl, dnsbuff
  41.     ld a, START_RESOLVE
  42.     ld b, 0
  43.     call UnApi.tpaUnApiCall
  44.  
  45.     ld a, TCP_WAIT
  46.     call UnApi.tpaUnApiCall
  47. .resolveLoop
  48.     ld a, GET_DNS_RESULT
  49.     ld b, 1
  50.     call UnApi.tpaUnApiCall
  51.     and a  : jp nz, .err
  52.     ld a, b
  53.     cp 2 : jr nz, .resolveLoop
  54. .fin
  55.     scf
  56.     ret
  57. .err
  58.     or a
  59.     ret
  60.  
  61. ; L.H.E.D - IP addr
  62. ; BC - port
  63. ; Output:
  64. ; NZ - error flag
  65. ; A - error code
  66. ; B - socket id
  67. openTcp:
  68.     ld (.ip), hl
  69.     ld (.ip + 2), de
  70.     ld (.port), bc
  71.     ld hl, .buff
  72.     ld a, OPEN_TCP
  73.     call UnApi.tpaUnApiCall
  74.     and a : ret nz
  75.  
  76.     ld a, b
  77.     ld (.socket), a
  78. .establishWait
  79.     call TcpIP.wait
  80.  
  81.     ld a, (.socket)
  82.     ld b, a
  83.     call TcpIP.stateTcp
  84.  
  85.     and a
  86.     ret nz
  87.  
  88.     ld a, b
  89.     cp 4
  90.     jr nz, .establishWait
  91.    
  92.     ld a, (.socket)
  93.     ld b, a
  94.     xor a
  95.     ret
  96. .buff
  97. .ip        ds 4
  98. .port      dw 0
  99. .localPort dw #ffff
  100. .timeout   dw 0
  101. .flags     db 0
  102. .socket    db 0
  103.  
  104. ; A - socket
  105. ; Output:
  106. ; B - connection state
  107. ; HL - bytes avail
  108. ; IX - free bytes in output buffer
  109. stateTcp:
  110.     ld b, a
  111.     ld hl, 0
  112.     ld a, STATE_TCP
  113.     jp UnApi.tpaUnApiCall
  114.    
  115.  
  116. ; A - socket
  117. ; DE - buffer
  118. ; HL - data len
  119. sendTCP:
  120.     push hl
  121.     ld bc, hl
  122.     ex hl, de
  123.     ld de, tcpBuff
  124.     ldir
  125.     pop hl
  126.     ld b, a
  127.     ld de, tcpBuff
  128.     ld c, 0
  129.     ld a, TcpIP.SEND_TCP
  130.     jp UnApi.tpaUnApiCall
  131.  
  132. ; A - socket
  133. ; HL - data len
  134. ; BC - actually received
  135. recvTCP:
  136.     ld b, a
  137.  
  138. ; 512 Bytes buffer limit
  139.     ld a, h
  140.     and a  
  141.     jr z, .skip
  142.     ld a, 1
  143. .skip
  144.     ld h, a
  145.  
  146.     ld a, RECV_TCP
  147.     ld de, tcpBuff
  148.     jp UnApi.tpaUnApiCall
  149.  
  150.  
  151. ; A - socket id
  152. closeTcp:
  153.     ld b, a
  154.     ld a, CLOSE_TCP
  155.     jp UnApi.tpaUnApiCall
  156.  
  157.  
  158. closeAll:
  159.     ld a, CLOSE_TCP
  160.     ld b, 0
  161.     call UnApi.tpaUnApiCall
  162. wait:
  163.     ld a, TCP_WAIT
  164.     jp UnApi.tpaUnApiCall
  165.  
  166. dnsbuff ds 64
  167. tcpBuff ds TCP_BUF_SIZE
  168.  
  169. api  db "TCP/IP", 0
  170.     endmodule