Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2010-2017. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  udpserv.asm - UDP server demo program for KolibriOS            ;;
  7. ;;                                                                 ;;
  8. ;;  Written by hidnplayr@kolibrios.org                             ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. format binary as ""
  16.  
  17. BUFFERSIZE      = 65536
  18.  
  19. use32
  20. ; standard header
  21.         db      'MENUET01'      ; signature
  22.         dd      1               ; header version
  23.         dd      start           ; entry point
  24.         dd      i_end           ; initialized size
  25.         dd      mem             ; required memory
  26.         dd      mem             ; stack pointer
  27.         dd      0               ; parameters
  28.         dd      0               ; path
  29.  
  30.  
  31. include '../../macros.inc'
  32. purge mov,add,sub
  33. include '../../proc32.inc'
  34. include '../../dll.inc'
  35.  
  36. include '../../network.inc'
  37.  
  38. ; entry point
  39. start:
  40. ; load libraries
  41.         stdcall dll.Load, @IMPORT
  42.         test    eax, eax
  43.         jnz     exit
  44.  
  45. ; initialize console
  46.         invoke  con_start, 1
  47.         invoke  con_init, 80, 25, 80, 25, title
  48.  
  49. ; Set event mask to socket events only
  50.         mcall   40, EVM_STACK
  51.  
  52. ; Write message str1 to the console
  53.         invoke  con_write_asciiz, str1
  54.  
  55. ; Allocate a TCP socket
  56.         mcall   socket, AF_INET4, SOCK_DGRAM, 0
  57.         cmp     eax, -1
  58.         je      sock_err
  59. ; Socket allocation succeeded, store it's number in socketnum
  60.         mov     [socketnum], eax
  61.  
  62. ; This might be needed in the future,
  63. ; SO_REUSEADDR option is not implemented in kernel yet.
  64. ;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
  65. ;        cmp     eax, -1
  66. ;        je      opt_err
  67.  
  68. ; Bind the socket to port 23 (as defined in sockaddr1)
  69.         mcall   bind, [socketnum], sockaddr1, sockaddr1.length
  70.         cmp     eax, -1
  71.         je      bind_err
  72.  
  73. ; Print the received data to the console, untill socket is closed by remote end
  74.   .loop:
  75.         mcall   recv, [socketnum], buffer, BUFFERSIZE, 0
  76.         cmp     eax, -1
  77.         je      .loop
  78.  
  79.  ;;       mov     byte[buffer+eax], 0             ; Zero-terminate the data, so we can print it
  80.   ;;      invoke  con_write_asciiz, buffer
  81.         jmp     .loop
  82.  
  83. ; Print error message
  84. bind_err:
  85.         invoke  con_write_asciiz, str4
  86.         jmp     done
  87.  
  88. sock_err:
  89.         invoke  con_write_asciiz, str6
  90.         jmp     done
  91.  
  92.  
  93. done:
  94. ; Wait for user input
  95.         invoke  con_getch2
  96. ; Close console
  97.         invoke  con_exit, 1
  98. exit:
  99. ; Close listening socket, if it is open
  100.         cmp     [socketnum], 0
  101.         je      @f
  102.         mcall   close, [socketnum]
  103.   @@:
  104.  
  105. ; Close application
  106.         mcall   -1
  107.  
  108.  
  109.  
  110. ; data
  111. title   db      'UDP stream server demo',0
  112. str1    db      'Opening socket',10, 0
  113. str2    db      'Listening for incoming connections...',10,0
  114. str4    db      'Bind error',10,10,0
  115. str6    db      'Could not open socket',10,10,0
  116. str7    db      'Got data',10,10,0
  117.  
  118. sockaddr1:
  119.         dw AF_INET4             ; IPv4
  120. .port   dw 23 shl 8             ; port 23 - network byte order
  121. .ip     dd 0
  122.         rb 10
  123. .length = $ - sockaddr1
  124.  
  125. ; import
  126. align 4
  127. @IMPORT:
  128.  
  129. library console, 'console.obj'
  130.  
  131. import  console,        \
  132.         con_start,      'START',        \
  133.         con_init,       'con_init',     \
  134.         con_write_asciiz,       'con_write_asciiz',     \
  135.         con_exit,       'con_exit',     \
  136.         con_gets,       'con_gets',\
  137.         con_cls,        'con_cls',\
  138.         con_printf,     'con_printf',\
  139.         con_getch2,     'con_getch2',\
  140.         con_set_cursor_pos, 'con_set_cursor_pos'
  141. i_end:
  142.  
  143. socketnum       dd 0
  144. buffer          rb BUFFERSIZE
  145.  
  146. align   4
  147. rb      4096    ; stack
  148. mem:
  149.