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

📄 cmediasocketserver.cpp

📁 WINCE下的流媒体播放器服务端
💻 CPP
字号:
/*=========================================================================== 
CMediaSocketServer.cpp
Implement of Server Communication Socket
===========================================================================*/

#include "stdafx.h"
#include "CMediaSocketServer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////////////
CMediaSocketServer::CMediaSocketServer()
{
	m_strSourceFile = "";
	m_bReadyToSend  = false;
	m_lBytesSent    = 0;
}

CMediaSocketServer::~CMediaSocketServer()
{
	m_bSending   = false;
	m_bReceiving = false;
}

void CMediaSocketServer::SetSourceFile( const char * inPath )
{
	bool   bBackup = m_bReadyToSend;
	if ( m_strSourceFile != "" )
	{
		// Pause the sending thread
		m_bReadyToSend = false;
		Sleep( 100 );
		m_objFile.Close();
	}
	m_strSourceFile = inPath;
	// Open source file successfully
	if ( m_objFile.Open( m_strSourceFile, CFile::modeRead | CFile::typeBinary ) )
	{
		m_lBytesSent    = 0;
		m_bReadyToSend  = bBackup;
	}
}

// Send the source file data once again!!!
void CMediaSocketServer::SendAtOnce(void)
{
	m_lBytesSent   = 0;
	m_bReadyToSend = true;
	// If sending thread has not been started, start it at once
	StartSending();
}

// Receiving blockingly
void CMediaSocketServer::ReceivingLoop( void )
{
	int    nret     = 0;
	int    nMsgType = 0;
	int    nDataLen = 0;
	char   buff[sizeof( MSG_HEADER ) + MPEG1_PACK];

	while ( m_bReceiving )
	{
		// Receive the message header
		while ( ( nret = Receive( buff, sizeof( MSG_HEADER ) ) ) == E_SOCKET_NOT_READY )
		{
			Sleep( 100 );
		}
		// When errors occur or socket closed, terminate this loop
		if ( nret == E_SOCKET_FAIL || nret == E_SOCKET_CLOSE )
			break;

		// See what type the payload is...
		PMSG_HEADER  pMsg = ( PMSG_HEADER ) buff;
		nMsgType = pMsg->nMsgType;   // Payload type retrieved
		nDataLen = pMsg->nDataSize;  // Payload size retrieved
		// Continue to receive the payload
		if ( nDataLen > 0 )
		{
			while ( ( nret = Receive( buff, nDataLen ) ) == E_SOCKET_NOT_READY )
			{
				Sleep( 100 );
			}
			// When errors occur or socket closed, terminate this loop
			if ( nret == E_SOCKET_FAIL || nret == E_SOCKET_CLOSE )
			{
				break;
			}
		}

		// Data processing
		switch ( nMsgType )
		{
		case DATA_REQUEST: // The remote request to data sending
			if ( !m_bReadyToSend )
			{
				SendAtOnce();
			}
			break;

		case DATA_REFUSED: // Stop current data sending
			if ( m_bReadyToSend )
			{
				m_bReadyToSend = false;
			}
			break;

		case RECV_EXIT_REQUEST: // Send message to stop client receiving
			{
			char pData[ sizeof( MSG_HEADER ) ];
			PMSG_HEADER pMsg = ( PMSG_HEADER ) pData;
			pMsg->nDataSize  = 0;
			pMsg->nMsgType   = RECV_EXIT;
			Send( pData, sizeof( MSG_HEADER ) );
			break;
			}

		case DISCONNECT_REQUEST: // The remote request disconnect
			// Stop sending thread first
			StopSending();
			// Close the socket connection
			if ( m_hSocket )
			{
				closesocket( m_hSocket );
				m_hSocket = NULL;
			}
			return ; // Terminate this receiving thread

		default:
			break;
		}
	}
}

// Sending cycle
void CMediaSocketServer::SendingLoop( void )
{
//	CMediaSocketServer * nDisConnect = NULL;
	int       nRead    = 0;
	const int nMsgSize = sizeof( MSG_HEADER ) + MPEG1_PACK;
	char *    pBuf     = new char[ nMsgSize ];
	// Make message header
	PMSG_HEADER  pMsg = ( PMSG_HEADER ) pBuf;
	pMsg->nMsgType  = DATA_MEDIA;
	pMsg->nDataSize = MPEG1_PACK;

	while ( m_bSending )
	{
		// Not ready to send just this time!!!
		if ( !m_bReadyToSend )
		{
			Sleep( 100 );
			continue;
		}

		// Read from file and send a pack every time
		if ( m_objFile.Seek( m_lBytesSent, CFile::begin ) == m_lBytesSent )
		{
			nRead = m_objFile.Read( pBuf + sizeof( MSG_HEADER ), MPEG1_PACK );
			// Maybe meet the end of file
			if ( nRead < MPEG1_PACK )
			{
				// Modify the payload's size
				pMsg->nDataSize = nRead;
				Send( pBuf, sizeof( MSG_HEADER ) + nRead );
				m_lBytesSent  += nRead;
				m_bReadyToSend = false;
//				nDisConnect = new CMediaSocketServer;
//				nDisConnect->Disconnect();
				// Reset to send again
			    // m_lBytesSent = 0;
			}
			else
			{
				Send( pBuf, nMsgSize );
				// Don't send data too fast!!!
				Sleep(20);
				m_lBytesSent += MPEG1_PACK;
			}
		}
		else
		{
			// Maybe meet the end of file
			m_bReadyToSend = false;
		}
	}
    delete []pBuf;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -