mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
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:
parent
e8d8fc9588
commit
774c3ef6dd
2 changed files with 77 additions and 31 deletions
|
@ -1,5 +1,18 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <objects/TcpPort.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[])
|
int main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
@ -17,6 +30,13 @@ int main (int argc, char *argv[])
|
||||||
onHost: @"localhost"];
|
onHost: @"localhost"];
|
||||||
|
|
||||||
in_port = [TcpInPort newForReceiving];
|
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++)
|
for (i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
|
@ -26,17 +46,7 @@ int main (int argc, char *argv[])
|
||||||
[out_port sendPacket: packet withTimeout: 20 * 1000];
|
[out_port sendPacket: packet withTimeout: 20 * 1000];
|
||||||
[packet release];
|
[packet release];
|
||||||
|
|
||||||
packet = [in_port receivePacketWithTimeout: 1000];
|
[RunLoop runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]];
|
||||||
if (packet)
|
|
||||||
{
|
|
||||||
fprintf (stdout, "received >");
|
|
||||||
fwrite ([packet streamBuffer] + [packet streamBufferPrefix],
|
|
||||||
[packet streamEofPosition], 1, stdout);
|
|
||||||
fprintf (stdout, "<\n");
|
|
||||||
[packet release];
|
|
||||||
}
|
|
||||||
|
|
||||||
sleep (2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[out_port close];
|
[out_port close];
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <objects/TcpPort.h>
|
#include <objects/TcpPort.h>
|
||||||
#include <objects/Notification.h>
|
#include <objects/Notification.h>
|
||||||
#include <objects/Invocation.h>
|
#include <objects/Invocation.h>
|
||||||
|
#include <objects/RunLoop.h>
|
||||||
|
|
||||||
id announce_new_connection (id notification)
|
id announce_new_connection (id notification)
|
||||||
{
|
{
|
||||||
|
@ -25,13 +26,32 @@ id announce_broken_connection (id notification)
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (int argc, char *argv[])
|
static id port = nil;
|
||||||
|
|
||||||
|
id handle_incoming_packet (id packet)
|
||||||
{
|
{
|
||||||
id port;
|
static unsigned message_count = 0;
|
||||||
id packet;
|
|
||||||
unsigned message_count = 0;
|
|
||||||
id reply_port;
|
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)
|
if (argc > 1)
|
||||||
port = [TcpInPort newForReceivingFromRegisteredName:
|
port = [TcpInPort newForReceivingFromRegisteredName:
|
||||||
[NSString stringWithCString: argv[1]]];
|
[NSString stringWithCString: argv[1]]];
|
||||||
|
@ -50,23 +70,39 @@ int main (int argc, char *argv[])
|
||||||
object: port];
|
object: port];
|
||||||
|
|
||||||
printf ("Waiting for connections.\n");
|
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
|
#if 1
|
||||||
replyPort: port];
|
[port setPacketInvocation:
|
||||||
[packet writeFormat: @"Your's was my message number %d",
|
[[[ObjectFunctionInvocation alloc]
|
||||||
message_count];
|
initWithObjectFunction: handle_incoming_packet]
|
||||||
[reply_port sendPacket: packet withTimeout: 20 * 1000];
|
autorelease]];
|
||||||
[packet release];
|
[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");
|
fprintf (stdout, "Timed out. Exiting.\n");
|
||||||
|
|
||||||
exit (0);
|
exit (0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue