Преглед изворни кода

Can now also print data sent by the remote side.

tags/v0.9.3
Michael Brown пре 19 година
родитељ
комит
218c26ff2b
1 измењених фајлова са 34 додато и 13 уклоњено
  1. 34
    13
      src/util/prototester.c

+ 34
- 13
src/util/prototester.c Прегледај датотеку

262
 	void ( * closed ) ( struct tcp_connection *conn );
262
 	void ( * closed ) ( struct tcp_connection *conn );
263
 	void ( * connected ) ( struct tcp_connection *conn );
263
 	void ( * connected ) ( struct tcp_connection *conn );
264
 	void ( * acked ) ( struct tcp_connection *conn, size_t len );
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
 	void ( * senddata ) ( struct tcp_connection *conn );
267
 	void ( * senddata ) ( struct tcp_connection *conn );
267
 };
268
 };
268
 
269
 
271
 	struct tcp_operations *tcp_op;
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
 	struct uip_conn *uip_conn;
276
 	struct uip_conn *uip_conn;
276
 	u16_t ipaddr[2];
277
 	u16_t ipaddr[2];
277
 
278
 
289
 	return 0;
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
 		       size_t len ) {
294
 		       size_t len ) {
294
 	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
295
 	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
295
 	uip_send ( ( void * ) data, len );
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
 	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
300
 	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
300
 	uip_close();
301
 	uip_close();
301
 }
302
 }
321
 	if ( uip_acked() )
322
 	if ( uip_acked() )
322
 		op->acked ( conn, uip_conn->len );
323
 		op->acked ( conn, uip_conn->len );
323
 	if ( uip_newdata() )
324
 	if ( uip_newdata() )
324
-		op->newdata ( conn );
325
+		op->newdata ( conn, ( void * ) uip_appdata, uip_len );
325
 	if ( uip_rexmit() || uip_newdata() || uip_acked() ||
326
 	if ( uip_rexmit() || uip_newdata() || uip_acked() ||
326
 	     uip_connected() || uip_poll() )
327
 	     uip_connected() || uip_poll() )
327
 		op->senddata ( conn );
328
 		op->senddata ( conn );
387
 	(type *)( (char *)__mptr - offsetof(type,member) );})
388
 	(type *)( (char *)__mptr - offsetof(type,member) );})
388
 
389
 
389
 enum hello_state {
390
 enum hello_state {
390
-	HELLO_SENDING_MESSAGE = 0,
391
+	HELLO_SENDING_MESSAGE = 1,
391
 	HELLO_SENDING_ENDL,
392
 	HELLO_SENDING_ENDL,
392
 };
393
 };
393
 
394
 
395
 	struct tcp_connection tcp;
396
 	struct tcp_connection tcp;
396
 	const char *message;
397
 	const char *message;
397
 	enum hello_state state;
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
 	int complete;
401
 	int complete;
401
 };
402
 };
402
 
403
 
427
 
428
 
428
 static void hello_connected ( struct tcp_connection *conn ) {
429
 static void hello_connected ( struct tcp_connection *conn ) {
429
 	struct hello_request *hello = tcp_to_hello ( conn );
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
 static void hello_acked ( struct tcp_connection *conn, size_t len ) {
436
 static void hello_acked ( struct tcp_connection *conn, size_t len ) {
434
 
438
 
435
 	hello->message += len;
439
 	hello->message += len;
436
 	hello->remaining -= len;
440
 	hello->remaining -= len;
437
-	if ( hello->remaining <= 0 ) {
441
+	if ( hello->remaining == 0 ) {
438
 		switch ( hello->state ) {
442
 		switch ( hello->state ) {
439
 		case HELLO_SENDING_MESSAGE:
443
 		case HELLO_SENDING_MESSAGE:
440
 			hello->state = HELLO_SENDING_ENDL;
444
 			hello->state = HELLO_SENDING_ENDL;
442
 			hello->remaining = 2;
446
 			hello->remaining = 2;
443
 			break;
447
 			break;
444
 		case HELLO_SENDING_ENDL:
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
 			break;
452
 			break;
447
 		default:
453
 		default:
448
 			assert ( 0 );
454
 			assert ( 0 );
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
 	struct hello_request *hello = tcp_to_hello ( conn );
461
 	struct hello_request *hello = tcp_to_hello ( conn );
462
+
463
+	hello->callback ( data, len );
455
 }
464
 }
456
 
465
 
457
 static void hello_senddata ( struct tcp_connection *conn ) {
466
 static void hello_senddata ( struct tcp_connection *conn ) {
557
 	return optind;
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
 static int test_hello ( int argc, char **argv ) {
585
 static int test_hello ( int argc, char **argv ) {

Loading…
Откажи
Сачувај