Updated for new RunLoop.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1115 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Andrew McCallum 1996-03-12 19:20:07 +00:00
parent 3417862743
commit 5f0b7b4d69
2 changed files with 77 additions and 31 deletions

View file

@ -1,5 +1,18 @@
#include <stdio.h>
#include <objects/TcpPort.h>
#include <objects/RunLoop.h>
#include <objects/Invocation.h>
#include <Foundation/NSDate.h>
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];

View file

@ -2,6 +2,7 @@
#include <objects/TcpPort.h>
#include <objects/Notification.h>
#include <objects/Invocation.h>
#include <objects/RunLoop.h>
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);