代码拉取完成,页面将自动刷新
同步操作将从 qykings/kgameserver 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "SGameSocket.h"
SGameSocket* SGameSocket::_instance=new SGameSocket();
SGameSocket* SGameSocket::GetInstance()
{
return _instance;
}
SGameSocket::SGameSocket(void)
{
}
SGameSocket::~SGameSocket(void)
{
}
int SGameSocket::clearSocketList(SOCKET socketId)
{
SGameSocketList_Str *str=findSocket(socketId,_headSgameSocket_str);
if(str){
SGameSocketList_Str* last=str->last;
last->next = str->next;
delete str;
str = NULL;
return 0;
}
return -1;
}
SGameSocketList_Str* SGameSocket::findSocket(int socketId,SGameSocketList_Str *str)
{
if( str && str->socketId == socketId )
{
return str;
}
if(str->next !=nullptr)
{
findSocket(socketId,str->next);
}
return nullptr;
}
void *SGameSocket::rec_data(void* serConn)
{
SOCKET *tmpcon = (SOCKET*)serConn;
char receiveBuf[ MSG_LEN ]={0};//接收
while( 1 ){
memset(receiveBuf,0, MSG_LEN);//重置
int buflen = recv(*tmpcon,receiveBuf,sizeof(receiveBuf),0);
if(buflen <= 0)
{
// 这里表示对端的socket已正常关闭
std::cout<<"buflen:%s\n"<<buflen<<
"\nsocket已正常关闭 " <<std::endl;
SOCKET socketId = *tmpcon;
//_instance->clearSocketList(socketId);
break;
}
else if(buflen>0)
{
/** MsgData_Str msgData;
memcpy( msgData.msgid,"100001", sizeof("100001") );
memcpy( msgData.msgType, "002", sizeof("002") );
memcpy( msgData.msg, "msg struct test", sizeof("msg struct test"));
send(*tmpcon, (char*)&msgData,sizeof(msgData),0);
**/
// CBuffer recvStr;
/// memcpy( &recvStr,receiveBuf,sizeof(receiveBuf) );
CByteArrays byt;
byt.setData( receiveBuf );
MsgHandle::GetInstance()->decodeMsg( byt ,*tmpcon);
}
}
return NULL;
}
int SGameSocket::creat( const char *ipstr ,short portValue )
{
//创建套接字
WORD myVersionRequest;
WSADATA wsaData;
myVersionRequest=MAKEWORD(1,1);
int err;
err=WSAStartup(myVersionRequest,&wsaData);
if (!err){
printf("已打开套接字\n");
}else{
printf("ERROR:嵌套字未打开!");
return 1;
}
//进一步绑定套接字
SOCKET serSocket=socket(AF_INET,SOCK_STREAM,0);//创建了可识别套接字
//需要绑定的参数
SOCKADDR_IN addr;
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=inet_addr( ipstr );;//ip地址
addr.sin_port=htons( portValue );//绑定端口
int ret = bind(serSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));//绑定完成
listen(serSocket,NUM_THREADS);//其中第二个参数代表能够接收的最多的连接数
//////////////////////////////////////////////////////////////////////////
//开始进行监听
//////////////////////////////////////////////////////////////////////////
SOCKADDR_IN clientsocket;
int len=sizeof(SOCKADDR);
int tmp=0;
_headSgameSocket_str = new SGameSocketList_Str;
SGameSocketList_Str* _currentSgameSocket_str=_headSgameSocket_str;
while (1)
{
SOCKET serConn=accept(serSocket,(SOCKADDR*)&clientsocket,&len);//如果这里不是accept而是conection的话。。就会不断的监听
if(_currentSgameSocket_str->socketId)
{
SGameSocketList_Str* temp = new SGameSocketList_Str;
temp->last=nullptr;
temp->next=nullptr;
temp->socketId =0;
_currentSgameSocket_str->next = temp;
temp->last = _currentSgameSocket_str;
temp->current = tmp;
_currentSgameSocket_str = temp;
}
_currentSgameSocket_str->socketId = serConn;
pthread_t ntid;
if(( pthread_create(&ntid,NULL,rec_data, &serConn) ) != 0)
{
printf("creat pthread fialed");
break;
}
tmp++;
}
return 1;
}
int SGameSocket::sendMsg(int socketId,CByteArrays* byt)
{
send(socketId, byt->getData(),byt->size(),0);
return 0;
}
int SGameSocket::destroy()
{
WSACleanup();//释放资源的操作
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。