Subversion Repositories Kolibri OS

Rev

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

  1. use32
  2. ; standard header
  3.         db      'MENUET01'      ; signature
  4.         dd      1               ; header version
  5.         dd      start           ; entry point
  6.         dd      i_end           ; initialized size
  7.         dd      mem             ; required memory
  8.         dd      mem             ; stack pointer
  9.         dd      0               ; parameters
  10.         dd      0               ; path
  11.  
  12.  
  13. BUFFERSIZE      equ 1500
  14. ; useful includes
  15. include '../macros.inc'
  16. purge mov,add,sub
  17. include '../proc32.inc'
  18. include '../dll.inc'
  19.  
  20. include '../network.inc'
  21.  
  22. ; entry point
  23. start:
  24. ; load libraries
  25.         stdcall dll.Load, @IMPORT
  26.         test    eax, eax
  27.         jnz     exit
  28.  
  29. ; initialize console
  30.         push    1
  31.         call    [con_start]
  32.         push    title
  33.         push    25
  34.         push    80
  35.         push    25
  36.         push    80
  37.         call    [con_init]
  38.  
  39.         mcall   40, 1 shl 7     ; we only want network events
  40.  
  41.         push    str1
  42.         call    [con_write_asciiz]
  43.  
  44.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  45.         cmp     eax, -1
  46.         je      sock_err
  47.  
  48.         mov     [socketnum], eax
  49.  
  50. ;;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
  51. ;;        cmp     eax, -1
  52. ;;        je      opt_err
  53.  
  54.         mcall   bind, [socketnum], sockaddr1, sockaddr1.length
  55.         cmp     eax, -1
  56.         je      bind_err
  57.  
  58.         mcall   listen, [socketnum], 10 ; Backlog = 10
  59.         cmp     eax, -1
  60.         je      listen_err
  61.  
  62.         push    str2
  63.         call    [con_write_asciiz]
  64.  
  65.         mcall   10
  66.  
  67.         mcall   accept, [socketnum], sockaddr1, sockaddr1.length
  68.         cmp     eax, -1
  69.         je      acpt_err
  70.  
  71.         mov     [socketnum2], eax
  72.  
  73. ;;        mcall   close, [socketnum]
  74.  
  75.         mcall   send, [socketnum2], hello, hello.length
  76.  
  77.   .loop:
  78.         mcall   10
  79.  
  80.         mcall   recv, [socketnum2], buffer, buffer.length
  81.         cmp     eax, -1
  82.         je      .loop
  83.  
  84.         mov     byte [buffer + eax], 0
  85.  
  86.         push    buffer
  87.         call    [con_write_asciiz]
  88.  
  89.         jmp     .loop
  90.  
  91. acpt_err:
  92.         push    str8
  93.         call    [con_write_asciiz]
  94.         jmp     done
  95.  
  96. listen_err:
  97.         push    str3
  98.         call    [con_write_asciiz]
  99.         jmp     done
  100.  
  101. bind_err:
  102.         push    str4
  103.         call    [con_write_asciiz]
  104.         jmp     done
  105.  
  106. sock_err:
  107.         push    str6
  108.         call    [con_write_asciiz]
  109.         jmp     done
  110.  
  111. done:
  112.         call    [con_getch2]
  113.         push    1
  114.         call    [con_exit]
  115. exit:
  116.         mcall   -1
  117.  
  118.  
  119.  
  120. ; data
  121. title   db      'TCP stream server - test',0
  122. str1    db      'Opening socket',10, 0
  123. str2    db      'Listening for incoming connections...',10,0
  124. str3    db      'Listen error',10,10,0
  125. str4    db      'Bind error',10,10,0
  126. str5    db      'Setsockopt error.',10,10,0
  127. str6    db      'Could not open socket',10,10,0
  128. str7    db      'Got data!',10,10,0
  129. str8    db      'Error accepting connection',10,10,0
  130.  
  131. hello   db      'Hello world!',0
  132. .length = $ - hello
  133.  
  134. sockaddr1:
  135.         dw AF_INET4
  136. .port   dw 23
  137. .ip     dd 0
  138.         rb 10
  139. .length = $ - sockaddr1
  140.  
  141. ; import
  142. align 4
  143. @IMPORT:
  144.  
  145. library console, 'console.obj'
  146.  
  147. import  console,        \
  148.         con_start,      'START',        \
  149.         con_init,       'con_init',     \
  150.         con_write_asciiz,       'con_write_asciiz',     \
  151.         con_exit,       'con_exit',     \
  152.         con_gets,       'con_gets',\
  153.         con_cls,        'con_cls',\
  154.         con_printf,     'con_printf',\
  155.         con_getch2,     'con_getch2',\
  156.         con_set_cursor_pos, 'con_set_cursor_pos'
  157. i_end:
  158.  
  159. socketnum       dd ?
  160. socketnum2      dd ?
  161. buffer         rb BUFFERSIZE
  162. .length = BUFFERSIZE
  163.  
  164. align   4
  165. rb      4096    ; stack
  166. mem:
  167.