Example #1
0
void KAIBase::OnReturn(void)
{
	int nRetCode = cmsInvalid;
	BOOL bRetCode = FALSE;

	bRetCode = CheckReturn();
	KG_PROCESS_SUCCESS(bRetCode);

	nRetCode = m_pSelf->m_eMoveState;

	if (nRetCode == cmsOnStand)
	{
		int nDir = g_GetDirection(m_pSelf->m_nX, m_pSelf->m_nY, m_nReturnX, m_nReturnY);
		m_pSelf->Turn(nDir, true, true);

		// 跑回ReturnPoint
		//KGLogPrintf(KGLOG_DEBUG, "[AI] Return Run to (%d, %d)\n", m_nReturnX, m_nReturnY);
		m_pSelf->RunTo(m_nReturnX, m_nReturnY);
	}

	return;
Exit1:
	//返回原点了,停一秒
	if (m_pNpcTeam)
		DoWait();
	else
		DoIdle(GAME_FPS);

	return;
}
int __cdecl main(int argc, char **argv)
{

    char *interface = NULL;
    char *Buffer = NULL;
    char *port = DEFAULT_PORT;
    char host[NI_MAXHOST];
    char serv[NI_MAXSERV];
    int hostlen = NI_MAXHOST;
    int servlen = NI_MAXSERV;
    int ai_family = AF_UNSPEC;
    int i = 0;
    int nStartup = 0;
    struct addrinfo hints = {0};
    struct addrinfo *local = NULL;
    WSADATA wsaData;
    SOCKET listen_socket = INVALID_SOCKET;
    SOCKET accept_socket = INVALID_SOCKET;
    WSAOVERLAPPED *Overlap = NULL;
    DWORD bytes = 0;
    DWORD bytes_read = 0;
    DWORD lasterror = 0;

    //
    // Handles is the array that stores the Event Handles
    //
    WSAEVENT Handles[MAX_IO_PEND] ;

    //
    // socklist is a parallel array that keeps state information for
    // each Handle.
    //
    Socklist socklist[MAX_IO_PEND];

    //
    // Pointers to Microsoft specific extensions
    //
    LPFN_ACCEPTEX pfnAcceptEx;
    GUID acceptex_guid = WSAID_ACCEPTEX;

    __try
    {


        if (NULL == (Buffer = (char*)XMALLOC(((2*sizeof(SOCKADDR_STORAGE))+32)*sizeof(char))))
        {
            ERR("HeapAlloc()");
            __leave;

        }

        //
        // Parse arguments
        //
        if ( argc > 1 )
        {
            for ( i = 1;i < argc; i++ )
            {
                if ( (argv[i][0] == '-') || (argv[i][0] == '/') )
                {
                    switch ( tolower(argv[i][1]) )
                    {
                    
                    case 'i':
                        if ( i+1 < argc )
                            interface = argv[++i];
                        break;
                    case 'e':
                        if ( i+1 < argc )
                            port = argv[++i];
                        break;
                    default:
                        {
                            Usage(argv[0]);
                            __leave;
                        }
                        break;
                    }
                }
                else
                {
                    Usage(argv[0]);
                    __leave;
                }
            }
        }

        //
        // Since port 0 would be any available port. 
        // Resetting port back to DEFAULT_PORT to avoid confusion.
        //
        if ( 0 == strncmp(port, "0", 1) )
        {
            port = DEFAULT_PORT;
        }

        if ( 0 != (nStartup = WSAStartup(WS_VER, &wsaData)) )
        {
            WSASetLastError(nStartup); //WSAStartup does not set last error on failure
            ERR("WSAStartup()");
            __leave;

        }
        else
            nStartup++;

        //
        // Resolve the interface
        //
        SecureZeroMemory(&hints,sizeof(hints));
        hints.ai_flags  = ((interface) ? 0 : AI_PASSIVE);
        hints.ai_family = ai_family;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;

        if ( 0 != getaddrinfo(interface, port, &hints, &local))
        {
            ERR("getaddrinfo()");
            __leave;
        }

        ai_family = local->ai_family;

        if (NULL == local)
        {
            fprintf(stderr, "getaddrinfo() failed to resolve/convert the interface\n");
            __leave;
        }

        if (INVALID_SOCKET == (listen_socket = WSASocket(local->ai_family, 
                                                         local->ai_socktype, 
                                                         local->ai_protocol, 
                                                         NULL, 
                                                         0,
                                                         WSA_FLAG_OVERLAPPED
                                                        )))
        {
            ERR("WSASocket()");
            __leave;
        }


        if ( SOCKET_ERROR == bind(listen_socket, 
                                  local->ai_addr, 
                                  (int)local->ai_addrlen
                                 ))
        {
            ERR("bind()");
            __leave;
        }


        if ( SOCKET_ERROR == listen(listen_socket, DEFAULT_BACKLOG))
        {
            ERR("listen()");
            __leave;
        }

        //
        // Resolve numeric host name
        //
        if ( 0 != getnameinfo(local->ai_addr, 
                              (int)local->ai_addrlen, 
                              host, 
                              hostlen, 
                              serv, 
                              servlen,
                              NI_NUMERICHOST | NI_NUMERICSERV
                             ))
        {
            ERR("getnameinfo()");
            __leave;
        }

        // Don't need the local interface anymore
        if (NULL != local)
            freeaddrinfo(local);

        printf("Listening on %s:%s\n", host, serv);

        //
        // Add the listening socket to our state information for the handle.
        //
        socklist[0].sock = listen_socket;

        curr_size =1;

        for ( i = 0;i < MAX_IO_PEND;i++ )
            Handles[i] = WSA_INVALID_EVENT;

        //
        // Load the extension functions
        //

        if ( SOCKET_ERROR == WSAIoctl(listen_socket, 
                                      SIO_GET_EXTENSION_FUNCTION_POINTER,
                                      &acceptex_guid,
                                      sizeof(acceptex_guid),
                                      &pfnAcceptEx,
                                      sizeof(pfnAcceptEx), 
                                      &bytes, 
                                      NULL, 
                                      NULL
                                     ))
        {
            fprintf(stderr,"Failed to obtain AcceptEx() pointer: ");
            ERR("WSAIoctl()");
            __leave;
        }

        //
        // The structure of the following loop is very similar to a situation
        // where select() might be used. 
        // We use WSAGetOverlappedResult() to multiplex between incoming/outgoing
        // data on existing connections.
        //
        // We don't queue an AcceptEx() until someone actually connects to 
        // the previous socket. This is to keep the code simple, not a limitation
        // of the API itself.
        //
        for ( ;; )
        {

            // create a socket for AcceptEx()

            if (INVALID_SOCKET == (accept_socket = WSASocket(ai_family, 
                                                             SOCK_STREAM, 
                                                             0, 
                                                             NULL, 
                                                             0,
                                                             WSA_FLAG_OVERLAPPED
                                                            )))
            {
                ERR("WSASocket()");
                __leave;
            }

            //
            // Allocate an overlapped structure.
            // We use the Offset field to keep track of the socket handle
            // we have accepted a connection on, since there is no other
            // way to pass information to GetOverlappedResult()
            //
            Overlap = (WSAOVERLAPPED*)XMALLOC(sizeof(WSAOVERLAPPED));

            //
            // Did the HeapAllocation FAIL??
            //
            if ( Overlap == NULL )
            {
                ERR("HeapAlloc()");
                __leave;
            }

            SecureZeroMemory(Overlap, sizeof(WSAOVERLAPPED));

            if ( WSA_INVALID_EVENT == (Overlap->hEvent = WSACreateEvent() ))
            {
                ERR("WSACreateEvent()");
                __leave;
            }

            //
            // Set the appropriate array members
            //
            Handles[0] = Overlap->hEvent;
            socklist[0].overlap = Overlap;
            socklist[0].SockAccepted = accept_socket;

            //
            // AcceptEx()
            //
            if ( !pfnAcceptEx(listen_socket, 
                              accept_socket, 
                              Buffer,
                              0, // read nothing from the socket
                              sizeof(SOCKADDR_STORAGE)+16, 
                              sizeof(SOCKADDR_STORAGE)+16,
                              &bytes_read, 
                              Overlap
                             ))
            {

                lasterror = WSAGetLastError();
                if ( WSA_IO_PENDING != lasterror)
                {
                    ERR("AcceptEx()");
                    for ( i = 0; i < curr_size; i++ )
                    {
                        shutdown(socklist[i].sock, SD_BOTH);
                        CLOSESOCK(socklist[i].sock);
                        XFREE(socklist[i].overlap);
                        CLOSEEVENT(Handles[i]);
                    }
                    __leave;
                }
            }

            //
            // This loop simple checks the handles to see which one is 
            // signalled. 
            // If error, exit. 
            // If there is a new incoming connection, we break to the outer loop
            // queue another AcceptEx()
            //
            for ( ;; )
            {
                i = DoWait(Handles, socklist);
                if ( i < 0 )
                    break;
                HandleEvent(i, Handles, socklist);
                if ( i == 0 )
                    break;
            };
            if ( i < 0 )
            {
                for ( i = 0; i < curr_size; i++ )
                {
                    shutdown(socklist[i].sock, SD_BOTH);
                    CLOSESOCK(socklist[i].sock);
                    XFREE(socklist[i].overlap);
                    CLOSEEVENT(Handles[i]);
                }
                __leave;
            }
        }
    }
    __finally
    {
        XFREE(Buffer);
        if (NULL != local) freeaddrinfo(local);
        CLOSESOCK(listen_socket); 
        CLOSESOCK(accept_socket);
        CLOSEEVENT(Overlap->hEvent);
        XFREE(Overlap);

    }
}
Example #3
0
void RestoreWait()
{
	DoWait(0);
}
Example #4
0
void EndWait()
{
	DoWait(-1);
}
Example #5
0
void BeginWait()
{
	DoWait(1);
}