push_server.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 2,135 行 · 第 1/5 页
C
2,135 行
* an alarm time. If found, clear the entry so it will * not fire again. */ for (alarmp = alarmlist; alarmp != NULL; alarmp = alarmtmp){ alarmtmp = alarmp->next; if (alarmp->timerHandle == fd){ alarmentry = midpStrdup(alarmp->midlet); if (alarmentry){ jlong lastalarm; alarmadd(alarmentry, 0, &lastalarm); return alarmentry; } else{ destroyTimerHandle(alarmp->timerHandle); alarmp->wakeup = 0; alarmp->timerHandle = 0; alarmp->state = AVAILABLE; } } } return NULL;}/** * Lookup the push entry, given the connection URL. * * @param str the connection URL * @return the full-text push entry from the registry */char *pushfindconn(char *str){ PushEntry *p; /* Find the entry that has matching connection URL. */ for (p = pushlist; p != NULL ; p = p->next){ if (strncmp (str, p->value, strlen(str)) == 0){ return p->value; } } return NULL;}/** * Given the connection string and port number, look up the * push entry and return its filter. * * @param conn the connection string * @param port the port number to match * @return the filter from the registry */char *pushgetfilter(char *conn, int port){ PushEntry *p; /* Find the entry that has matching connection and port. */ for (p = pushlist; p != NULL ; p = p->next){ if ((strncmp (conn, p->value, strlen(conn)) == 0) && (p->port == port)){ /* Find the matching filter */ return pushfilter(p->value); } } return NULL;}/** * Given the connection string and application ID, look up the * push entry and return its filter. * * @param conn the connection string * @param appID The MMS application ID to match * @return the filter from the registry */char *pushgetfiltermms(char *conn, char *appID){ PushEntry *p; /* Find the entry that has matching connection and port. */ for (p = pushlist; p != NULL ; p = p->next){ if ((strncmp (conn, p->value, strlen(conn)) == 0) && (strcmp(appID, p->appID) == 0)){ /* Find the matching filter */ return pushfilter(p->value); } } return NULL;}/** * To be called when a WMA SMS or CBS message has arrived and has been * cached in an inbox. For the given connection string and * port number, a push entry is looked up. If one is found, * isWMAMessCached flag is set to KNI_TRUE. * * @param conn the connection string * @param port the port number to match */void pushsetcachedflag(char *conn, int port){ PushEntry *p; /* Find the entry that has matching connection and port. */ for (p = pushlist; p != NULL ; p = p->next){ if ((strncmp (conn, p->value, strlen(conn)) == 0) && (p->port == port)){ p->isWMAMessCached = KNI_TRUE; } }}/** * To be called when a WMA MMS message has arrived and has been * cached in an inbox. For the given connection string and * port number, a push entry is looked up. If one is found, * isWMAMessCached flag is set to KNI_TRUE. * * @param conn the connection string * @param appId The MMS application ID to match */void pushsetcachedflagmms(char *conn, char *appID){ PushEntry *p; /* Find the entry that has matching connection and port. */ for (p = pushlist; p != NULL ; p = p->next){ if ((strncmp (conn, p->value, strlen(conn)) == 0) && (strcmp(appID, p->appID) == 0)){ p->isWMAMessCached = KNI_TRUE; } }}/** * Finds push entries that belong to the given suite. If the available * flag is set, return only those entries with live connections (socket) * or cached incoming data. * * @param store The storagename of the suite * @param available If set, return only "live" push entries * * @return A comma delimited list of full-text push entries */char *pushfindsuite(char *store, int available){ PushEntry *p; char *ret = NULL; char *ptr; int len=0; char *connlist = NULL; /* Find the entry to pass off the open file descriptor. */ for (p = pushlist; p != NULL ; p = p->next){ if (strcmp(store, p->storagename) == 0){ ret = midpStrdup(p->value); for (ptr = ret, len=0; *ptr; ptr++, len++){ if (*ptr == ','){ *ptr = '\0'; break; } } /* * Check if there is pending I/O on the * current file descriptor. e.g. an accepted socket * or a cache datagram. */ if (available && (p->fd != -1)){ if ((p->fdsock == -1) && (p->pCachedData == NULL) && (!p->isWMAMessCached)){ midpFree(ret); ret = NULL; continue; } } /* * Append the entries together in a single list. */ if (connlist == NULL){ connlist= ret; } else{ strcat(connlist, ","); strcat(connlist, ret); midpFree(ret); } ret = NULL; } } return connlist;}/** * Parses the persistent push registry from disk into the * in memory cache representation. * * @param pushfd file descriptor for reading * @return <tt>0</tt> if successful, <tt>-1</tt> if there was an error opening * the push registry file, <tt>-2</tt> if there was a memory allocation failure */static int parsePushList(int pushfd){ char buffer[MAX_LINE+1]; char *errStr = NULL; PushEntry *pe; /* Read a line at a time */ while (readLine(&errStr, pushfd, buffer, sizeof(buffer)) != 0){ if (errStr != NULL){ REPORT_WARN2(LC_PROTOCOL, "Warning: could not read push registration: %s; buffer = '%s'", errStr, buffer); storageFreeError(errStr); return -1; } pe = (PushEntry *) midpMalloc (sizeof(PushEntry)); if (pe == NULL){ pushListFree(); return -2; } pe->next = pushlist; pe->value = midpStrdup(buffer); pe->storagename = midpStrdup(pushstorage(pe->value, 3)); if ((pe->value == NULL) || (pe->storagename == NULL)){ midpFree(pe->value); midpFree(pe->storagename); midpFree(pe); pushListFree(); return -2; } else{ pe->filter = pushfilter(pe->value); pe->fd = -1; pe->fdsock = -1; pe->fdAccepted = -1; pe->state = AVAILABLE; pe->pCachedData = NULL; pe->isWMAEntry = KNI_FALSE; pe->isWMAMessCached = KNI_FALSE; pe->appID = NULL;#if ENABLE_JSR_180 pe->isSIPEntry = KNI_FALSE; pe->isShared = KNI_FALSE;#endif } /* * Add the new entry to the top of the push cached * list. */ pushlist = pe; pushlength++; } /* This check is required for the case when readLine() didn't put any characters into the buffer and while() was not executed */ if (errStr != NULL){ REPORT_WARN1(LC_PROTOCOL, "Warning: could not read push registration: %s", errStr); storageFreeError(errStr); return -1; } return 0;}static void pushStartListening(){ PushEntry *pe; for (pe = pushlist; pe != NULL ; pe = pe->next){ if (pe->state == AVAILABLE){ pushProcessPort(pe); if (pe->fd != -1){ pe->state = CHECKED_IN; pushAddNetworkNotifier(pe); } } }}/** * Parses the URL port. * * buffer A full-text push entry string from the registry ("mms://12345:11") * @return port or <tt>-1</tt> on parse error */static int getUrlPort(char* buffer){ char* p = buffer; int colon_found = 0; for (; *p != '\0' ; p++){ if (*p == ':'){ colon_found++ ; } if (colon_found == 2){ p++ ; /* * Parse the port number from the * connection string */ return atoi(p); } } return -1;}/** * Checks current status of the network. * * @return <tt>1</tt> if network is enabled, <tt>0</tt> otherwise */static int isNetworkUp(){ int res, status; res = 0; switch (networkStatus){ case NET_STATUS_UP: res = 1; break; case NET_STATUS_GOING_UP:{ status = pcsl_network_init_finish(); switch (status){ case PCSL_NET_SUCCESS: networkStatus = NET_STATUS_UP; res = 1; break; case PCSL_NET_WOULDBLOCK: /* network is still going up. */ break; default: /* network error, try later */ networkStatus = NET_STATUS_ERROR; break; } break; } case NET_STATUS_UNKNOWN: case NET_STATUS_ERROR:{ status = pcsl_network_init_start(pcsl_network_initialized); switch (status){ case PCSL_NET_SUCCESS: networkStatus = NET_STATUS_UP; res = 1; break; case PCSL_NET_WOULDBLOCK: networkStatus = NET_STATUS_GOING_UP; break; default: /* network error, try later */ networkStatus = NET_STATUS_ERROR; break; } break; } case NET_STATUS_GOING_DOWN: case NET_STATUS_DOWN: /* * don't prevent network from going down as well as don't try to * bring it up if MIDP has put it down. */ break; default: break; } return res;}/** * Parses the port number from the connection field * and uses it for the connection appropriate open * call. The handle will be included in * the connection registry until needed by the * application level connection open request. * * @param pe push entry; the following fields are used: * <ul> * <li>value A full-text push entry string from the registry</li> * <li>fd A pointer to a handle. Used to return the open handle</li> * <li>port A pointer to a portId. Used to return the port to the caller</li> * <li>appID Application ID</li> * <li>isWMAEntry set to KNI_TRUE for a WMA connection, KNI_FALSE otherwise</li> * <li>isSipEntry set to KNI_TRUE for a SIP/SIPS connection, KNI_FALSE otherwise</li> * * @return <tt>0</tt> if successful, * <tt>-1</tt> connection error, * <tt>-2</tt> if out of memory * <tt>-3</tt> if illegal arguments or unknown connection type * </ul> */static int pushProcessPort(PushEntry* pe){ char *p, *buffer; void *handle; void *context = NULL; int status; if (pe == NULL || pe->value == NULL){ return -3; } /* Make sure the network is properly initialized. */ if (!isNetworkUp()){ pe->fd = -1; return -1; } /* * Open the file descriptor so it can be monitored * for inbound connection requests. For
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?