Ejemplo n.º 1
0
/**
 * Common functionality for accept start and finish
 */
static int pcsl_serversocket_accept_common(
    void *handle,
    void **pConnectionHandle,
    void **pContext)
{
    SOCKET sock;
    struct sockaddr sa;
    int saLen = sizeof (sa);

    sock = accept((SOCKET)handle, &sa, &saLen);
    /*
      fprintf(stderr, "accept(%d) = %d\n", handle, sock);
    */
    if (sock == SOCKET_ERROR) {
        lastError = WSAGetLastError();

        if (lastError == WSAEWOULDBLOCK) {
            WSAAsyncSelect((SOCKET)handle, win32app_get_window_handle(), WM_NETWORK, FD_ACCEPT);
            *pContext = NULL;
            return PCSL_NET_WOULDBLOCK;
        } else {
            sock = PCSL_NET_IOERROR;
        }
    } else {
        unsigned long blockingFlag = 1;

        ioctlsocket(sock, FIONBIO, &blockingFlag);
        *pConnectionHandle = (void*)sock;
    }

    WSAAsyncSelect((SOCKET)handle, win32app_get_window_handle(), 0, 0);
    return PCSL_NET_SUCCESS;
}
Ejemplo n.º 2
0
/**
 * Common implementation between pcsl_datagram_read_start() 
 * and pcsl_datagram_read_finish().
 */
static int pcsl_datagram_read_common(void *handle, unsigned char *pAddress,
        int *port, char *buffer, int length, int *pBytesRead) {
    SOCKET s = (SOCKET)handle;
    struct sockaddr_in addr;
    int len = sizeof(struct sockaddr_in);
    int status;

    status = recvfrom(s, buffer, length, 0, (struct sockaddr*)&addr,  &len);
    lastError = WSAGetLastError();

    if (SOCKET_ERROR == status) {
        if (WSAEWOULDBLOCK == lastError) {
            /*
             * Win32 only has one notifier per socket so always set both and
             * close, the MIDP event code can handle any extra notifications.
             * Do not cancel the notifications until the socket is closed.
             */
            WSAAsyncSelect(s, win32app_get_window_handle(), WM_NETWORK,
                           FD_READ | FD_WRITE | FD_CLOSE);
            return PCSL_NET_WOULDBLOCK;
        }

        if (WSAECONNRESET == lastError) {
            /* The last call to sendto failed. Just return 0. */
            memset(pAddress, 0, sizeof(addr.sin_addr.s_addr)); 
            *port = 0;
            *pBytesRead = 0;

            return PCSL_NET_SUCCESS;
        }

        if (WSAEINTR == lastError) {
            return PCSL_NET_INTERRUPTED;
        }

        if (WSAEMSGSIZE == lastError) {
            /* The message was bigger than the buffer provided. */
            status = length;
        } else {
            return PCSL_NET_IOERROR;
        }
    }

    memcpy(pAddress, &addr.sin_addr.s_addr, sizeof(addr.sin_addr.s_addr)); 
    *port = ntohs(addr.sin_port);
    *pBytesRead = status;

    return PCSL_NET_SUCCESS;
}
Ejemplo n.º 3
0
int
createTimerHandle(int alarmHandle, jlong time) {
    /*
     * The alarm handle must be greater than zero. But timer handles need be
     * greater than the event loop timer handle so not to be mistaken for
     * a wake up event.
     */
    alarmHandle += EVENT_TIMER_ID;

    if (time > INT_MAX) {
	time = INT_MAX;
    }

    SetTimer(win32app_get_window_handle(), (UINT)alarmHandle, (UINT)time, NULL);
    return alarmHandle;
}
Ejemplo n.º 4
0
/**
 * See pcsl_datagram.h for definition.
 *
 * Note that this function NEVER returns PCSL_NET_WOULDBLOCK. Therefore, the 
 * finish() function should never be called and does nothing.
 */
int pcsl_datagram_close_start(void *handle, void **pContext) {
    SOCKET s = (SOCKET)handle;

    (void)pContext;

    /*
     * Unblock any waiting threads, by send a close event with an interrupt
     * status. Closesocket cancels async notitifications on the socket and
     * does NOT send any messages.
     */
    PostMessage(win32app_get_window_handle(), WM_NETWORK, s,
                WSAMAKESELECTREPLY(FD_CLOSE, WSAEINTR));

    /*
     * There are no documented errors when closing a UDP port,
     * also nothing can be done if a UDP port can't be closed.
     */
    closesocket(s);
    lastError = 0;
    return PCSL_NET_SUCCESS;
}
Ejemplo n.º 5
0
/**
 * Common implementation between pcsl_datagram_write_start() 
 * and pcsl_datagram_write_finish().
 */
