void SDPContainer::PrintLine(SInt32 lineIndex) { StrPtrLen *printLinePtr = GetLine(lineIndex); if (printLinePtr) { printLinePtr->PrintStr(); qtss_printf("\n"); } }
QTSS_Error RTSPRequestStream::ReadRequest() { while (true) { UInt32 newOffset = 0; //If this is the case, we already HAVE a request on this session, and we now are done //with the request and want to move onto the next one. The first thing we should do //is check whether there is any lingering data in the stream. If there is, the parent //session believes that is part of a new request if (fRequestPtr != NULL) { fRequestPtr = NULL;//flag that we no longer have a complete request // Take all the retreated leftover data and move it to the beginning of the buffer if ((fRetreatBytes > 0) && (fRequest.Len > 0)) ::memmove(fRequest.Ptr, fRequest.Ptr + fRequest.Len + fRetreatBytesRead, fRetreatBytes); // if we are decoding, we need to also move over the remaining encoded bytes // to the right position in the fRequestBuffer if (fEncodedBytesRemaining > 0) { //Assert(fEncodedBytesRemaining < 4); // The right position is at fRetreatBytes offset in the request buffer. The reason for this is: // 1) We need to find a place in the request buffer where we know we have enough space to store // fEncodedBytesRemaining. fRetreatBytes + fEncodedBytesRemaining will always be less than // kRequestBufferSize because all this data must have been in the same request buffer, together, at one point. // // 2) We need to make sure that there is always more data in the RequestBuffer than in the decoded // request buffer, otherwise we could overrun the decoded request buffer (we bounds check on the encoded // buffer, not the decoded buffer). Leaving fRetreatBytes as empty space in the request buffer ensures // that this principle is maintained. ::memmove(&fRequestBuffer[fRetreatBytes], &fRequestBuffer[fCurOffset - fEncodedBytesRemaining], fEncodedBytesRemaining); fCurOffset = fRetreatBytes + fEncodedBytesRemaining; Assert(fCurOffset < kRequestBufferSizeInBytes); } else fCurOffset = fRetreatBytes; newOffset = fRequest.Len = fRetreatBytes; fRetreatBytes = fRetreatBytesRead = 0; } // We don't have any new data, so try and get some if (newOffset == 0) { if (fRetreatBytes > 0) { // This will be true if we've just snarfed another input stream, in which case the encoded data // is copied into our request buffer, and its length is tracked in fRetreatBytes. // If this is true, just fall through and decode the data. newOffset = fRetreatBytes; fRetreatBytes = 0; Assert(fEncodedBytesRemaining == 0); } else { // We don't have any new data, get some from the socket... QTSS_Error sockErr = fSocket->Read(&fRequestBuffer[fCurOffset], (kRequestBufferSizeInBytes - fCurOffset) - 1, &newOffset); //assume the client is dead if we get an error back if (sockErr == EAGAIN) return QTSS_NoErr; if (sockErr != QTSS_NoErr) { Assert(!fSocket->IsConnected()); return sockErr; } } if (fDecode) { // If we need to decode this data, do it now. Assert(fCurOffset >= fEncodedBytesRemaining); QTSS_Error decodeErr = this->DecodeIncomingData(&fRequestBuffer[fCurOffset - fEncodedBytesRemaining], newOffset + fEncodedBytesRemaining); // If the above function returns an error, it is because we've // encountered some non-base64 data in the stream. We can process // everything up until that point, but all data after this point will // be ignored. if (decodeErr == QTSS_NoErr) Assert(fEncodedBytesRemaining < 4); } else fRequest.Len += newOffset; Assert(fRequest.Len < kRequestBufferSizeInBytes); fCurOffset += newOffset; } Assert(newOffset > 0); // See if this is an interleaved data packet if ('$' == *(fRequest.Ptr)) { if (fRequest.Len < 4) continue; UInt16* dataLenP = (UInt16*)fRequest.Ptr; UInt32 interleavedPacketLen = ntohs(dataLenP[1]) + 4; if (interleavedPacketLen > fRequest.Len) continue; //put back any data that is not part of the header fRetreatBytes += fRequest.Len - interleavedPacketLen; fRequest.Len = interleavedPacketLen; fRequestPtr = &fRequest; fIsDataPacket = true; return QTSS_RequestArrived; } fIsDataPacket = false; if (fPrintRTSP) { DateBuffer theDate; DateTranslator::UpdateDateBuffer(&theDate, 0); // get the current GMT date and time qtss_printf("\n\n#C->S:\n#time: ms=%lu date=%s\n", (UInt32) OS::StartTimeMilli_Int(), theDate.GetDateBuffer()); if (fSocket != NULL) { UInt16 serverPort = fSocket->GetLocalPort(); UInt16 clientPort = fSocket->GetRemotePort(); StrPtrLen* theLocalAddrStr = fSocket->GetLocalAddrStr(); StrPtrLen* theRemoteAddrStr = fSocket->GetRemoteAddrStr(); if (theLocalAddrStr != NULL) { qtss_printf("#server: ip="); theLocalAddrStr->PrintStr(); qtss_printf(" port=%u\n" , serverPort ); } else { qtss_printf("#server: ip=NULL port=%u\n" , serverPort ); } if (theRemoteAddrStr != NULL) { qtss_printf("#client: ip="); theRemoteAddrStr->PrintStr(); qtss_printf(" port=%u\n" , clientPort ); } else { qtss_printf("#client: ip=NULL port=%u\n" , clientPort ); } } StrPtrLen str(fRequest); str.PrintStrEOL("\n\r\n", "\n");// print the request but stop on \n\r\n and add a \n afterwards. } //use a StringParser object to search for a double EOL, which signifies the end of //the header. Bool16 weAreDone = false; StringParser headerParser(&fRequest); UInt16 lcount = 0; while (headerParser.GetThruEOL(NULL)) { lcount++; if (headerParser.ExpectEOL()) { //The legal end-of-header sequences are \r\r, \r\n\r\n, & \n\n. NOT \r\n\r! //If the packets arrive just a certain way, we could get here with the latter //combo, and not wait for a final \n. if ((headerParser.GetDataParsedLen() > 2) && (memcmp(headerParser.GetCurrentPosition() - 3, "\r\n\r", 3) == 0)) continue; weAreDone = true; break; } else if (lcount == 1) { // if this request is actually a ShoutCast password it will be // in the form of "xxxxxx\r" where "xxxxx" is the password. // If we get a 1st request line ending in \r with no blanks we will // assume that this is the end of the request. UInt16 flag = 0; UInt16 i = 0; for (i=0; i<fRequest.Len; i++) { if (fRequest.Ptr[i] == ' ') flag++; } if (flag == 0) { weAreDone = true; break; } } } //weAreDone means we have gotten a full request if (weAreDone) { //put back any data that is not part of the header fRequest.Len -= headerParser.GetDataRemaining(); fRetreatBytes += headerParser.GetDataRemaining(); fRequestPtr = &fRequest; return QTSS_RequestArrived; } //check for a full buffer if (fCurOffset == kRequestBufferSizeInBytes - 1) { fRequestPtr = &fRequest; return E2BIG; } } }
/* 从RTSP Request stream中获取full RTSP Request Header,打印fRequest相关信息,解析得到它的尾部,并配置相应数据成员的值 */ QTSS_Error RTSPRequestStream::ReadRequest() { while (true) { /* 注意这个量非常重要 */ UInt32 newOffset = 0; //If this is the case, we already HAVE a request on this session, and we now are done //with the request and want to move onto the next one. The first thing we should do //is check whether there is any lingering(延迟的) data in the stream. If there is, the parent //session believes that is part of a new request if (fRequestPtr != NULL) { /* 标记不再有complete RTSP client Request */ fRequestPtr = NULL;//flag that we no longer have a complete request // Take all the retreated leftover(剩余的) data and move it to the beginning of the buffer if ((fRetreatBytes > 0) && (fRequest.Len > 0)) ::memmove(fRequest.Ptr, fRequest.Ptr + fRequest.Len + fRetreatBytesRead/* NOTE! */, fRetreatBytes); // if we are decoding, we need to also move over the remaining encoded bytes // to the right position in the fRequestBuffer if (fEncodedBytesRemaining > 0) { /* 参见RTSPRequestStream::DecodeIncomingData() */ //Assert(fEncodedBytesRemaining < 4); // The right position is at fRetreatBytes offset in the request buffer. The reason for this is: // 1) We need to find a place in the request buffer where we know we have enough space to store // fEncodedBytesRemaining. fRetreatBytes + fEncodedBytesRemaining will always be less than // kRequestBufferSize because all this data must have been in the same request buffer, together, at one point. // // 2) We need to make sure that there is always more data in the RequestBuffer than in the decoded // request buffer, otherwise we could overrun(超出限度) the decoded request buffer (we bounds check on the encoded // buffer, not the decoded buffer). Leaving fRetreatBytes as empty space in the request buffer ensures // that this principle is maintained. ::memmove(&fRequestBuffer[fRetreatBytes], &fRequestBuffer[fCurOffset - fEncodedBytesRemaining], fEncodedBytesRemaining); fCurOffset = fRetreatBytes + fEncodedBytesRemaining; Assert(fCurOffset < kRequestBufferSizeInBytes); } else fCurOffset = fRetreatBytes; newOffset = fRequest.Len = fRetreatBytes; /* 因为已将Retreat data移位了 */ fRetreatBytes = fRetreatBytesRead = 0; } // We don't have any new data, so try and get some /* 以下操作是确保newOffset>0 */ if (newOffset == 0) { if (fRetreatBytes > 0) { // This will be true if we've just snarfed(复制) another input stream, in which case the encoded data // is copied into our request buffer, and its length is tracked(追踪) in fRetreatBytes. // If this is true, just fall through(进行下去) and decode the data. newOffset = fRetreatBytes; fRetreatBytes = 0; Assert(fEncodedBytesRemaining == 0); } else { // We don't have any new data, get some from the socket... /* 利用TCPSocket的::recv()来接收数据,第一,二个参数是缓存地址和长度,第三个参数是接收数据的长度 */ QTSS_Error sockErr = fSocket->Read(&fRequestBuffer[fCurOffset], (kRequestBufferSizeInBytes - fCurOffset) - 1, &newOffset); //assume the client is dead if we get an error back /* 目前缓冲区无数据可读 */ /* 当recv系统调用返回这个值时表示recv读数据时,对方没有发送数据过来。 */ if (sockErr == EAGAIN) return QTSS_NoErr; /* 假如返回出错,一定是连接断开 */ if (sockErr != QTSS_NoErr) { Assert(!fSocket->IsConnected()); return sockErr; } } /* 当要base64 decode时 */ if (fDecode) { // If we need to decode this data, do it now. /* 确保数据偏移量大于内存中需要decode的数据 */ Assert(fCurOffset >= fEncodedBytesRemaining); /* 解密读进来的数据 */ QTSS_Error decodeErr = this->DecodeIncomingData(&fRequestBuffer[fCurOffset - fEncodedBytesRemaining], newOffset + fEncodedBytesRemaining); // If the above function returns an error, it is because we've // encountered some non-base64 data in the stream. We can process // everything up until that point, but all data after this point will // be ignored. /* 解码成功时,确保最后剩下的未解码的数据少于4个字节,参见下面的RTSPRequestStream::DecodeIncomingData() */ if (decodeErr == QTSS_NoErr) Assert(fEncodedBytesRemaining < 4); } else fRequest.Len += newOffset; Assert(fRequest.Len < kRequestBufferSizeInBytes); fCurOffset += newOffset; } /* 最后一定要达到这个结论! */ Assert(newOffset > 0); // See if this is an interleaved data packet /* 查找data packet,配置fIsDataPacket的值 */ /* 找到data packet */ if ('$' == *(fRequest.Ptr)) { if (fRequest.Len < 4) continue; UInt16* dataLenP = (UInt16*)fRequest.Ptr; /* 获取混叠包长度 */ UInt32 interleavedPacketLen = ntohs(dataLenP[1]) + 4; if (interleavedPacketLen > fRequest.Len) continue; //put back any data that is not part of the header /* 将不是header部分的数据放回失控数据处 */ fRetreatBytes += fRequest.Len - interleavedPacketLen; fRequest.Len = interleavedPacketLen; fRequestPtr = &fRequest; fIsDataPacket = true; return QTSS_RequestArrived; } fIsDataPacket = false; /* 当打印RTSP info时,打印出如下信息: (空两行) #C->S; #time: ms=*** data=Mon,29 July 2009 15:17:17 GTM #server: ip=172.16.32.37 port=*** 或#server: ip=NULL port=*** #client: ip=172.16.39.30 port=*** 或#client: ip=NULL port=*** *********fRequest info*************** */ if (fPrintRTSP) { /* 类似RTSPResponseStream::WriteV() */ /* 得到当前的GTM时间,格式为"Mon, 04 Nov 1996 21:42:17 GMT" */ DateBuffer theDate; DateTranslator::UpdateDateBuffer(&theDate, 0); // get the current GMT date and time /* OS::StartTimeMilli_Int()表示服务器运行多长时间? */ qtss_printf("\n\n#C->S:\n#time: ms=%lu date=%s\n", (UInt32) OS::StartTimeMilli_Int(), theDate.GetDateBuffer()); /* 假如有TCPSocket存在,就获取并打印其相关信息 */ if (fSocket != NULL) { UInt16 serverPort = fSocket->GetLocalPort(); UInt16 clientPort = fSocket->GetRemotePort(); StrPtrLen* theLocalAddrStr = fSocket->GetLocalAddrStr(); StrPtrLen* theRemoteAddrStr = fSocket->GetRemoteAddrStr(); if (theLocalAddrStr != NULL) { qtss_printf("#server: ip="); theLocalAddrStr->PrintStr(); qtss_printf(" port=%u\n" , serverPort ); } else { qtss_printf("#server: ip=NULL port=%u\n" , serverPort ); } if (theRemoteAddrStr != NULL) { qtss_printf("#client: ip="); theRemoteAddrStr->PrintStr(); qtss_printf(" port=%u\n" , clientPort ); } else { qtss_printf("#client: ip=NULL port=%u\n" , clientPort ); } } /* 打印fRequest字符串 */ StrPtrLen str(fRequest); str.PrintStrEOL("\n\r\n", "\n");// print the request but stop on \n\r\n and add a \n afterwards. } //use a StringParser object to search for a double EOL, which signifies the end of //the header. /* 下面要查找client发出的RTSP Request Header的末尾处 */ /* 找到fRequest末端了吗? */ Bool16 weAreDone = false; StringParser headerParser(&fRequest); UInt16 lcount = 0; /* 当遇到eol时,fStartGet指针越过它 */ while (headerParser.GetThruEOL(NULL)) { lcount++; /* 当当前的fStartGet指针指向EOL时 */ if (headerParser.ExpectEOL()) { //The legal end-of-header sequences are \r\r, \r\n\r\n, & \n\n. NOT \r\n\r! //If the packets arrive just a certain way, we could get here with the latter //combo(组合,即\r\n\r), and not wait for a final \n. /* 假如查找到"\r\n\r",就继续查找,直至找到符合条件的末端 */ if ((headerParser.GetDataParsedLen() > 2) && (memcmp(headerParser.GetCurrentPosition() - 3, "\r\n\r", 3) == 0)) continue; /* 标记找到了 */ weAreDone = true; break; } /* 假如是"xxxxxx\r" */ else if (lcount == 1) { // if this request is actually a ShoutCast password it will be // in the form of "xxxxxx\r" where "xxxxx" is the password. // If we get a 1st request line ending in \r with no blanks we will // assume that this is the end of the request. UInt16 flag = 0; UInt16 i = 0; /* 检查"xxxxxx\r"中有无空格?没有就说明我们找到了 */ for (i=0; i<fRequest.Len; i++) { if (fRequest.Ptr[i] == ' ') flag++; } if (flag == 0) { weAreDone = true; break; } } } //weAreDone means we have gotten a full request /* 我们找到了一个Full RTSP Request Header? */ if (weAreDone) { //put back any data that is not part of the header fRequest.Len -= headerParser.GetDataRemaining(); fRetreatBytes += headerParser.GetDataRemaining(); fRequestPtr = &fRequest; return QTSS_RequestArrived; } //check for a full buffer /* 当到达Request buffer末端时,给出提示信息E2BIG */ if (fCurOffset == kRequestBufferSizeInBytes - 1) { fRequestPtr = &fRequest; return E2BIG; } } }