/* 发送HTTP+json报文,决定是否关闭当前Session HTTP部分构造,json部分由函数传递 */ QTSS_Error CServiceSession::SendHTTPPacket(StrPtrLen* contentXML, Bool16 connectionClose, Bool16 decrement) { //构造响应报文(HTTP头) HTTPRequest httpAck(&sServiceStr); httpAck.CreateResponseHeader(contentXML->Len?httpOK:httpNotImplemented); if (contentXML->Len) httpAck.AppendContentLengthHeader(contentXML->Len); if(connectionClose) httpAck.AppendConnectionCloseHeader(); char respHeader[2048] = { 0 }; StrPtrLen* ackPtr = httpAck.GetCompleteResponseHeader(); strncpy(respHeader,ackPtr->Ptr, ackPtr->Len); BaseResponseStream *pOutputStream = GetOutputStream(); pOutputStream->Put(respHeader); if (contentXML->Len > 0) pOutputStream->Put(contentXML->Ptr, contentXML->Len); if (pOutputStream->GetBytesWritten() != 0) { pOutputStream->Flush(); } //将对CServiceSession的引用减少一 if(fObjectHolders && decrement) DecrementObjectHolderCount(); if(connectionClose) this->Signal(Task::kKillEvent); return QTSS_NoErr; }
QTSS_Error EasyCMSSession::processMessage() { if (NULL == fRequest) return QTSS_BadArgument; QTSS_Error theErr = fRequest->Parse(); if (theErr != QTSS_NoErr) return QTSS_BadArgument; //获取具体Content json数据部分 StrPtrLen* lengthPtr = fRequest->GetHeaderValue(httpContentLengthHeader); StringParser theContentLenParser(lengthPtr); theContentLenParser.ConsumeWhitespace(); UInt32 content_length = theContentLenParser.ConsumeInteger(NULL); if (content_length) { qtss_printf("EasyCMSSession::ProcessMessage read content-length:%lu \n", content_length); // 检查content的fContentBuffer和fContentBufferOffset是否有值存在,如果存在,说明我们已经开始 // 进行content请求处理,如果不存在,我们需要创建并初始化fContentBuffer和fContentBufferOffset if (fContentBuffer == NULL) { fContentBuffer = NEW char[content_length + 1]; memset(fContentBuffer, 0, content_length + 1); fContentBufferOffset = 0; } UInt32 theLen = 0; // 读取HTTP Content报文数据 theErr = fInputStream.Read(fContentBuffer + fContentBufferOffset, content_length - fContentBufferOffset, &theLen); Assert(theErr != QTSS_BadArgument); if (theErr == QTSS_RequestFailed) { OSCharArrayDeleter charArrayPathDeleter(fContentBuffer); fContentBufferOffset = 0; fContentBuffer = NULL; return QTSS_RequestFailed; } qtss_printf("EasyCMSSession::ProcessMessage() Add Len:%lu \n", theLen); if ((theErr == QTSS_WouldBlock) || (theLen < (content_length - fContentBufferOffset))) { // // Update our offset in the buffer fContentBufferOffset += theLen; Assert(theErr == QTSS_NoErr); return QTSS_WouldBlock; } Assert(theErr == QTSS_NoErr); // 处理完成报文后会自动进行Delete处理 OSCharArrayDeleter charArrayPathDeleter(fContentBuffer); qtss_printf("EasyCMSSession::ProcessMessage() Get Complete Msg:\n%s", fContentBuffer); fNoneACKMsgCount = 0; EasyProtocol protocol(fContentBuffer); int nNetMsg = protocol.GetMessageType(); switch (nNetMsg) { case MSG_SD_REGISTER_ACK: { EasyMsgSDRegisterACK ack(fContentBuffer); qtss_printf("session id = %s\n", ack.GetBodyValue(EASY_TAG_SESSION_ID).c_str()); qtss_printf("device serial = %s\n", ack.GetBodyValue(EASY_TAG_SERIAL).c_str()); } break; case MSG_SD_POST_SNAP_ACK: { ; } break; case MSG_SD_PUSH_STREAM_REQ: { EasyMsgSDPushStreamREQ startStreamReq(fContentBuffer); string serial = startStreamReq.GetBodyValue(EASY_TAG_SERIAL); string ip = startStreamReq.GetBodyValue(EASY_TAG_SERVER_IP); string port = startStreamReq.GetBodyValue(EASY_TAG_SERVER_PORT); string protocol = startStreamReq.GetBodyValue(EASY_TAG_PROTOCOL); string channel = startStreamReq.GetBodyValue(EASY_TAG_CHANNEL); string streamID = startStreamReq.GetBodyValue(EASY_TAG_STREAM_ID); string reserve = startStreamReq.GetBodyValue(EASY_TAG_RESERVE); qtss_printf("Serial = %s\n", serial.c_str()); qtss_printf("Server_IP = %s\n", ip.c_str()); qtss_printf("Server_Port = %s\n", port.c_str()); //TODO::这里需要对传入的Serial/StreamID/Channel做一下容错处理 if (serial.empty() || ip.empty() || port.empty()) { return QTSS_ValueNotFound; } QTSS_RoleParams params; params.startStreaParams.inIP = ip.c_str(); params.startStreaParams.inPort = atoi(port.c_str()); params.startStreaParams.inSerial = serial.c_str(); params.startStreaParams.inProtocol = protocol.c_str(); params.startStreaParams.inChannel = channel.c_str(); params.startStreaParams.inStreamID = streamID.c_str(); QTSS_Error errCode = QTSS_NoErr; UInt32 fCurrentModule = 0; UInt32 numModules = QTSServerInterface::GetNumModulesInRole(QTSSModule::kStartStreamRole); for (; fCurrentModule < numModules; ++fCurrentModule) { QTSSModule* theModule = QTSServerInterface::GetModule(QTSSModule::kStartStreamRole, fCurrentModule); errCode = theModule->CallDispatch(Easy_StartStream_Role, ¶ms); } fCurrentModule = 0; EasyJsonValue body; body[EASY_TAG_SERIAL] = params.startStreaParams.inSerial; body[EASY_TAG_CHANNEL] = params.startStreaParams.inChannel; body[EASY_TAG_PROTOCOL] = params.startStreaParams.inProtocol; body[EASY_TAG_SERVER_IP] = params.startStreaParams.inIP; body[EASY_TAG_SERVER_PORT] = params.startStreaParams.inPort; body[EASY_TAG_RESERVE] = reserve; EasyMsgDSPushSteamACK rsp(body, startStreamReq.GetMsgCSeq(), getStatusNo(errCode)); string msg = rsp.GetMsg(); StrPtrLen jsonContent((char*)msg.data()); HTTPRequest httpAck(&QTSServerInterface::GetServerHeader(), httpResponseType); if (httpAck.CreateResponseHeader()) { if (jsonContent.Len) httpAck.AppendContentLengthHeader(jsonContent.Len); //Push msg to OutputBuffer char respHeader[2048] = { 0 }; StrPtrLen* ackPtr = httpAck.GetCompleteHTTPHeader(); strncpy(respHeader, ackPtr->Ptr, ackPtr->Len); fOutputStream.Put(respHeader); if (jsonContent.Len > 0) fOutputStream.Put(jsonContent.Ptr, jsonContent.Len); } } break; case MSG_SD_STREAM_STOP_REQ: { EasyMsgSDStopStreamREQ stopStreamReq(fContentBuffer); QTSS_RoleParams params; string serial = stopStreamReq.GetBodyValue(EASY_TAG_SERIAL); params.stopStreamParams.inSerial = serial.c_str(); string protocol = stopStreamReq.GetBodyValue(EASY_TAG_PROTOCOL); params.stopStreamParams.inProtocol = protocol.c_str(); string channel = stopStreamReq.GetBodyValue(EASY_TAG_CHANNEL); params.stopStreamParams.inChannel = channel.c_str(); QTSS_Error errCode = QTSS_NoErr; UInt32 fCurrentModule = 0; UInt32 numModules = QTSServerInterface::GetNumModulesInRole(QTSSModule::kStopStreamRole); for (; fCurrentModule < numModules; ++fCurrentModule) { QTSSModule* theModule = QTSServerInterface::GetModule(QTSSModule::kStopStreamRole, fCurrentModule); errCode = theModule->CallDispatch(Easy_StopStream_Role, ¶ms); } fCurrentModule = 0; EasyJsonValue body; body[EASY_TAG_SERIAL] = params.stopStreamParams.inSerial; body[EASY_TAG_CHANNEL] = params.stopStreamParams.inChannel; body[EASY_TAG_PROTOCOL] = params.stopStreamParams.inProtocol; EasyMsgDSStopStreamACK rsp(body, stopStreamReq.GetMsgCSeq(), getStatusNo(errCode)); string msg = rsp.GetMsg(); //回应 StrPtrLen jsonContent((char*)msg.data()); HTTPRequest httpAck(&QTSServerInterface::GetServerHeader(), httpResponseType); if (httpAck.CreateResponseHeader()) { if (jsonContent.Len) httpAck.AppendContentLengthHeader(jsonContent.Len); //Push msg to OutputBuffer char respHeader[2048] = { 0 }; StrPtrLen* ackPtr = httpAck.GetCompleteHTTPHeader(); strncpy(respHeader, ackPtr->Ptr, ackPtr->Len); fOutputStream.Put(respHeader); if (jsonContent.Len > 0) fOutputStream.Put(jsonContent.Ptr, jsonContent.Len); } } break; default: break; } }