Subversion Repositories Kolibri OS

Rev

Rev 3562 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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