Exemple #1
0
EStatus StationSendPacket(Station* pThis, Packet* pPacket)
{
    PacketEntry* pEntry;

	VALIDATE_ARGUMENTS(pThis && pPacket);
	if (pPacket->header.type != ePKT_TYPE_ACK) pPacket->header.sequenceNum = pThis->sequenceNum++;
	StationNewPacketEntry(pThis, pPacket, &pEntry);
	if (!pEntry)
	{
	    PacketDelete(&pPacket);
	    return eSTATUS_COMMON_OK;
	}

    if (pThis->outboxHandler.callback)
    {
        pThis->outboxHandler.callback(pThis,
                                      pEntry->pPacket,
                                      ePKT_EVENT_ADDED,
                                      pEntry->retriesCount,
                                      pEntry->nextRetryTime,
                                      pThis->outboxHandler.pArg);
    }

	if (StationIsPacketPrioritized(pThis, pPacket)) CHECK(ListPushFront(pThis->pOutbox, pEntry));
	else CHECK(ListPushBack(pThis->pOutbox, pEntry));

	// Don't check the result: it can fail if silentTime is in the past - just ignore this case
	// TimeLineEvent(pThis->pTimeLine, pThis->silentTime, pPacket);

	return eSTATUS_COMMON_OK;
}
Exemple #2
0
int main(int argc, char *args[])
{
    char *buffer = (char *)malloc(MAX_LINE_LEN);;
    char *result = (char *)malloc(MAX_LINE_LEN);;
    int number;
    int dwnum;

    dwnum = GetPrice();
    if(dwnum != 0)
    {
	return dwnum;
    }
    list_t *L = ListInit();
    if(L == NULL)
    {
	return ERRNO_NULL_POINTER;
    }

    while(fgets(buffer, MAX_LINE_LEN, stdin))
    {
	number = 0;
	memset(result, 0, MAX_LINE_LEN);
	dwnum = ParseLine(buffer, result, &number);

	if(dwnum == 0)
	{
	    item_t* curnode;
	    if((curnode = ListIsNodeInListById(L, result)) != NULL)
	    {
		curnode->count += number;
		continue;
	    }

	    item_t * node = ItemMakeitem(result, number);
	    if(node == NULL)
	    {
		return ERRNO_MAKEITEM_FAIL;
	    }

	    dwnum = ListPushBack(L, (void*) node);
	    if(dwnum != 0)
	    {
		return ERRNO_LISTPUSHBACK_FAIL;
	    }
	}
    }

    dwnum = CalculateSum(L);
    if(dwnum != 0)
    {
	return ERRNO_CALCULATESUM_FAIL;	
    }

    ListFree(L);
    ListFree(my_price);
    free(buffer);
    free(result);
    return 0;
}
node * CreateList( T xs ){
    node * head = nullptr;

    for( auto x: xs ){
        ListPushBack( &head, x );
    }

    return head;
}
Exemple #4
0
// 蛇生长
int SnakeGorwup()
{
	// 给新的节点分配内存
	PGAME_COORD pNewTail;
	PGAME_COORD pTail;		// 倒数第一节点
	PGAME_COORD pLastButOne;	// 倒数第二节点
	int size = GetSnakeSize();

	if (size >= boundary.x*boundary.y-1) //长到最长了,游戏结束!~
		return SNAKE_COMPLETE;

	if (size == 0) // 没有头,不知从何生长,返回错误。
		return SNAKE_ERROR;

	pNewTail = (PGAME_COORD)malloc(sizeof(GAME_COORD));
	if (size == 1) // 只有一个节点,按照当前的移动方向生长。
	{
		
		pTail = (PGAME_COORD)GetSnakeTail();
		switch (snake_dir)
		{
		case SNAKE_LEFT:
			pNewTail->x = pTail->x + 1;
			pNewTail->y = pTail->y;
			break;
		case SNAKE_RIGHT:
			pNewTail->x = pTail->x - 1;
			pNewTail->y = pTail->y;
			break;
		case SNAKE_UP:
			pNewTail->x = pTail->x ;
			pNewTail->y = pTail->y + 1;
			break;
		case SNAKE_DOWN:
			pNewTail->x = pTail->x;
			pNewTail->y = pTail->y - 1;
			break;
		}

	}
	else // 有两个以上节点,取倒数第一和倒数第二计算生长方向。
	{
		pTail = (PGAME_COORD)GetSnakeTail();
		pLastButOne = (PGAME_COORD)GetSnakeAt(size - 2);
		// 沿着倒数第二->倒数第一的方向,添加一个新的节点。
		pNewTail->x = pTail->x + (pTail->x - pLastButOne->x);
		pNewTail->y = pTail->y + (pTail->y - pLastButOne->y);

	}

	// 新节点放入到蛇尾的位置
	ListPushBack(snake_list, pNewTail);
	return SNAKE_GROWUP;

}
Exemple #5
0
list_node* ListInsert(linked_list* list, const void* data, list_node* prev_node) {
    list_node* new_node;

    if(prev_node == list->tail) return ListPushBack(list, data);

	new_node = (list_node*)malloc(sizeof(list_node));
	new_node->next = prev_node->next;
	new_node->next->prev = new_node;
	prev_node->next = new_node;
	new_node->prev  = prev_node;
    new_node->data = (void*)data;
    list->count++;
    return new_node;
}
Exemple #6
0
    ExpressionSptr Parser::ReadList()
    {
        assert(_lexer);

        auto list = Expression::List();

        for (;;)
        {
            if (!_lexer->Next())
            {
                auto lx = _lexer->GetLexeme();
                if (lx)
                {
                    AddLexerError(lx);
                    return ExpressionSptr();
                }

                AddParserError(lx, ParserErrorCode::UnexpectedEof);
                return ExpressionSptr();
            }

            auto lx = _lexer->GetLexeme();

            if (lx->Type == LexemeType::RightBrace)
            {
                return list;
            }

            auto expr = ReadExpression();
            if (!expr)
            {
                return ExpressionSptr();
            }

            list->ListPushBack(expr);
        }
    }
