天天看點

live555峰哥的私房菜(五)-----MyRTSP主函數

本文按照(四)的規劃建立了RTSP伺服器的主函數。

//TcpSocket.h

#ifndef _TCPSOCKET_H__
#define _TCPSOCKET_H__
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netdb.h>      
#include <pthread.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>        
#include <unistd.h>       
#include <dirent.h>       
#include <limits.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <stddef.h>
#include<string>
#include<iostream>

using namespace std;

class CTcpSocket

{
public:
CTcpSocket(void);

virtual ~CTcpSocket(){NULL;};

int tcpCreate(const int port,const char* pszIpStr);

int tcpBind();

int tcpListen(const int iLisNum);

int tcpAccept();

int tcpConnect();

int tcpSend(int iSendFd,const char* pszSenBuf,int iSenBufLen);

int tcpRecv(int iRecFd,char*pszRecBuf,int iRecBufLen);

void tcpClose();

public:
    int iTcpSock;

private:

	sockaddr_in TcpAddr;

	sockaddr_in TcpClientAddr;

	socklen_t iAddLen;

};

#endif


           
//TcpSocket.cpp

#include"TcpSocket.h"

CTcpSocket::CTcpSocket(void)
{
  iTcpSock=0;
}

int CTcpSocket::tcpCreate(const int port,const char* pszIpStr)
{
	   	  bzero(&TcpAddr,sizeof(TcpAddr));
	   	  TcpAddr.sin_family=AF_INET;
		if(pszIpStr!=NULL)
		{
			TcpAddr.sin_addr.s_addr=inet_addr(pszIpStr);
		}

		else
		{
			TcpAddr.sin_addr.s_addr=htonl(INADDR_ANY);
		}

		TcpAddr.sin_port=htons(port);
		iAddLen=sizeof(TcpAddr);
		iTcpSock=socket(AF_INET,SOCK_STREAM,0);
		return iTcpSock;
}

int  CTcpSocket::tcpBind()
{
int iBind=0;
iBind=bind(iTcpSock,(struct sockaddr*)&TcpAddr,sizeof(TcpAddr));
if(-1==iBind) 
{
 cout<<"bind error"<<endl;
exit(-1);
}

return iBind;
}

int CTcpSocket::tcpListen(const int iLisNUm)
{
return listen(iTcpSock,iLisNUm);
}

int CTcpSocket::tcpAccept()
{
	int iAccpet=0;
	iAddLen=sizeof(struct sockaddr_in);
	iAccpet=accept(iTcpSock,(struct sockaddr*)&TcpClientAddr,&iAddLen);
	if(-1==iAccpet)
	{
	cout<<"accpet error!"<<endl;
	exit(1);
	}
return iAccpet;
}

int CTcpSocket::tcpConnect()
{
int iConnect=connect(iTcpSock,(struct sockaddr*)&TcpAddr,iAddLen);
if(-1==iConnect)
{
cout<<"connect error"<<endl;
exit(-1);
}

return iConnect;
}

 
int CTcpSocket::tcpSend(int iSendFd,const char*pszSenBuf,int iSenBufLen)

{
int iSendNum=0;
iSendNum=write(iSendFd,pszSenBuf,iSenBufLen);
if(iSendNum!=iSenBufLen)
cout<<"sendNum!=senBufLen"<<endl;
return iSendNum;
}

int CTcpSocket::tcpRecv(int iRecFd,char* pszRecBuf,int iRecBufLen)
{
int iRecNum=0;
iRecNum=read(iRecFd,pszRecBuf,iRecBufLen);
return iRecNum;
}

void CTcpSocket::tcpClose()
{
close(iTcpSock);
}


           
//MyRtsp.h
#include "TcpSocket.h"

#define TURE 1
           

//MyRtsp.cpp

#include"MyRtsp.h"

void* RtspHandleFun(void *Fd);


int main()
{
	CTcpSocket TcpServer;
	TcpServer.tcpCreate(8554,NULL);
	TcpServer.tcpBind();
	TcpServer.tcpListen(10);
	int iClientSocketFd=0;
	int iPthreadNum=0;
	pthread_t a_thread[300];

	
	while(TURE)
	{
		iClientSocketFd=TcpServer.tcpAccept();
		//建立分線程
		pthread_create(&a_thread[iPthreadNum++],NULL,RtspHandleFun,&iClientSocketFd);
	}

	return 0;
}

void* RtspHandleFun(void *Fd)
{
	int iClientFd=*(int *)Fd;
	char pszBuffer[512]={0};	
	char pszParseBuffer[128]={0};	


	CRtspClientSession RtspClientSession;
	int iRunning=1;

while(iRunning)
{
	read(iClientFd,pszBuffer,sizeof(pszBuffer));
	printf("%s\n",pszBuffer);

	int iRet=RtspClientSession.ParseRtspRequestString(pszBuffer,pszParseBuffer);

//"OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, GET_PARAMETER, SET_PARAMETER";
	switch(iRet)
	{
		case 1:
			pszBuffer=RtspClientSession.handleCmdOptions(pszParseBuffer);
			break;
		case 2:
			pszBuffer=RtspClientSession.handleCmdDescribe(pszParseBuffer);
			break;
		case 3:
			pszBuffer=RtspClientSession.handleCmdSetup(pszParseBuffer);
			break;
		case 4:
			pszBuffer=RtspClientSession.handleCmdTeardown(pszParseBuffer);
			break;
		case 5:
			pszBuffer=RtspClientSession.handleCmdPlay(pszParseBuffer);
			break;
		case 6:
			pszBuffer=RtspClientSession.handleCmdPause(pszParseBuffer);
			break;
		case 7:
			pszBuffer=RtspClientSession.handleCmdGetParameter(pszParseBuffer);
			break;
		case 8:
			pszBuffer=RtspClientSession.handleCmdSetParameter(pszParseBuffer);
			break;		
		default:
			printf("error,unknown methods!!\n");
			iRunning=0;
			break;
	}
	
	write(iClientFd,pszBuffer,sizeof(pszBuffer));
	
}
	
}