Subversion Repositories Kolibri OS

Rev

Rev 3704 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3704 Rev 5859
Line 1... Line 1...
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2010-2013. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2010-2015. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
5
;;                                                                 ;;
6
;;  tcpserv.asm - TCP demo program for KolibriOS                   ;;
6
;;  tcpserv.asm - TCP demo program for KolibriOS                   ;;
7
;;                                                                 ;;
7
;;                                                                 ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
Line 41... Line 41...
41
        stdcall dll.Load, @IMPORT
41
        stdcall dll.Load, @IMPORT
42
        test    eax, eax
42
        test    eax, eax
43
        jnz     exit
43
        jnz     exit
Line 44... Line 44...
44
 
44
 
45
; initialize console
-
 
46
        push    1
45
; initialize console
47
        call    [con_start]
-
 
48
        push    title
-
 
49
        push    25
-
 
50
        push    80
-
 
51
        push    25
-
 
52
        push    80
46
        invoke  con_start, 1
Line 53... Line 47...
53
        call    [con_init]
47
        invoke  con_init, 80, 25, 80, 25, title
Line 54... Line -...
54
 
-
 
55
        mcall   40, 1 shl 7     ; we only want network events
48
 
Line 56... Line 49...
56
 
49
        mcall   40, EVM_STACK
57
        push    str1
50
 
58
        call    [con_write_asciiz]
51
        invoke  con_write_asciiz, str1
59
 
-
 
60
        mcall   socket, AF_INET4, SOCK_STREAM, 0
52
 
Line -... Line 53...
-
 
53
        mcall   socket, AF_INET4, SOCK_STREAM, 0
61
        cmp     eax, -1
54
        cmp     eax, -1
62
        je      sock_err
55
        je      sock_err
63
 
56
        mov     [socketnum], eax
Line 64... Line 57...
64
        mov     [socketnum], eax
57
 
65
 
58
; This socket option is not implemented in kernel yet.
66
;;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
59
;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
Line 67... Line 60...
67
;;        cmp     eax, -1
60
;        cmp     eax, -1
68
;;        je      opt_err
61
;        je      opt_err
69
 
62
 
Line 70... Line -...
70
        mcall   bind, [socketnum], sockaddr1, sockaddr1.length
-
 
71
        cmp     eax, -1
63
        mcall   bind, [socketnum], sockaddr1, sockaddr1.length
72
        je      bind_err
-
 
73
 
-
 
Line 74... Line 64...
74
        mcall   listen, [socketnum], 10 ; Backlog = 10
64
        cmp     eax, -1
75
        cmp     eax, -1
65
        je      bind_err
76
        je      listen_err
66
 
77
 
-
 
78
        push    str2
67
        mcall   listen, [socketnum], 10 ; Backlog = 10
Line 79... Line -...
79
        call    [con_write_asciiz]
-
 
80
 
-
 
81
        mcall   10
68
        cmp     eax, -1
Line 82... Line 69...
82
 
69
        je      listen_err
83
        mcall   accept, [socketnum], sockaddr1, sockaddr1.length
70
 
84
        cmp     eax, -1
71
        invoke  con_write_asciiz, str2
85
        je      acpt_err
72
 
Line 86... Line 73...
86
 
73
        mcall   accept, [socketnum], sockaddr1, sockaddr1.length
87
        mov     [socketnum2], eax
-
 
88
 
-
 
89
;;        mcall   close, [socketnum]
74
        cmp     eax, -1
90
 
-
 
91
        mcall   send, [socketnum2], hello, hello.length
75
        je      acpt_err
Line 92... Line 76...
92
 
76
        mov     [socketnum2], eax
93
  .loop:
-
 
94
        mcall   recv, [socketnum2], buffer, buffer.length, 0
77
 
95
        cmp     eax, -1
78
        mcall   send, [socketnum2], hello, hello.length
Line 96... Line 79...
96
        je      .loop
79
 
97
 
-
 
98
        mov     byte [buffer + eax], 0
80
  .loop:
99
 
81
        mcall   recv, [socketnum2], buffer, buffer.length, 0
Line 100... Line 82...
100
        push    buffer
82
        cmp     eax, -1
101
        call    [con_write_asciiz]
-
 
102
 
83
        je      .loop
103
        jmp     .loop
84
 
Line 104... Line 85...
104
 
85
        mov     byte[buffer+eax], 0
105
acpt_err:
-
 
106
        push    str8
86
        invoke  con_write_asciiz, buffer
107
        call    [con_write_asciiz]
87
        jmp     .loop
Line 108... Line 88...
108
        jmp     done
88
 
109
 
89
acpt_err:
110
listen_err:
-
 
111
        push    str3
90
        invoke  con_write_asciiz, str8
112
        call    [con_write_asciiz]
91
        jmp     done
-
 
92
 
-
 
93
listen_err:
-
 
94
        invoke  con_write_asciiz, str3
-
 
95
        jmp     done
-
 
96
 
-
 
97
bind_err:
-
 
98
        invoke  con_write_asciiz, str4
-
 
99
        jmp     done
113
        jmp     done
100
 
Line 114... Line 101...
114
 
101
sock_err:
115
bind_err:
102
        invoke  con_write_asciiz, str6
116
        push    str4
103
        jmp     done
117
        call    [con_write_asciiz]
104
 
118
        jmp     done
105
done:
119
 
106
        invoke  con_getch2      ; Wait for user input
120
sock_err:
107
        invoke  con_exit, 1
121
        push    str6
108
exit:
122
        call    [con_write_asciiz]
109
        cmp     [socketnum], 0
123
        jmp     done
110
        je      @f
Line 124... Line 111...
124
 
111
        mcall   close, [socketnum]
125
done:
112
  @@:
Line 126... Line 113...
126
        call    [con_getch2]
113
        cmp     [socketnum2], 0
127
        push    1
114
        je      @f
128
        call    [con_exit]
115
        mcall   close, [socketnum2]
129
exit:
116
  @@:
130
        mcall   -1
117
        mcall   -1
131
 
118
 
Line 132... Line 119...
132
 
119
 
Line 168... Line 155...
168
        con_printf,     'con_printf',\
155
        con_printf,     'con_printf',\
169
        con_getch2,     'con_getch2',\
156
        con_getch2,     'con_getch2',\
170
        con_set_cursor_pos, 'con_set_cursor_pos'
157
        con_set_cursor_pos, 'con_set_cursor_pos'
171
i_end:
158
i_end:
Line 172... Line 159...
172
 
159
 
173
socketnum       dd ?
160
socketnum       dd 0
174
socketnum2      dd ?
161
socketnum2      dd 0
175
buffer         rb BUFFERSIZE
162
buffer          rb BUFFERSIZE
Line 176... Line 163...
176
.length = BUFFERSIZE
163
.length = BUFFERSIZE
177
 
164