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

📄 vnetservice.cpp

📁 otl简单包装实现类,对数据库进行操作的,简单易用.
💻 CPP
字号:
/***************************************************************************                          VNetService.cpp -  description                             -------------------    begin                : 11.21 10:18:00 CST 2003    copyright            : (C) 2003 by |LiuZhong|    email                : |zliu@foundermn.com| ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/ #include "vnetservice.h"//--------------------------------------------------------------------------------------------------------------------------------------------------------VNetService::VNetService(){	m_Address = "127.0.0.1";	m_Port = 0;	m_bReUseAddr = true;	m_bNoDelay = false;	m_bLingerOn = false;	m_LingerSec = 0;	m_SendBufLen = 0;	m_RecvBufLen = 0;	m_ListenNum = 1024;	m_bService = false;	}VNetService::~VNetService(){	StopService();}void* VNetService::ServiceThread(void* pArg){	pthread_detach(pthread_self());	((TArg*)pArg)->m_Self->ClientService(((TArg*)pArg)->m_pAny);	delete (TArg*)pArg;	return NULL;	}////////////////////////////////////////////////////////////////////////////////////////////////////////void VNetService::SetReUseAddr(bool bReUseAddr){ 	m_bReUseAddr = bReUseAddr; }void VNetService::SetTcpNoDelay(bool bNoDelay){	m_bNoDelay = bNoDelay;}void VNetService::SetRecvBuf(int RecvBufLen){	if(RecvBufLen>0) m_RecvBufLen = RecvBufLen;}void VNetService::SetSendBuf(int SendBufLen){	if(SendBufLen>0) m_SendBufLen = SendBufLen;}void VNetService::SetLinger(bool bOn,int LingerSec){	if(bOn){		m_bLingerOn = bOn;		m_LingerSec = LingerSec;	}	else{		m_bLingerOn = false;		m_LingerSec = 0;	}	}void VNetService::SetListenNum(int Num){	if(Num>0) m_ListenNum = Num;}/////////////////////////////////////////////////////////////////////////////////////////////////////////int  VNetService::StartService(const char* Address,int Port){	int Val = m_ServiceRs.Create(SOCK_STREAM);	if(m_bNoDelay){		m_ServiceRs.SetTcpNoDelay(m_bNoDelay);	}	if(m_bReUseAddr){		m_ServiceRs.SetReUseAddr(m_bReUseAddr);	}    if(m_bLingerOn){    	m_ServiceRs.SetLinger(1,m_LingerSec);    }        if(m_SendBufLen>0){    	m_ServiceRs.SetSendBufSize(m_SendBufLen);    }        if(m_RecvBufLen>0){    	m_ServiceRs.SetRecvBufSize(m_RecvBufLen);    }        m_Address = Address;    m_Port = Port;    Val = m_ServiceRs.Bind((char*)Address,Port);	if(Val!=RS_OK){	    return -1;	}		Val = m_ServiceRs.Listen(m_ListenNum);	if(Val!=RS_OK){		return -2;	}		m_bService = true;	return 0;}int  VNetService::StopService(){	m_bService = false;	m_ServiceRs.Close();	return 0;}int  VNetService::SelectWait(long SEvent,long MSec){	return m_ServiceRs.SelectWait(SEvent,MSec);	}int  VNetService::GetNewClient(CRSSocket** ClientRs){	int Val = -1;	*ClientRs = NULL;	CRSSocket *newrs = new CRSSocket();	int s = m_ServiceRs.Accept (newrs);  	if(s>=0){		*ClientRs = newrs;		Val = 0;	}	else{		newrs->Close();		delete newrs;	}	return Val;}int  VNetService::DoService(int MSec){	while(m_bService){		int sw = m_ServiceRs.SelectWait(FD_READ,MSec);		if(sw==FD_READ){			CRSSocket* ClientRs = NULL;			int Val= GetNewClient(&ClientRs);			if(Val==0){				TArg *Arg = new TArg;				Arg->m_Self = this;				Arg->m_pAny = ClientRs;				pthread_t pid;				if(pthread_create(&pid,NULL,ServiceThread,Arg)<0){					if(Arg){						delete Arg;						Arg = NULL;					}				}			}		}				else if(sw==0){				ServiceTimeOut();		}		else if(sw<0){			return -1;		}	}	return 0;}void VNetService::ClientService(CRSSocket* ClientRs){	}void VNetService::ServiceTimeOut(){		}//---------------------------------------------------------------------------------------------------------------------------------------------------------VNetService2::VNetService2(){	m_BaseNum = 10;	m_RealNum = 0;	m_EmptyNum = 0;}VNetService2::~VNetService2(){}void*  VNetService2::ServiceThread2(void* pArg){	pthread_detach(pthread_self());	((VNetService2*)pArg)->ClientControl();		return NULL;}void   VNetService2::ClientControl(){        AddRealNum();	AddToEmpty();	while(1){         	m_RunEvent.Wait(1000);                CRSSocket*  ClientRs = RemoveFromClientList();                if(ClientRs){			RemoveFromEmpty();	          	ClientService(ClientRs);	           	if(m_RealNum>m_BaseNum && m_EmptyNum>m_BaseNum){				break;			}			AddToEmpty();	         	if(m_ClientList.size()>0){                         	m_RunEvent.Set();			}	         }  	}	RemoveFromEmpty();	RemoveRealNum();}int   VNetService2::SetBaseNum(long BaseNum){ 	if(BaseNum>0) m_BaseNum = BaseNum;}int   VNetService2::DoService2(int MSec){	pthread_t pid;	for(int i=0;i<m_BaseNum;i++){        	if(pthread_create(&pid,NULL,ServiceThread2,this)<0){		}	}		while(m_bService){		int sw = m_ServiceRs.SelectWait(FD_READ,MSec);		if(sw==FD_READ){			CRSSocket* ClientRs = NULL;			int Val= GetNewClient(&ClientRs);			if(Val==0){				AddToClientList(ClientRs);				if(m_EmptyNum==0){					if(pthread_create(&pid,NULL,ServiceThread2,this)<0){											}				}			}		}		else if(sw==0){			ServiceTimeOut();		}		else if(sw<0){			return -1;		}	}	return 0;}void  VNetService2::AddRealNum(){	VAutoLock al(&m_CriReal);	m_RealNum++;}void VNetService2::RemoveRealNum(){	VAutoLock al(&m_CriReal);	m_RealNum--;}void  VNetService2::AddToClientList(CRSSocket*  ClientRs){	VAutoLock al(&m_CriClient);	m_ClientList.push_back(ClientRs);        m_RunEvent.Set();}CRSSocket*  VNetService2::RemoveFromClientList(){	CRSSocket*  ClientRs = NULL;	VAutoLock al(&m_CriClient);	if(m_ClientList.size()>0){		ClientRs = m_ClientList.front();		m_ClientList.pop_front();	}	return  ClientRs;}void   VNetService2::AddToEmpty(){	VAutoLock al(&m_CriEmpty);	m_EmptyNum++;}void   VNetService2::RemoveFromEmpty(){        VAutoLock al(&m_CriEmpty);        if(m_EmptyNum>0) m_EmptyNum--;}void VNetService2::ClientService(CRSSocket* ClientRs){}void VNetService2::ServiceTimeOut(){}//---------------------------------------------------------------------------------------------------------------------------------------------------------	

⌨️ 快捷键说明

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