⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shppsocket.cpp

📁 本源码为移动公司话费查询中间件TUXEDO使用的实例
💻 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 + -