|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nspr.h" |
|
7 #include "prio.h" |
|
8 #include "prerror.h" |
|
9 #include "prlog.h" |
|
10 #include "prprf.h" |
|
11 #include "prnetdb.h" |
|
12 #include "plerror.h" |
|
13 #include "obsolete/probslet.h" |
|
14 |
|
15 #include <stdio.h> |
|
16 #include <string.h> |
|
17 #include <stdlib.h> |
|
18 |
|
19 #define NUMBER_ROUNDS 5 |
|
20 |
|
21 #if defined(WIN16) |
|
22 /* |
|
23 ** Make win16 unit_time interval 300 milliseconds, others get 100 |
|
24 */ |
|
25 #define UNIT_TIME 200 /* unit time in milliseconds */ |
|
26 #elif defined(SYMBIAN) |
|
27 #define UNIT_TIME 5000 /* unit time in milliseconds */ |
|
28 #else |
|
29 #define UNIT_TIME 100 /* unit time in milliseconds */ |
|
30 #endif |
|
31 #define CHUNK_SIZE 10 |
|
32 #undef USE_PR_SELECT /* If defined, we use PR_Select. |
|
33 * If not defined, use PR_Poll instead. */ |
|
34 |
|
35 #if defined(USE_PR_SELECT) |
|
36 #include "pprio.h" |
|
37 #endif |
|
38 |
|
39 static void PR_CALLBACK |
|
40 clientThreadFunc(void *arg) |
|
41 { |
|
42 PRUintn port = (PRUintn)arg; |
|
43 PRFileDesc *sock; |
|
44 PRNetAddr addr; |
|
45 char buf[CHUNK_SIZE]; |
|
46 int i; |
|
47 PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME); |
|
48 PRSocketOptionData optval; |
|
49 PRStatus retVal; |
|
50 PRInt32 nBytes; |
|
51 |
|
52 /* Initialize the buffer so that Purify won't complain */ |
|
53 memset(buf, 0, sizeof(buf)); |
|
54 |
|
55 addr.inet.family = PR_AF_INET; |
|
56 addr.inet.port = PR_htons((PRUint16)port); |
|
57 addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); |
|
58 PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.ip); |
|
59 |
|
60 /* time 1 */ |
|
61 PR_Sleep(unitTime); |
|
62 sock = PR_NewTCPSocket(); |
|
63 optval.option = PR_SockOpt_Nonblocking; |
|
64 optval.value.non_blocking = PR_TRUE; |
|
65 PR_SetSocketOption(sock, &optval); |
|
66 retVal = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT); |
|
67 if (retVal == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) { |
|
68 #if !defined(USE_PR_SELECT) |
|
69 PRPollDesc pd; |
|
70 PRInt32 n; |
|
71 fprintf(stderr, "connect: EWOULDBLOCK, good\n"); |
|
72 pd.fd = sock; |
|
73 pd.in_flags = PR_POLL_WRITE; |
|
74 n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); |
|
75 PR_ASSERT(n == 1); |
|
76 PR_ASSERT(pd.out_flags == PR_POLL_WRITE); |
|
77 #else |
|
78 PR_fd_set writeSet; |
|
79 PRInt32 n; |
|
80 fprintf(stderr, "connect: EWOULDBLOCK, good\n"); |
|
81 PR_FD_ZERO(&writeSet); |
|
82 PR_FD_SET(sock, &writeSet); |
|
83 n = PR_Select(0, NULL, &writeSet, NULL, PR_INTERVAL_NO_TIMEOUT); |
|
84 PR_ASSERT(n == 1); |
|
85 PR_ASSERT(PR_FD_ISSET(sock, &writeSet)); |
|
86 #endif |
|
87 } |
|
88 printf("client connected\n"); |
|
89 fflush(stdout); |
|
90 |
|
91 /* time 4, 7, 11, etc. */ |
|
92 for (i = 0; i < NUMBER_ROUNDS; i++) { |
|
93 PR_Sleep(3 * unitTime); |
|
94 nBytes = PR_Write(sock, buf, sizeof(buf)); |
|
95 if (nBytes == -1) { |
|
96 if (PR_GetError() == PR_WOULD_BLOCK_ERROR) { |
|
97 fprintf(stderr, "write: EWOULDBLOCK\n"); |
|
98 exit(1); |
|
99 } else { |
|
100 fprintf(stderr, "write: failed\n"); |
|
101 } |
|
102 } |
|
103 printf("client sent %d bytes\n", nBytes); |
|
104 fflush(stdout); |
|
105 } |
|
106 |
|
107 PR_Close(sock); |
|
108 } |
|
109 |
|
110 static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv ) |
|
111 { |
|
112 PRFileDesc *listenSock, *sock; |
|
113 PRUint16 listenPort; |
|
114 PRNetAddr addr; |
|
115 char buf[CHUNK_SIZE]; |
|
116 PRThread *clientThread; |
|
117 PRInt32 retVal; |
|
118 PRSocketOptionData optval; |
|
119 PRIntn i; |
|
120 PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME); |
|
121 |
|
122 /* Create a listening socket */ |
|
123 if ((listenSock = PR_NewTCPSocket()) == NULL) { |
|
124 fprintf(stderr, "Can't create a new TCP socket\n"); |
|
125 exit(1); |
|
126 } |
|
127 addr.inet.family = PR_AF_INET; |
|
128 addr.inet.ip = PR_htonl(PR_INADDR_ANY); |
|
129 addr.inet.port = PR_htons(0); |
|
130 if (PR_Bind(listenSock, &addr) == PR_FAILURE) { |
|
131 fprintf(stderr, "Can't bind socket\n"); |
|
132 exit(1); |
|
133 } |
|
134 if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) { |
|
135 fprintf(stderr, "PR_GetSockName failed\n"); |
|
136 exit(1); |
|
137 } |
|
138 listenPort = PR_ntohs(addr.inet.port); |
|
139 if (PR_Listen(listenSock, 5) == PR_FAILURE) { |
|
140 fprintf(stderr, "Can't listen on a socket\n"); |
|
141 exit(1); |
|
142 } |
|
143 |
|
144 PR_snprintf(buf, sizeof(buf), |
|
145 "The server thread is listening on port %hu\n\n", |
|
146 listenPort); |
|
147 printf("%s", buf); |
|
148 |
|
149 clientThread = PR_CreateThread(PR_USER_THREAD, |
|
150 clientThreadFunc, (void *) listenPort, |
|
151 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, |
|
152 PR_UNJOINABLE_THREAD, 0); |
|
153 if (clientThread == NULL) { |
|
154 fprintf(stderr, "can't create thread\n"); |
|
155 exit(1); |
|
156 } |
|
157 |
|
158 printf("client thread created.\n"); |
|
159 |
|
160 optval.option = PR_SockOpt_Nonblocking; |
|
161 optval.value.non_blocking = PR_TRUE; |
|
162 PR_SetSocketOption(listenSock, &optval); |
|
163 /* time 0 */ |
|
164 sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); |
|
165 if (sock != NULL || PR_GetError() != PR_WOULD_BLOCK_ERROR) { |
|
166 PL_PrintError("First Accept\n"); |
|
167 fprintf(stderr, "First PR_Accept() xxx\n" ); |
|
168 exit(1); |
|
169 } |
|
170 printf("accept: EWOULDBLOCK, good\n"); |
|
171 fflush(stdout); |
|
172 /* time 2 */ |
|
173 PR_Sleep(2 * unitTime); |
|
174 sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); |
|
175 if (sock == NULL) { |
|
176 PL_PrintError("Second Accept\n"); |
|
177 fprintf(stderr, "Second PR_Accept() failed: (%d, %d)\n", |
|
178 PR_GetError(), PR_GetOSError()); |
|
179 exit(1); |
|
180 } |
|
181 printf("accept: succeeded, good\n"); |
|
182 fflush(stdout); |
|
183 PR_Close(listenSock); |
|
184 |
|
185 PR_SetSocketOption(sock, &optval); |
|
186 |
|
187 /* time 3, 5, 6, 8, etc. */ |
|
188 for (i = 0; i < NUMBER_ROUNDS; i++) { |
|
189 PR_Sleep(unitTime); |
|
190 retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT); |
|
191 if (retVal != -1 || PR_GetError() != PR_WOULD_BLOCK_ERROR) { |
|
192 PL_PrintError("First Receive:\n"); |
|
193 fprintf(stderr, "First PR_Recv: retVal: %ld, Error: %ld\n", |
|
194 retVal, PR_GetError()); |
|
195 exit(1); |
|
196 } |
|
197 printf("read: EWOULDBLOCK, good\n"); |
|
198 fflush(stdout); |
|
199 PR_Sleep(2 * unitTime); |
|
200 retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT); |
|
201 if (retVal != CHUNK_SIZE) { |
|
202 PL_PrintError("Second Receive:\n"); |
|
203 fprintf(stderr, "Second PR_Recv: retVal: %ld, Error: %ld\n", |
|
204 retVal, PR_GetError()); |
|
205 exit(1); |
|
206 } |
|
207 printf("read: %d bytes, good\n", retVal); |
|
208 fflush(stdout); |
|
209 } |
|
210 PR_Close(sock); |
|
211 |
|
212 printf("All tests finished\n"); |
|
213 printf("PASS\n"); |
|
214 return 0; |
|
215 } |
|
216 |
|
217 int main(int argc, char **argv) |
|
218 { |
|
219 PRIntn rv; |
|
220 |
|
221 PR_STDIO_INIT(); |
|
222 rv = PR_Initialize(RealMain, argc, argv, 0); |
|
223 return rv; |
|
224 } /* main */ |
|
225 |