?? shppsocket.cpp
字號:
/***********************************************
* file name :SHPPSocket.cpp
* date: 2001.07.17
*
***********************************************/
#include <stdio.h>
#include "SHPPSocket.h"
#define nSizeRecv 512
#include "SHPPSession.h"
CSHPPSocket::CSHPPSocket()
{
m_pReadBuf = (char*) malloc(nSizeRecv);
m_unReadBuf = 0;
m_unReadTotal = 0;
m_unBuffer = nSizeRecv;
}
CSHPPSocket::CSHPPSocket(CKNSocket* inSocket)
{
this->m_hSocket = inSocket->m_hSocket;
m_pReadBuf = (char*) malloc(nSizeRecv);
m_unReadBuf = 0;
m_unReadTotal = 0;
m_unBuffer = nSizeRecv;
}
//釋放內存
CSHPPSocket::~CSHPPSocket()
{
if(m_pReadBuf)
{
free((void*)m_pReadBuf);
m_pReadBuf = NULL;
}
}
//每次接收一行請求("\r\n"結束),并copy一行請求
int CSHPPSocket::ReadRtspHeaderLine(char* pchOut, const int nSize, const int nSecs)
{
int nBytesThisTime = m_unReadTotal - m_unReadBuf;
int nLineLength = 0;
char* pch1 , *pch2;
pch1 = m_pReadBuf + m_unReadBuf;
pch2 = NULL;
do {
// look for lf (assume preceded by cr)
if((pch2 = (char*) memchr(pch1 , '\n', nBytesThisTime)) != NULL) {
assert(pch2 > m_pReadBuf);
if(*(pch2 - 1) != '\r')
{
pch1 = ++pch2;
nBytesThisTime = m_unReadTotal -(pch2 - m_pReadBuf +1);
if(nBytesThisTime > 0)
continue; //繼續查找"\r\n"
}else
{
nLineLength = (pch2 - m_pReadBuf - m_unReadBuf) + 1;
if(nLineLength >= nSize) nLineLength = nSize - 1;
//復制一行
memcpy(pchOut, m_pReadBuf + m_unReadBuf, nLineLength); // copy the line to caller
m_unReadBuf += nLineLength;
break;
}
}
//沒找到時,接收請求
pch1 += m_unReadTotal;
nBytesThisTime = Receive(pch1, nSizeRecv - m_unReadBuf, nSecs);
m_unReadTotal += nBytesThisTime;
//內存滿時,重新分配內存空間
if(m_unReadTotal /nSizeRecv * nSizeRecv == m_unReadTotal)
m_pReadBuf = (char*)realloc((void*)m_pReadBuf, nSizeRecv + m_unReadTotal);
}
while(true);
*(pchOut + nLineLength) = '\0';
return nLineLength;
}
//接收剩余的socket字節或在關閉socket前接收剩余的數據
int CSHPPSocket::ReadRtspResponse(char* pchOut, const int nSize, const int nSecs)
{
int nBytesToRead, nBytesThisTime, nBytesRead = 0;
char *pch = m_pReadBuf + m_unReadTotal;
//內存滿時,重新分配內存空間
if((m_unReadTotal + nSize) >= nSizeRecv)
m_pReadBuf = (char*)realloc((void*)m_pReadBuf, nSize + m_unReadTotal +1);
do { // now pass the rest of the data directly to the caller
nBytesToRead = nSize - nBytesRead;
nBytesThisTime = Receive(pch, nBytesToRead, nSecs);
if(nBytesThisTime <= 0) break; // sender closed the socket
m_unReadTotal += nBytesThisTime;
nBytesRead += nBytesThisTime;
}
while(nBytesRead < nSize);
if(pchOut || nBytesRead >0)
{
memcpy((void*)pchOut,(void*)pch,nBytesRead);
// *(pchOut + nBytesRead) = '\0';
}
return nBytesRead;
}
int CSHPPSocket::ReadRtspHeaderLine(char** pchOutRow, const int nSecs/* = 0 */)
{
int nBytesThisTime = m_unReadTotal - m_unReadBuf;
int nLineLength = 0;
// *pchOutRow = m_pReadBuf + m_unReadBuf;//行頭指針
char* pch1 , *pch2;
pch1 = m_pReadBuf + m_unReadBuf;//行頭指針
*pchOutRow = pch1;//m_pReadBuf;//
// pch2 = NULL;
do {
// look for lf (assume preceded by cr)
if((pch2 = (char*) memchr(pch1 , '\n', nBytesThisTime)) != NULL) {
assert((pch2 - pch1) > 0);
if(*(pch2 - 1) != '\r')
{
pch1 = ++pch2;
nBytesThisTime = m_unReadTotal -(pch2 - m_pReadBuf +1);
if(nBytesThisTime > 0)
continue; //繼續查找"\r\n"
}else
{
// nLineLength = (pch2 - m_pReadBuf - m_unReadBuf) + 1;
nLineLength = (pch2 - pch1) + 1;
m_unReadBuf += nLineLength;
break;
}
}
//沒找到時,接收請求
pch1 += nBytesThisTime;
nBytesThisTime = Receive(pch1, nSizeRecv - m_unReadBuf, nSecs);
m_unReadTotal += nBytesThisTime;
//內存滿時,重新分配內存空間
if((m_unBuffer - m_unReadTotal) <= nSizeRecv)
{
m_unBuffer += m_unBuffer;
m_pReadBuf = (char*)realloc((void*)m_pReadBuf, m_unBuffer);
pch1 = m_pReadBuf + m_unReadBuf;//行頭指針
*pchOutRow = pch1;//m_pReadBuf;//
}
}
while(true);
return nLineLength;
// return m_unReadTotal;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -