diff --git a/Testing/tcpport-client.m b/Testing/tcpport-client.m index 389244d39..9d9fc4c9e 100644 --- a/Testing/tcpport-client.m +++ b/Testing/tcpport-client.m @@ -1,5 +1,18 @@ #include #include +#include +#include +#include + +id handle_incoming_packet (id packet) +{ + fprintf (stdout, "received >"); + fwrite ([packet streamBuffer] + [packet streamBufferPrefix], + [packet streamEofPosition], 1, stdout); + fprintf (stdout, "<\n"); + [packet release]; + return nil; +} int main (int argc, char *argv[]) { @@ -17,6 +30,13 @@ int main (int argc, char *argv[]) onHost: @"localhost"]; in_port = [TcpInPort newForReceiving]; + + [in_port setPacketInvocation: + [[[ObjectFunctionInvocation alloc] + initWithObjectFunction: handle_incoming_packet] + autorelease]]; + + [in_port addToRunLoop: [RunLoop currentInstance] forMode: nil]; for (i = 0; i < 10; i++) { @@ -26,17 +46,7 @@ int main (int argc, char *argv[]) [out_port sendPacket: packet withTimeout: 20 * 1000]; [packet release]; - packet = [in_port receivePacketWithTimeout: 1000]; - if (packet) - { - fprintf (stdout, "received >"); - fwrite ([packet streamBuffer] + [packet streamBufferPrefix], - [packet streamEofPosition], 1, stdout); - fprintf (stdout, "<\n"); - [packet release]; - } - - sleep (2); + [RunLoop runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]]; } [out_port close]; diff --git a/Testing/tcpport-server.m b/Testing/tcpport-server.m index 574fb6aec..722b52421 100644 --- a/Testing/tcpport-server.m +++ b/Testing/tcpport-server.m @@ -2,6 +2,7 @@ #include #include #include +#include id announce_new_connection (id notification) { @@ -25,13 +26,32 @@ id announce_broken_connection (id notification) return nil; } -int main (int argc, char *argv[]) +static id port = nil; + +id handle_incoming_packet (id packet) { - id port; - id packet; - unsigned message_count = 0; + static unsigned message_count = 0; id reply_port; + message_count++; + fprintf (stdout, "received >"); + fwrite ([packet streamBuffer] + [packet streamBufferPrefix], + [packet streamEofPosition], 1, stdout); + fprintf (stdout, "<\n"); + reply_port = [packet replyPort]; + [packet release]; + + packet = [[TcpPacket alloc] initForSendingWithCapacity: 100 + replyPort: port]; + [packet writeFormat: @"Your's was my message number %d", + message_count]; + [reply_port sendPacket: packet withTimeout: 20 * 1000]; + [packet release]; + return nil; +} + +int main (int argc, char *argv[]) +{ if (argc > 1) port = [TcpInPort newForReceivingFromRegisteredName: [NSString stringWithCString: argv[1]]]; @@ -50,23 +70,39 @@ int main (int argc, char *argv[]) object: port]; printf ("Waiting for connections.\n"); - while ((packet = [port receivePacketWithTimeout: -1])) - { - message_count++; - fprintf (stdout, "received >"); - fwrite ([packet streamBuffer] + [packet streamBufferPrefix], - [packet streamEofPosition], 1, stdout); - fprintf (stdout, "<\n"); - reply_port = [packet replyPort]; - [packet release]; - packet = [[TcpPacket alloc] initForSendingWithCapacity: 100 - replyPort: port]; - [packet writeFormat: @"Your's was my message number %d", - message_count]; - [reply_port sendPacket: packet withTimeout: 20 * 1000]; - [packet release]; - } +#if 1 + [port setPacketInvocation: + [[[ObjectFunctionInvocation alloc] + initWithObjectFunction: handle_incoming_packet] + autorelease]]; + [port addToRunLoop: [RunLoop currentInstance] forMode: nil]; + [[RunLoop currentInstance] run]; +#else + { + id packet; + unsigned message_count = 0; + id reply_port; + + while ((packet = [port receivePacketWithTimeout: -1])) + { + message_count++; + fprintf (stdout, "received >"); + fwrite ([packet streamBuffer] + [packet streamBufferPrefix], + [packet streamEofPosition], 1, stdout); + fprintf (stdout, "<\n"); + reply_port = [packet replyPort]; + [packet release]; + + packet = [[TcpPacket alloc] initForSendingWithCapacity: 100 + replyPort: port]; + [packet writeFormat: @"Your's was my message number %d", + message_count]; + [reply_port sendPacket: packet withTimeout: 20 * 1000]; + [packet release]; + } + } +#endif fprintf (stdout, "Timed out. Exiting.\n"); exit (0);