Exemple #1
0
int CUDTUnited::startup(const char* ctlip, const uint16_t ctlport, const char* stunip, const uint16_t stunport, const char* name, const char* passwd, p2p_init_h* inith, p2p_request_h* requesth)
{
	CGuard gcinit(m_InitLock);

	if (m_iInstanceCount++ > 0)
		return 0;

	if (m_bGCStatus)
		return 0;

	p2p_init(&m_p2ph, ctlip, ctlport, stunip, stunport, name, passwd, p2p_init_handler, p2p_request_handler);

	m_bClosing = false;
#ifndef WIN32
	pthread_mutex_init(&m_GCStopLock, NULL);
	pthread_cond_init(&m_GCStopCond, NULL);
	pthread_create(&m_WorkerThread, NULL, worker, this);
#else
	m_GCStopLock = CreateMutex(NULL, false, NULL);
	m_GCStopCond = CreateEvent(NULL, false, false, NULL);
	m_WorkerThread = CreateThread(NULL, 0, worker, this, 0, NULL);
#endif

	//m_p2ph = p;
	m_bGCStatus = true;

	return 0;
}
Exemple #2
0
int main(void)
{
	gcinit();
	stackinit();

	while(1) {
		char buffer[80];
		ssize_t bytes_read = raw_input("calc> ", buffer, sizeof(buffer));
		if(bytes_read == EOF) {
			return 0;
		} else {	
			
			Buffer p;
			buffer_init(&p, buffer, bytes_read);
			size_t i = 0;
			while(p.pos != p.end) {
				if(IS_DIGIT(*p.pos)) {
					int num = *p.pos - '0';
					stackpush(newLong(num));
					printf("PUSH %d\n", num);
				} else if(IS_OPERATOR(*p.pos)) {
					if(pstack.stacksize < 2) {
						fprintf(stderr,
							"operator '%c' takes 2 args\n", *p.pos);
						goto finally;

					} else {
						Object* op2 = stackpop();
						Object* op1 = stackpop();
							
						if(*p.pos == '+') {
							printf("POP %ld\n", O_LVAL(op2));
							printf("POP %ld\n", O_LVAL(op1));
							long result;
							result = O_LVAL(op1) + O_LVAL(op2);

							Object *retval = newLong(result);
							printf("ADD\n");
							stackpush(retval);
							printf("PUSH %ld\n", result);
						} else if(*p.pos == '-') {
							long result;
							result = O_LVAL(op1) - O_LVAL(op2);

							Object *retval = newLong(result);
							stackpush(retval);	
						}

					}	
					
				} 
				else if (IS_WHITE(*p.pos)) 
				{
					goto out;
				}
				else 
				{
					fprintf(stderr, "Invalid token %c\n", *p.pos);
					goto finally;
				}

				out:
					p.pos++;
					i++;
			}

			if(pstack.stacksize == 1) {
				Object *top = stackpop();
				objectEcho(top);
			} else {
				printf("To many values: %zu", pstack.stacksize);
			}
			

			finally:
				gcterm();
				gcinit();
				stackinit();
				printf("\n");
		}
	}

	return 0;
}