static int pcsl_datagram_write_common(void *handle, unsigned char *ipBytes,
        int port, char *buffer, int length, int *pBytesWritten) {
    SOCKET s = (SOCKET)handle;
    struct sockaddr_in addr;
    int status;

    addr.sin_family      = AF_INET;
    addr.sin_port        = htons((short)port);
    memcpy(&addr.sin_addr.s_addr, ipBytes, sizeof(addr.sin_addr.s_addr));

    status = sendto(s, buffer, length, 0, (struct sockaddr*)&addr,
                    sizeof(addr));
    lastError = WSAGetLastError();

    if (SOCKET_ERROR == status) {
        if (WSAEWOULDBLOCK == lastError) {
            /*
             * Win32 only has one notifier per socket so always set both and
             * close, the MIDP event code can handle any extra notifications.
             * Do not cancel the notifications until the socket is closed.
             */
            WSAAsyncSelect(s, win32app_get_window_handle(), WM_NETWORK,
                           FD_READ | FD_WRITE | FD_CLOSE);
            return PCSL_NET_WOULDBLOCK;
        }

        if (WSAEINTR == lastError) {
            return PCSL_NET_INTERRUPTED;
        }

        return PCSL_NET_IOERROR;
    }

    *pBytesWritten = status;
    return PCSL_NET_SUCCESS;
}
/*
 * This function is called by the VM periodically. It has to check if
 * system has sent a signal to MIDP and return the result in the
 * structs given.
 *
 * Values for the <timeout> paramater:
 *  >0 = Block until a signal sent to MIDP, or until <timeout> milliseconds
 *       has elapsed.
 *   0 = Check the system for a signal but do not block. Return to the
 *       caller immediately regardless of the if a signal was sent.
 *  -1 = Do not timeout. Block until a signal is sent to MIDP.
 */
void checkForSystemSignal(MidpReentryData* pNewSignal,
                          MidpEvent* pNewMidpEvent,
                          jlong timeout) {
    MSG msg;
    unsigned long before;
    unsigned long after;

    if (timeout > 0) {
        before = (unsigned long)GetTickCount();
    }
    
    /*
     * The only way to get output from the window procedure is to use
     * shared data.
     */
    pSignalResult = pNewSignal;
    pMidpEventResult = pNewMidpEvent;

    do {
        if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if (timeout == 0) {
                return;
            }

            /*
             * Unblock at the specified timeout even if not messages are
             * present.
             * A negative timeout value means to block until an event comes in.
             */
            if (timeout > 0) {
                if (SetTimer(win32app_get_window_handle(), EVENT_TIMER_ID,
                       (UINT)timeout, NULL) == 0) {
                    /* Timer already exists from last time. */
                    KillTimer(win32app_get_window_handle(), EVENT_TIMER_ID);
                    
                    if (SetTimer(win32app_get_window_handle(), EVENT_TIMER_ID,
                                 (UINT)timeout, NULL) == 0) {
                        /* Can't set up the timer so do not block. */
                        return;
                    }
                }
            }

            GetMessage(&msg, NULL, 0, 0);
        }

        /* Dispatching the message will call WndProc below. */
        appManagerRequestWaiting = 0;
        DispatchMessage(&msg);

        if (appManagerRequestWaiting ||
               pSignalResult->waitingFor != NO_SIGNAL) {
            /* We got signal to unblock a Java thread. */
            return;
        }

        if (timeout < 0) {
            /* Wait until there is a signal for MIDP. */
            continue;
        }

        if (timeout > 0) {
            after = (unsigned long)GetTickCount();
            if (after > before) {
                timeout -= (jlong)(after - before);
            } else {
                /* The tick count has wrapped. (happens every 49.7 days) */
                timeout -= (jlong)((unsigned long)0xFFFFFFFF - before + after);
            }
        }
    } while (timeout > 0);
}
Ejemplo n.º 7
0
int
destroyTimerHandle(int timerHandle) {
    KillTimer(win32app_get_window_handle(), timerHandle);
    return 0;
}