📄 protocol.c
字号:
if (pid->connHandle != NULL) protocolReleaseConnection(pid); if (pid->protocolSettings != NULL) sapParmsPtr = pid->protocolSettings->servAccessPtParms; /* * The following code should set a flag indicating if the SAP * was opened for session mode or not. Questions is: How * do we know what the default mode is? */ if (sapParmsPtr != NULL) { rc = initializeSAP(&(pid->connHandle), sapParmsPtr->bearerType, sapParmsPtr->addressType, sapParmsPtr->serverAddress, sapParmsPtr->serverPort, sapParmsPtr->clientAddress, sapParmsPtr->clientPort); } else rc = initializeSAPd(&(pid->connHandle)); return rc;} /* End of protocolInitializeConnection() *//** * protocolReleaseConnection * - closes the Service Access Point and nullifies the connection * handle to it. * * IN: pid A pointer to a protocol handle structure * **/void protocolReleaseConnection(WspProtocolHandle_t *pid) { XPTDEBUG(("protocolReleaseConnection(%lx)\n", (unsigned long) pid)); if ((pid == NULL) || (pid->connHandle == NULL)) return; closeSAP(pid->connHandle); pid->connHandle = NULL;} /* End of protocolReleaseConnection() *//** * protocolInitializeRequest * - Builds the header information for the WSP request and, based on * the method requested, sends the WSP request to the WAP server. * * IN: pid A pointer to a protocol handle structure * mode Communication mode (send, receive, exchange, etc.) * pDoc A pointer to a structure that contains information * about the data to be transmitted. * For receive, this structure is also modified with * response information. * * RETURN: * An indication of the success or failure of the operation. * * Implementation Notes: * * In composing the http request, the toolkit user indicates: * method (XptCommunicationMode) * uri (XptCommunicationInfoPtr->szName) * mime type (XptCommunicationInfoPtr->szType) * * Other information may have been specified in the szSettings on the * selectProtocol, but that would be defined by the protocol. * * We are required to support the HTTP Post method. We can optionally * support GET, PUT, HEAD, DELETE, TRACE and OPTIONS. * * The header tags that WSP is required to support are listed in the * SyncML HTTP specification. * **/Ret_t protocolInitializeRequest(WspProtocolHandle_t *pid){ Ret_t rc = SML_ERR_OK; XPTDEBUG(("protocolInitializeRequest(%lx)\n", (unsigned long) pid)); /* Compose the transaction request. */ rc = transInitialize(&(pid->transaction)); if (rc != SML_ERR_OK) return rc; if (pid->protocolSettings != NULL) { rc = transCreateRequest(pid->transaction, pid->protocolSettings->httpParms, pid->protocolSettings->host, ((pid->session == NULL) ? AWSP_FALSE : AWSP_TRUE)); } else { rc = transCreateRequest(pid->transaction, NULL, NULL, ((pid->session == NULL) ? AWSP_FALSE : AWSP_TRUE)); } if (rc != SML_ERR_OK){ transRelease(pid->transaction); pid->transaction = NULL; return rc; } if (pid->mode == XPT_CLIENT) pid->transaction->method = "POST"; else return -1; // What to do for server mode???? /* We should flag that a transaction request is pending for tracking */ return rc;} /* End of protocolInitializeRequest() */void protocolReleaseRequest(WspProtocolHandle_t *pid){ XPTDEBUG(("protocolReleaseRequest(%lx)\n", (unsigned long) pid)); transRelease(pid->transaction); if (pid->protocolSettings != NULL) httpReinitParms(pid->protocolSettings->httpParms); pid->transaction = NULL; /* We should flag that a transaction request is complete for tracking */} /* End of protocolReleaseRequest() *//** * sendRequest * - Invokes the appropriate abstract WSP methodInvoke according to * the mode (session or connectionless). * * IN: pid A pointer to a protocol handle structure * * RETURN: * An indication of whether the method invocation was successful **/Ret_t sendRequest(WspProtocolHandle_t *pid) { Ret_t rc = SML_ERR_OK; XPTDEBUG((" sendRequest(%lx)\n", (unsigned long) pid)); if (pid == NULL) return SML_ERR_A_XPT_INVALID_PARM; /* We wait until now to compose the request-specific http headers so * we only do it once, when we're sure all the data has been set to its * final state */ if (pid->protocolSettings != NULL) transBuildDynamicRequestHdr(pid->transaction, pid->protocolSettings->httpParms); /** * Need to set flag that method is pending in case deselect occurs before response. * Need to keep track of pending transaction id in case abort is required - can more * than one transaction be pending in a single threaded machine? If so, then need a * table of pending transactions associated with a session, and deselect protcol needs * to cancel them all... * One request is made, needs to wait for response confirmation... **/ if (sessionIsConnected(pid->session) != AWSP_TRUE) rc = transSendRequestNoSession(pid->connHandle, pid->transaction); else rc = transSendRequestOverSession(pid->session->sessionHandle, pid->transaction); if (rc != SML_ERR_OK) return rc; rc = processResponse(pid); return rc;} /* End sendRequest() */Ret_t processResponse(WspProtocolHandle_t *pid){ Ret_t rc = SML_ERR_OK; const char *staticHdrs = NULL; char *mergedHdrs = NULL; XPTDEBUG((" processResponse(%lx)\n", (unsigned long) pid)); if (pid == NULL) return SML_ERR_A_XPT_INVALID_PARM; if ((pid->transaction == NULL) || (pid->protocolSettings == NULL)) return rc; staticHdrs = sessionGetStaticServerHeaders(pid->session); if (staticHdrs != NULL) { mergedHdrs = (char *) xppMalloc(xppStrlen(staticHdrs) + (pid->transaction->rspHdrSize)); if (mergedHdrs != NULL) { xppStrcpy(mergedHdrs, staticHdrs); xppStrcpy(mergedHdrs + xppStrlen(staticHdrs) - 1, pid->transaction->rspHdr); rc = httpParseResponseHdr(pid->protocolSettings->httpParms, mergedHdrs); } xppFree(mergedHdrs); } else rc = httpParseResponseHdr(pid->protocolSettings->httpParms, pid->transaction->rspHdr); return rc;} /* End processResponse() *//** * protocolSendRequestData * - invokes abstract WSP method to send push/post data to server. * * IN pid A pointer to the wsp protocol structure * pbData The data buffer to be sent. * uDataSize The size of the data buffer * bLastBlock An indication of whether or not this is the * last block of data to be sent as part of this * transaction. * * RETURN: * An indication of whether the data was successfully sent. **/Ret_t protocolSendRequestData (WspProtocolHandle_t *pid, const void *buffer, size_t bufferLen, size_t *bytesSent){ Ret_t rc = SML_ERR_OK; XPTDEBUG(("protocolSendRequestData(%lx, %lx, %lu, %lx)\n", (unsigned long) pid, (unsigned long) buffer, (unsigned long) bufferLen, (unsigned long) bytesSent)); if ((pid == NULL) || (buffer == NULL) || (bufferLen <= 0)) return SML_ERR_A_XPT_INVALID_PARM; if (pid->transaction == NULL) return -1; /* invalid request */ /* Verify that transaction state allows sendData - make sure they are not * trying to send more data after having sent the last block */ transAddRequestData(pid->transaction, buffer, bufferLen); /* For now we'll just tell them this - need to figure out if/when we're * lying. Truth is this should be 0 until the last block, at which time * it would be the total accumulated. However, I don't think the app * would be prepared to handle that... */ *bytesSent = bufferLen; return rc;} /* End protocolSendRequestData() */Ret_t protocolSendComplete(WspProtocolHandle_t *pid){ XPTDEBUG(("protocolSendComplete(%lx)\n", (unsigned long) pid)); if (pid == NULL) return SML_ERR_A_XPT_INVALID_PARM; return sendRequest(pid); /* Have all data, do WSP request */} /* End protocolSendComplete() */Ret_t protocolReadResponseData(WspProtocolHandle_t *pid, void * pbData, size_t uDataSize, size_t *puDataRead){ XPTDEBUG(("protocolReadResponseData(%lx, %lx, %lu, %lx)\n", (unsigned long) pid, (unsigned long) pbData, (unsigned long) uDataSize, (unsigned long) puDataRead)); /* Verify that transaction state allows read */ return transReadResponseData(pid->transaction, pbData, uDataSize, puDataRead);} /* End protocolReadResponseData() */Ret_t protocolSetRequestInfo(WspProtocolHandle_t *pid, const XptCommunicationInfo_t *pDoc){ XPTDEBUG(("protocolSetRequestInfo(%lx, %lx)\n", (unsigned long) pid, (unsigned long) pDoc)); return transSetDocInfo(pid->transaction, pDoc);} /* End of protocolSetRequestInfo() */Ret_t protocolGetResponseInfo(WspProtocolHandle_t *pid, XptCommunicationInfo_t *pDoc){ XPTDEBUG(("protocolGetResponseInfo(%lx, %lx)\n", (unsigned long) pid, (unsigned long) pDoc)); return transGetDocInfo(pid->transaction, pDoc);} /* End of protocolGetResponseInfo() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -