Subversion Repositories Kolibri OS

Rev

Go to most recent revision | 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.  
  82.         push    buffer
  83.         call    [con_write_asciiz]
  84.  
  85.         jmp     .loop
  86.  
  87. acpt_err:
  88.         push    str8
  89.         call    [con_write_asciiz]
  90.         jmp     done
  91.  
  92. listen_err:
  93.         push    str3
  94.         call    [con_write_asciiz]
  95.         jmp     done
  96.  
  97. bind_err:
  98.         push    str4
  99.         call    [con_write_asciiz]
  100.         jmp     done
  101.  
  102. sock_err:
  103.         push    str6
  104.         call    [con_write_asciiz]
  105.         jmp     done
  106.  
  107. done:
  108.         call    [con_getch2]
  109.         push    1
  110.         call    [con_exit]
  111. exit:
  112.         mcall   -1
  113.  
  114.  
  115.  
  116. ; data
  117. title   db      'TCP stream server - test',0
  118. str1    db      'Opening socket',10, 0
  119. str2    db      'Listening for incoming connections...',10,0
  120. str3    db      'Listen error',10,10,0
  121. str4    db      'Bind error',10,10,0
  122. str5    db      'Setsockopt error.',10,10,0
  123. str6    db      'Could not open socket',10,10,0
  124. str7    db      'Got data!',10,10,0
  125. str8    db      'Error accepting connection',10,10,0
  126.  
  127. hello   db      'Hello world!',0
  128. .length = $ - hello
  129.  
  130. sockaddr1:
  131.         dw AF_INET4
  132. .port   dw 23
  133. .ip     dd 0
  134.         rb 10
  135. .length = $ - sockaddr1
  136.  
  137. ; import
  138. align 4
  139. @IMPORT:
  140.  
  141. library console, 'console.obj'
  142.  
  143. import  console,        \
  144.         con_start,      'START',        \
  145.         con_init,       'con_init',     \
  146.         con_write_asciiz,       'con_write_asciiz',     \
  147.         con_exit,       'con_exit',     \
  148.         con_gets,       'con_gets',\
  149.         con_cls,        'con_cls',\
  150.         con_printf,     'con_printf',\
  151.         con_getch2,     'con_getch2',\
  152.         con_set_cursor_pos, 'con_set_cursor_pos'
  153. i_end:
  154.  
  155. socketnum       dd ?
  156. socketnum2      dd ?
  157. buffer         rb BUFFERSIZE
  158. .length = BUFFERSIZE
  159.  
  160. align   4
  161. rb      4096    ; stack
  162. mem:
  163.