Pārlūkot izejas kodu

Can now also print data sent by the remote side.

tags/v0.9.3
Michael Brown 19 gadus atpakaļ
vecāks
revīzija
218c26ff2b
1 mainītis faili ar 34 papildinājumiem un 13 dzēšanām
  1. 34
    13
      src/util/prototester.c

+ 34
- 13
src/util/prototester.c Parādīt failu

@@ -262,7 +262,8 @@ struct tcp_operations {
262 262
 	void ( * closed ) ( struct tcp_connection *conn );
263 263
 	void ( * connected ) ( struct tcp_connection *conn );
264 264
 	void ( * acked ) ( struct tcp_connection *conn, size_t len );
265
-	void ( * newdata ) ( struct tcp_connection *conn );
265
+	void ( * newdata ) ( struct tcp_connection *conn,
266
+			     void *data, size_t len );
266 267
 	void ( * senddata ) ( struct tcp_connection *conn );
267 268
 };
268 269
 
@@ -271,7 +272,7 @@ struct tcp_connection {
271 272
 	struct tcp_operations *tcp_op;
272 273
 };
273 274
 
274
-static int tcp_connect ( struct tcp_connection *conn ) {
275
+int tcp_connect ( struct tcp_connection *conn ) {
275 276
 	struct uip_conn *uip_conn;
276 277
 	u16_t ipaddr[2];
277 278
 
@@ -289,13 +290,13 @@ static int tcp_connect ( struct tcp_connection *conn ) {
289 290
 	return 0;
290 291
 }
291 292
 
292
-static void tcp_send ( struct tcp_connection *conn, const void *data,
293
+void tcp_send ( struct tcp_connection *conn, const void *data,
293 294
 		       size_t len ) {
294 295
 	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
295 296
 	uip_send ( ( void * ) data, len );
296 297
 }
297 298
 
298
-static void tcp_close ( struct tcp_connection *conn ) {
299
+void tcp_close ( struct tcp_connection *conn ) {
299 300
 	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
300 301
 	uip_close();
301 302
 }
@@ -321,7 +322,7 @@ void uip_tcp_appcall ( void ) {
321 322
 	if ( uip_acked() )
322 323
 		op->acked ( conn, uip_conn->len );
323 324
 	if ( uip_newdata() )
324
-		op->newdata ( conn );
325
+		op->newdata ( conn, ( void * ) uip_appdata, uip_len );
325 326
 	if ( uip_rexmit() || uip_newdata() || uip_acked() ||
326 327
 	     uip_connected() || uip_poll() )
327 328
 		op->senddata ( conn );
@@ -387,7 +388,7 @@ static void run_tcpip ( void ) {
387 388
 	(type *)( (char *)__mptr - offsetof(type,member) );})
388 389
 
389 390
 enum hello_state {
390
-	HELLO_SENDING_MESSAGE = 0,
391
+	HELLO_SENDING_MESSAGE = 1,
391 392
 	HELLO_SENDING_ENDL,
392 393
 };
393 394
 
@@ -395,8 +396,8 @@ struct hello_request {
395 396
 	struct tcp_connection tcp;
396 397
 	const char *message;
397 398
 	enum hello_state state;
398
-	int remaining;
399
-	void ( *callback ) ( struct hello_request *hello );
399
+	size_t remaining;
400
+	void ( *callback ) ( char *data, size_t len );
400 401
 	int complete;
401 402
 };
402 403
 
@@ -427,6 +428,9 @@ static void hello_closed ( struct tcp_connection *conn ) {
427 428
 
428 429
 static void hello_connected ( struct tcp_connection *conn ) {
429 430
 	struct hello_request *hello = tcp_to_hello ( conn );
431
+
432
+	printf ( "Connection established\n" );
433
+	hello->state = HELLO_SENDING_MESSAGE;
430 434
 }
431 435
 
432 436
 static void hello_acked ( struct tcp_connection *conn, size_t len ) {
@@ -434,7 +438,7 @@ static void hello_acked ( struct tcp_connection *conn, size_t len ) {
434 438
 
435 439
 	hello->message += len;
436 440
 	hello->remaining -= len;
437
-	if ( hello->remaining <= 0 ) {
441
+	if ( hello->remaining == 0 ) {
438 442
 		switch ( hello->state ) {
439 443
 		case HELLO_SENDING_MESSAGE:
440 444
 			hello->state = HELLO_SENDING_ENDL;
@@ -442,7 +446,9 @@ static void hello_acked ( struct tcp_connection *conn, size_t len ) {
442 446
 			hello->remaining = 2;
443 447
 			break;
444 448
 		case HELLO_SENDING_ENDL:
445
-			tcp_close ( conn );
449
+			/* Nothing to do once we've finished sending
450
+			 * the end-of-line indicator.
451
+			 */
446 452
 			break;
447 453
 		default:
448 454
 			assert ( 0 );
@@ -450,8 +456,11 @@ static void hello_acked ( struct tcp_connection *conn, size_t len ) {
450 456
 	}
451 457
 }
452 458
 
453
-static void hello_newdata ( struct tcp_connection *conn ) {
459
+static void hello_newdata ( struct tcp_connection *conn, void *data,
460
+			    size_t len ) {
454 461
 	struct hello_request *hello = tcp_to_hello ( conn );
462
+
463
+	hello->callback ( data, len );
455 464
 }
456 465
 
457 466
 static void hello_senddata ( struct tcp_connection *conn ) {
@@ -557,8 +566,20 @@ static int hello_parse_options ( int argc, char **argv,
557 566
 	return optind;
558 567
 }
559 568
 
560
-static void test_hello_callback ( struct hello_request *hello ) {
561
-	
569
+static void test_hello_callback ( char *data, size_t len ) {
570
+	int i;
571
+	char c;
572
+
573
+	for ( i = 0 ; i < len ; i++ ) {
574
+		c = data[i];
575
+		if ( c == '\r' ) {
576
+			/* Print nothing */
577
+		} else if ( ( c == '\n' ) || ( c >= 32 ) || ( c <= 126 ) ) {
578
+			putchar ( c );
579
+		} else {
580
+			putchar ( '.' );
581
+		}
582
+	}	
562 583
 }
563 584
 
564 585
 static int test_hello ( int argc, char **argv ) {

Notiek ielāde…
Atcelt
Saglabāt