Exemple #7
0
void SceneAddObject(SceneT *self, SceneObjectT *object) {
    ListPushBack(self->objects, object);
}
Exemple #8
0
void ManipulateNumerics()
{
    /* We should initialize the container before any operations. */
    List* list = ListInit();

    /* Push the integer elements. */
    ListPushFront(list, (void*)(intptr_t)20);
    ListPushBack(list, (void*)(intptr_t)40);

    /* Insert the elements with the specified indexes. */
    ListInsert(list, 0, (void*)(intptr_t)10);
    ListInsert(list, 3, (void*)(intptr_t)50);
    ListInsert(list, 2, (void*)(intptr_t)30);

    /*---------------------------------------------------------------*
     * Now the list should be: (10)<-->(20)<-->(30)<-->(40)<-->(50)  *
     *---------------------------------------------------------------*/

    /* Iterate through the list. */
    void* element;
    int num = 10;
    ListFirst(list, false);
    while (ListNext(list, &element)) {
        assert((int)(intptr_t)element == num);
        num += 10;
    }

    /* Iterate through the list in the reversed order. */
    num = 50;
    ListFirst(list, true);
    while (ListReverseNext(list, &element)) {
        assert((int)(intptr_t)element == num);
        num -= 10;
    }

    /* Get the element from the list head. */
    ListGetFront(list, &element);
    assert((int)(intptr_t)element == 10);

    /* Get the element from the list tail. */
    ListGetBack(list, &element);
    assert((int)(intptr_t)element == 50);

    /* Get the elements from the specified indexes. */
    ListGetAt(list, 2, &element);
    assert((int)(intptr_t)element == 30);
    ListGetAt(list, 3, &element);
    assert((int)(intptr_t)element == 40);

    /* Replace the element residing at the list head. */
    ListSetFront(list, (void*)(intptr_t)-1);

    /* Replace the element residing at the list tail. */
    ListSetBack(list, (void*)(intptr_t)-5);

    /* Replace the elements residing at the specified indexes. */
    ListSetAt(list, 1, (void*)(intptr_t)-2);
    ListSetAt(list, 2, (void*)(intptr_t)-3);
    ListSetAt(list, 3, (void*)(intptr_t)-4);

    /* Reverse the list. */
    ListReverse(list);

    /*---------------------------------------------------------------*
     * Now the list should be: (-5)<-->(-4)<-->(-3)<-->(-2)<-->(-1)  *
     *---------------------------------------------------------------*/

    /* Remove the element from the list head. */
    ListPopFront(list);

    /* Remove the element from the list tail. */
    ListPopBack(list);

    /* Remove the elements from the specified indexes. */
    ListRemove(list, 1);
    ListRemove(list, 1);

    /* Get the list size. And the remaining element should be (-4). */
    unsigned size = ListSize(list);
    assert(size == 1);

    ListGetFront(list, &element);
    assert((int)(intptr_t)element == -4);

    ListDeinit(list);
}
Exemple #9
0
void ManipulateObjects()
{
    /* We should initialize the container before any operations. */
    List* list = ListInit();
    ListSetClean(list, CleanObject);

    /* Push the object elements. */
    Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 20;
    tuple->second = -20;
    ListPushFront(list, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 40;
    tuple->second = -40;
    ListPushBack(list, tuple);

    /* Insert the elements with the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 10;
    tuple->second = -10;
    ListInsert(list, 0, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 50;
    tuple->second = -50;
    ListInsert(list, 3, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 30;
    tuple->second = -30;
    ListInsert(list, 2, tuple);

    /*---------------------------------------------------------------*
     * Now the list should be: (10)<-->(20)<-->(30)<-->(40)<-->(50)  *
     *---------------------------------------------------------------*/

    /* Iterate through the list. */
    void* element;
    int num = 10;
    ListFirst(list, false);
    while (ListNext(list, &element)) {
        assert(((Tuple*)element)->first == num);
        num += 10;
    }

    /* Iterate through the list in the reversed order. */
    num = 50;
    ListFirst(list, true);
    while (ListReverseNext(list, &element)) {
        assert(((Tuple*)element)->first == num);
        num -= 10;
    }

    /* Get the element from the list head. */
    ListGetFront(list, &element);
    assert(((Tuple*)element)->first == 10);

    /* Get the element from the list tail. */
    ListGetBack(list, &element);
    assert(((Tuple*)element)->first == 50);

    /* Get the elements from the specified indexes. */
    ListGetAt(list, 2, &element);
    assert(((Tuple*)element)->first == 30);
    ListGetAt(list, 3, &element);
    assert(((Tuple*)element)->first == 40);

    /* Replace the element residing at the list head. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -1;
    tuple->second = 1;
    ListSetFront(list, tuple);

    /* Replace the element residing at the list tail. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -5;
    tuple->second = 5;
    ListSetBack(list, tuple);

    /* Replace the elements residing at the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -2;
    tuple->second = 2;
    ListSetAt(list, 1, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -3;
    tuple->second = 3;
    ListSetAt(list, 2, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -4;
    tuple->second = 4;
    ListSetAt(list, 3, tuple);

    /* Reverse the list. */
    ListReverse(list);

    /*---------------------------------------------------------------*
     * Now the list should be: (-5)<-->(-4)<-->(-3)<-->(-2)<-->(-1)  *
     *---------------------------------------------------------------*/

    /* Remove the element from the list head. */
    ListPopFront(list);

    /* Remove the element from the list tail. */
    ListPopBack(list);

    /* Remove the elements from the specified indexes. */
    ListRemove(list, 1);
    ListRemove(list, 1);

    /* Get the list size. And the remaining element should be (-4). */
    unsigned size = ListSize(list);
    assert(size == 1);

    ListGetFront(list, &element);
    assert(((Tuple*)element)->first == -4);

    ListDeinit(list);
}
Exemple #10
0
int main(int argc, char** argv)
{
    signal(SIGINT, IntHandler);

    Server serv;

    // Initialize the server
    InitConfig(&serv.conf, argv[1], argc, argv);
    
    if(!serv.conf.name) {
        fprintf(stderr, "Script doesn't specify a server name.\n");
        return 1;
    }

    if(!serv.conf.port) {
        fprintf(stderr, "Script doesn't specify a port.\n");
        return 1;
    }

	printf("Successfully configured server.\n"
		"Name: %s\n"
		"Port: %s\n"
		"Num Threads: %d\n"
		"Cycles Per Loop: %d\n"
		"Num Routes: %d\n", serv.conf.name, serv.conf.port, serv.conf.numThreads, serv.conf.cyclesPerLoop, sb_count(serv.conf.routes));

    InitList(&serv.clientQueue, sizeof(Sock));
    InitList(&serv.requestQueue, sizeof(ClientRequest));

    cnd_init(&serv.updateLoop);
    cnd_init(&serv.newConn);

    // The loop thread is responsible for initializing LoopData

    thrd_t loopThread, connThread;

    thrd_create(&loopThread, MainLoop, &serv);
    thrd_create(&connThread, ConnLoop, &serv);

    // Initialize winsock and listen for clients
    WSADATA wsaData;
    SOCKET listenSocket;

    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if(iResult != 0) {
        fprintf(stderr, "WSAStartup failed: %d\n", iResult);
        return 1;
    }

    struct addrinfo hints;
    
    ZeroMemory(&hints, sizeof(hints));

    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    struct addrinfo* result;

    iResult = getaddrinfo(NULL, serv.conf.port, &hints, &result);
    if(iResult != 0) {
        fprintf(stderr, "getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    
    listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

    if(listenSocket == INVALID_SOCKET) {
        fprintf(stderr, "Error at socket(): %d\n", WSAGetLastError());
        freeaddrinfo(result); 
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);

    if(iResult == SOCKET_ERROR) {
        fprintf(stderr, "bind failed with error: %d\n", WSAGetLastError());
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }

    if(listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
        fprintf(stderr, "listen failed with error: %d\n", WSAGetLastError());
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }
	
	while (KeepRunning) {
		SOCKET clientSocket = accept(listenSocket, NULL, NULL);

		if (clientSocket == INVALID_SOCKET) {
			fprintf(stderr, "accept failed: %d\n", WSAGetLastError());
            continue;
		}

		int iMode = 1;
        if (ioctlsocket(clientSocket, FIONBIO, &iMode) == SOCKET_ERROR) {
            fprintf(stderr, "ioctlsocket failed: %d\n", WSAGetLastError());
            continue;
        }

        Sock sock;

        InitSock(&sock, (void*)clientSocket);
        
        ListPushBack(&serv.clientQueue, &sock);

        cnd_signal(&serv.newConn);
	}

    WSACleanup();

    thrd_join(&connThread, NULL);
	thrd_join(&loopThread, NULL);

    cnd_destroy(&serv.newConn);
    cnd_destroy(&serv.updateLoop);

	DestroyList(&serv.requestQueue);
	DestroyList(&serv.clientQueue);

    DestroyConfig(&serv.conf);

	return 0;
}