Пример #1
0
void *otudp_new(Symbol *s, short argc, Atom *argv) {
	OTUDP *x;
	OSStatus err;
	InetHost host;
	
	// These variables will be filled with the args we parse
	int writer;
	char *inetHostName = 0;
	InetPort port = 0;
	InetPort receivePort = 0;
	long nbufs = DEFAULT_NUM_BUFFERS;
	long bufsize = DEFAULT_BUFFER_SIZE;

		
	if (ParseArgs(argc, argv, &writer, &inetHostName, &port, &receivePort, &nbufs, &bufsize) == 0) {
		error("¥ OTUDP usage: \"otudp read <port> [bufsize <nbytes>] [nbufs <n>]\" or "
				   "\"otudp write <hostname> <port> [bufsize <nbytes>] [nbufs <n>]\"");
		return 0;
	}
	
	if (writer) {
		if (LookUpInetHost(inetHostName, &host) == 0) {
			error("otudp: Couldn't make sense of Internet host \"%s\"", inetHostName);
			return 0;
		}
	}
	
	x = newobject(otudp_class);
/*	x->o_a5 = (long) LMGetCurrentA5(); */
	x->o_writer = writer;
	x->o_inetHostName = inetHostName;
	x->o_inetPort = port;
	x->o_inetHost = host;
	x->o_receiveInetPort = receivePort;
	x->o_desiredNewHostName = 0;
	x->o_desiredNewPort = 0;

	x->o_ready = 0;
	x->o_errorreporting = 1;
	x->o_ever_dropped_a_packet = 0;
	x->o_num_dropped_packets = 0;
	x->o_outlet = outlet_new(x,0L);
	x->o_clock = clock_new(x, (method) otudp_output);
	InitPBFIFO(&(x->pendingBuffers));
	
	x->o_complained_about_old_msgs = 0;
	
	OTClearLock(&(x->o_readlock));
	x->o_semaphoreTest = x->o_semaphoreTestFailureCount = 0;
	
	/* Allocate packet buffers */
	x->bufsize = bufsize;
	x->nbufs = nbufs;
	if (nbufs == 0) {
		x->allBuffers = x->freeBuffers = 0; 
	} else {
		x->allBuffers = InitPackets(bufsize, nbufs);
		if (x->allBuffers == 0) {
			error("¥ OTUDP: Out of memory (Couldn't allocate buffers to store incoming packets)");
			return 0;
		}

		x->freeBuffers = x->allBuffers;
	}
	
	x->o_udp_ep = 0; 	// Indicates endpoint hasn't been created yet.
	err = OTAsyncOpenEndpointInContext(OTCreateConfiguration(kUDPName), 0, &(x->epinfo), 
									   NewOTNotifyUPP(OTUDPNotifier), x, OTContext);
	if (err != noErr) {
		error("¥ otudp: Error %d from OTAsyncOpenEndpoint. This is bad.", err);
		return 0;
	}
	// This didn't actually make the endpoint; my notifier should get the T_OPENCOMPLETE
	// event after the endpoint is opened.
	
	/* Create a Qelem that will let us change the host at non-interrupt, non-defered-task-time level */
	x->UnbindQelem = qelem_new(x, (method)unbind_from_qelem);

	return (x);
}
static OSStatus OpenTest(void)
{
	OSStatus err;
	OSStatus junk;
	EndpointRef ep1;
	EndpointRef ep2;
	MapperRef map1;
	MapperRef map2;
	ATSvcRef at1;
	ATSvcRef at2;
	InetSvcRef inet1;
	InetSvcRef inet2;
	ProviderRef prov1;
	ProviderRef prov2;

	ep1   = NULL;
	ep2   = NULL;
	map1  = NULL;
	map2  = NULL;
	at1   = NULL;
	at2   = NULL;
	inet1 = NULL;
	inet2 = NULL;
	prov1 = NULL;
	prov2 = NULL;
	
	ep1 = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, NULL, &err, NULL);
	if (err == noErr) {
		err = OTAsyncOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, NULL, DummyNotifier, &ep2, NULL);
		if (err == noErr) {
			while (ep2 == NULL) {
				SystemTask();
			}
			if (ep2 == (void *) -1) {
				err = -1;
			}
		}
	}

	if (err == noErr) {
		map1 = OTOpenMapperInContext(OTCreateConfiguration("nbp"), 0, &err, NULL);
	}
	if (err == noErr) {
		err = OTAsyncOpenMapperInContext(OTCreateConfiguration("nbp"), 0, DummyNotifier, &map2, NULL);
		if (err == noErr) {
			while (map2 == NULL) {
				SystemTask();
			}
			if (map2 == (void *) -1) {
				err = -1;
			}
		}
	}

	if (err == noErr) {
		inet1 = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, &err, NULL);
	}
	if (err == noErr) {
		err = OTAsyncOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, DummyNotifier, &inet2, NULL);
		if (err == noErr) {
			while (inet2 == NULL) {
				SystemTask();
			}
			if (inet2 == (void *) -1) {
				err = -1;
			}
		}
	}

	if (err == noErr) {
		at1 = OTOpenAppleTalkServicesInContext(kDefaultAppleTalkServicesPath, 0, &err, NULL);
	}
	if (err == noErr) {
		err = OTAsyncOpenAppleTalkServicesInContext(kDefaultAppleTalkServicesPath, 0, DummyNotifier, &at2, NULL);
		if (err == noErr) {
			while (at2 == NULL) {
				SystemTask();
			}
			if (at2 == (void *) -1) {
				err = -1;
			}
		}
	}


	if (err == noErr) {
		prov1 = OTOpenProviderInContext(OTCreateConfiguration("enet"), 0, &err, NULL);
	}
	if (err == noErr) {
		err = OTAsyncOpenProviderInContext(OTCreateConfiguration("enet"), 0, DummyNotifier, &prov2, NULL);
		if (err == noErr) {
			while (prov2 == NULL) {
				SystemTask();
			}
			if (prov2 == (void *) -1) {
				err = -1;
			}
		}
	}

	if (ep1 != NULL) {
		junk = OTCloseProvider(ep1);
		assert(junk == noErr);
	}
	if (ep2 != NULL) {
		junk = OTCloseProvider(ep2);
		assert(junk == noErr);
	}
	if (map1 != NULL) {
		junk = OTCloseProvider(map1);
		assert(junk == noErr);
	}
	if (map2 != NULL) {
		junk = OTCloseProvider(map2);
		assert(junk == noErr);
	}
	if (at1 != NULL) {
		junk = OTCloseProvider(at1);
		assert(junk == noErr);
	}
	if (at2 != NULL) {
		junk = OTCloseProvider(at2);
		assert(junk == noErr);
	}
	if (inet1 != NULL) {
		junk = OTCloseProvider(inet1);
		assert(junk == noErr);
	}
	if (inet2 != NULL) {
		junk = OTCloseProvider(inet2);
		assert(junk == noErr);
	}
	if (prov1 != NULL) {
		junk = OTCloseProvider(prov1);
		assert(junk == noErr);
	}
	if (prov2 != NULL) {
		junk = OTCloseProvider(prov2);
		assert(junk == noErr);
	}
	return err;
}