push_server.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 2,135 行 · 第 1/5 页

C
2,135
字号
                if (midpDecResourceCount(RSC_TYPE_UDP, 1) == 0) {                    REPORT_INFO(LC_PROTOCOL, "(Push)Datagram : Resource limit"                                " update error");                }            }        }#if (ENABLE_JSR_205 || ENABLE_JSR_120)        /* Check for sms,cbs or mms connection. */        wmaPushCloseEntry(p->state, p->value, p->port,                          suiteIdFromChars(p->storagename), p->appID, p->fd);#endif        p->fd = -1;    }    p->state = AVAILABLE;    midpFree(p->value);    p->value = NULL;    midpFree(p->filter);    p->filter = NULL;    midpFree(p->storagename);    p->storagename = NULL;    /* Remove the registration entry from the list. */    *pPrevNext = p->next;    midpFree(p);    pushlength--;}/** * Returns the number of buffered bytes for the given socket. * * @param fd handle of the socket * @return number of cached bytes of -1 if none */int pushcacheddatasize(int fd) {    PushEntry *p;    for (p = pushlist; p != NULL ; p = p->next) {        if ((p->fd == fd && p->pCachedData != NULL) ||            (p->fdAccepted == fd && p->pCachedData != NULL)) {            return(p->pCachedData->length - p->pCachedData->offs);        }    }    return -1;}/** * Fetch the buffered datagram or TCP packet into a buffer. * * @param fd The handle of the socket from which the packet was received * @param ip The ip address of the incoming datagram * @param sndport The port from which the data was sent * @param buf A pointer to a buffer into which the data should be copied * @param len The size of buf * @return the length of the returned data if successful, or <tt>-1</tt> *         if unsuccessful. */int pushgetcachedpacket(int fd, int *ip, int *sndport, char *buf, int len) {    PushEntry *p;    int length = -1;    /* Find the entry to pass off the open file descriptor. */    for (p = pushlist; p != NULL; p = p->next) {        if (((p->fd == fd && p->fdAccepted == -1) || (p->fdAccepted == fd)) &&            (p->pCachedData != NULL)) {            /* Return the cached data. */            *ip = p->pCachedData->ipAddress;            *sndport = p->pCachedData->senderport;            length = p->pCachedData->length - p->pCachedData->offs;            if (length > 0) {                if (len < length) {                    length = len;                }                memcpy(buf, &p->pCachedData->buffer[p->pCachedData->offs],                       length);                p->pCachedData->offs += length;            }            if (p->pCachedData->offs >= p->pCachedData->length) {                p->fdAccepted = -1;                /* Destroy the cached entry after it has been read. */                midpFree(p->pCachedData);                p->pCachedData = NULL;            }            return length;        }    }    return -1;}/** * Checks out the handle for the requested server socket. * @param fd The handle to check out * @return the handle to the checked-out server socket */int pushcheckoutaccept(int fd) {    PushEntry *p;    int temp;    /* Find the entry to pass off the open file descriptor. */    for (p = pushlist; p != NULL ; p = p->next) {        if (p->fd == fd) {            temp = p->fdsock;            p->fdsock = -1;            return temp;        }    }    return -1;}/** * Checks out the handle for the requested connection. * The CHECKED_OUT token * is left in the registry to indicate that an application is * actively using the connection. * * @param protocol The protocol of the connection * @param port The port number of the connection * @param store The storage name of the requesting Suite * * @return <tt>-1</tt> if the connection is not found, other wise returns * the previously opened file descriptor. */int pushcheckout(char* protocol, int port, char * store) {    PushEntry *p;    int fd;    jboolean wmaProtocol = KNI_FALSE;    jboolean standardProtocol = KNI_FALSE;#if ENABLE_JSR_82    bt_bool_t is_bluetooth = bt_is_bluetooth_url(protocol);#endif    /* Find the entry to pass off the open file descriptor. */    for (p = pushlist; p != NULL ; p = p->next) {#if ENABLE_JSR_82        if (is_bluetooth == BT_BOOL_TRUE &&            !strncmp(p->value, protocol, strlen(protocol))) {            if (strcmp(store, p->storagename)) {                return -2;            }            return 0;        }#endif#if (ENABLE_JSR_205 || ENABLE_JSR_120)        wmaProtocol = isWmaProtocol(p->port, p->value,                                    p->storagename, port, store);#endif#if ENABLE_JSR_180        /*         * A registered 'sip' connection matches physical 'datagram'         * connection for UDP transport and 'socket' connection for TCP.         */        standardProtocol = (p->port == port &&                            (!strncmp(p->value, protocol, strlen(protocol)) ||                             (strncmp(p->value, "sip", 3) == 0 &&                              ((!strncmp("datagram", protocol, strlen(protocol)) &&                                 pushIsDatagramConnection(p->value)) ||                               (!strncmp("socket", protocol, strlen(protocol)) &&                                 pushIsSocketConnection(p->value)))                             )                            )                           );#else        /* Port and protocol must match before other checks are done. */        standardProtocol = (p->port == port &&                            strncmp(p->value, protocol, strlen(protocol)) == 0);#endif        if (standardProtocol || wmaProtocol) {            /* Check if the current suite reserved the port. */            if (strcmp(store, p->storagename) != 0) {                return -2;            }            fd = p->fd;            /* The push system should stop monitoring this connection. */            if (pushIsSocketConnection(p->value)) {                pcsl_remove_network_notifier((void*)fd, PCSL_NET_CHECK_ACCEPT);            } else if (pushIsDatagramConnection(p->value)) {                pcsl_remove_network_notifier((void*)fd, PCSL_NET_CHECK_READ);            }            p->state = CHECKED_OUT;            return fd;        }    }    return -1;}/** * Checks in the handle for the requested connection. * @param fd The handle to be checked in * @return <tt>0</tt> if successful, or <tt>-1</tt> on failure to check in the * file descriptor to the cached push registry. */int pushcheckin(int fd) {    PushEntry *p;    /* Find the entry to check in the open file descriptor. */    for (p = pushlist; p != NULL ; p = p->next) {        if (p->fd == fd) {            if (p->state == CHECKED_OUT) {                pushcheckinentry(p);            }            return 0;        }    }    return -1;}/** * Checks in all the push connections. Used to cleanup by runMIDletWithArgs. */void pushcheckinall() {    PushEntry *p;    for (p = pushlist; p != NULL ; p = p->next) {        pushcheckinentry(p);    }}/** * Checks in the push connection(s) left over from the previous suite * that were not opened. Used between VM starts by runMIDletWithArgs. * The method does not check in connections that may still be used * by the next MIDlet, but leaves them ready to check out. * * @param nextToRun suiteId of the next suite to run, any connection *     that is not checked in and does not belong to the next suite to *     to run will be checked in. */void pushcheckinLeftOvers(SuiteIdType nextToRun) {    PushEntry *p;    const pcsl_string* strSuiteId = midp_suiteid2pcsl_string(nextToRun);    const char* pszTemp = (char*)pcsl_string_get_utf8_data(strSuiteId);    /* IMPL_NOTE: can we do anything meaningful in case of out-of-memory error? */    for (p = pushlist; p != NULL ; p = p->next) {        if (p->state == CHECKED_IN) {            continue;        }        if (NULL != pszTemp) {            if (0 == strcmp(pszTemp, p->storagename)) {                /* matches next suite to run, do not check in */                continue;            }        }        pushcheckinentry(p);    }    pcsl_string_release_utf8_data((jbyte*)pszTemp, strSuiteId);}/** * Checks in connections that are in * launch pending state for a specific MIDlet. * * @param suiteId Suite ID of the MIDlet * @param pszClassName Class name of the MIDlet */voidpushcheckinbymidlet(SuiteIdType suiteId, char* pszClassName) {    PushEntry *p;    char* pTemp;    int tempLen;    const pcsl_string* strId = midp_suiteid2pcsl_string(suiteId);    const char* pszSuiteId = (char*)pcsl_string_get_utf8_data(strId);    for (p = pushlist; p != NULL ; p = p->next) {#ifdef ENABLE_JSR_82        /* IMPL_NOTE: Provide a separate function for this functionality.         * This function is called when a MIDlet terminates, and we use it to         * re-activate services closed by the MIDlet. This bears no connection         * to the launch pending state (see function comment).         */        bt_port_t port;        if (bt_push_parse_url(p->value, &port, NULL) == BT_RESULT_SUCCESS) {            bt_handle_t handle = bt_push_start_server(&port);            if (handle != BT_INVALID_HANDLE) {                p->fd = (int)handle;            }            continue;        }#endif        if (p->state != LAUNCH_PENDING) {            continue;        }        if (strcmp(p->storagename, pszSuiteId) != 0) {            continue;        }        pTemp = pushclassname(p->value, &tempLen);        if (strncmp(pTemp, pszClassName, tempLen) != 0) {            continue;        }        pushcheckinentry(p);    }    pcsl_string_release_utf8_data((jbyte*)pszSuiteId, strId);}/** * Checks in the handle for the requested connection, * given the connection name. * @param str The connection name * @return <tt>0</tt> on success, or <tt>-1</tt> on failure to check in the * file descriptor to the cached push registry. */int pushcheckinbyname(char* str) {    PushEntry *p;    /* Find the entry to remove */    for (p = pushlist; p != NULL ; p = p->next) {        if (strncmp (str, p->value, strlen(str)) == 0) {            pushcheckinentry(p);            return 0;        }    }    return -1;}/** * Checks in the push entry and cleanup anything the MIDlet did not get. * @param pe The push entry to check in. */static void pushcheckinentry(PushEntry *pe) {    pushcleanupentry(pe);    if (pe->fd != -1) {        pe->state = CHECKED_IN;        pushAddNetworkNotifier(pe);    }}/** * Cleanup anything cached in the entry the MIDlet did not get. * But do not close the server port. * @param p The push entry to clean up */static void pushcleanupentry(PushEntry *p) {#if ENABLE_JSR_82    if (bt_is_bluetooth_url(p->value)) {        bt_push_reject(bt_push_find_server((bt_handle_t)p->fd));        p->fdsock = -1;        return;    }#endif    /* Close any accepted socket not accessed, yet (if any). */    if (p->fdsock != -1) {#if ENABLE_SERVER_SOCKET        void *context;        pcsl_socket_close_start((void*)(p->fdsock), &context);        /*         * Update the resource count         * Accepted sockets should be counted against client socket         * resource limit and not serversocket         */        if (midpDecResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) {            REPORT_INFO(LC_PROTOCOL, "(Push)TCP Client : Resource limit"                        " update error");        }#endif        p->fdsock = -1;        p->fdAccepted = -1;    }    /* Remove the cached datagram (if any). */    if (p->pCachedData != NULL) {        midpFree(p->pCachedData);        p->pCachedData = NULL;    }}#if ENABLE_JSR_180/** * Applies SIP filtering rules on the given push entry. * IMPL_NOTE: it should be moved to JSR 180 workspace. * * @param pushp the push entry to filter * * @return a midlet suite to launch or NULL */static char* pushApplySipFilter(PushEntry* pushp) {    unsigned char *sender = NULL;    unsigned char *acceptcontact_type = NULL;    unsigned char *required_type = NULL;    char *p;    int required_type_len;    PushEntry* next;    jboolean found = KNI_FALSE;    /*     * SIP Datagram and Socket connections use the SIP     * "From header URI" filter. First, extract the sender     * from the cached message, then check for a match     * with the filter pattern string.     */    sender = getSipFromHeaderURI((unsigned char *)                                 pushp->pCachedData->buffer,                                 pushp->pCachedData->length);    next = pushp;    do {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?