Monday, November 08, 2010 at 12:01 AM.


on listenStream (port, flwaitloop = true, refcon = 0, timeout = 0) {
	<<Set up a listener on the indicated port.
		<<If flwaitloop is true, we wait for a connection to be created.
	local (stream);
	local (timedOut);
	netEvents.init (); <<make sure all the tables we need are set up
	loop { // we loop in case NetEvents times-out.
		timedOut = false;
		stream = appleEvent (, 'WAPI', 'LIST', '----', short (port), 'REFC', long (refcon), 'TIME', short (timeout));
		local (adrtable = @user.netEvents.listens.[stream]);
		new (tabletype, adrtable);
		adrtable^.ready = false;
		adrtable^.refcon = refcon;
		if not flwaitloop { // if the caller doesn't want to wait, we immediately return the stream
			return (stream)}
		else { // we've been asked to wait
			local (tc = clock.ticks ());
			while not adrtable^.ready {
				if clock.ticks () > (tc + 1800) { <<30 seconds
					local (status, bytespending);
					status = netEvents.statusStream (stream, @bytespending);
					if status == "INACTIVE" {
						timedOut = true;
						delete (@user.netEvents.listens.[stream]);
					tc = clock.ticks ()};
				sys.systemTask ()};
			if timedOut {
			else {
				return (stream)}}}} <<the caller is now ready to start reading from the stream

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.