📄 net_factory.cpp
字号:
#include "stdafx.h"
#include "net_factory.h"
#include "../audio/audioapp.h"
net_factory::net_factory()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2),&wsaData)) //调用Windows Sockets DLL
{
WSACleanup();
return;
}
pnetcmd = NULL;
ptcpnet = NULL;
for(int i=0; i<MAXCONNECTSERVER; i++)
memset(&logininfo[i], 0, sizeof(loginteminfo));
}
net_factory::~net_factory()
{
destroy();
}
bool net_factory::create(HWND m_hwnd)
{
pnetcmd = new net_cmd;
if(pnetcmd == NULL)
return FALSE;
if(!audioplay_create(1, 8000, 16))
return false;
if(!pnetcmd->create(m_hwnd))
return false;
threadstart(THREAD_PRIORITY_NORMAL);
return true;
}
bool net_factory::destroy()
{
audioapp_destroy();
//退出网路协议探测线程
threadclose();
if(pnetcmd)
{
delete pnetcmd;
pnetcmd = NULL;
}
if(ptcpnet)
{
delete ptcpnet;
ptcpnet = NULL;
}
return true;
}
//探测网路连接方式并且完成登陆
bool net_factory::logindetectnet(loginteminfo *ploginfo)
{
if(ploginfo == NULL)
return false;
if(!ploginfo->ip)
return false;
//探测网络的连接方式
net_tcp * ptcpnet = new net_tcp;
if(ptcpnet == NULL)
return false;
SOCKET temsocket = ptcpnet->create();
if(temsocket == SOCKET_ERROR)
{
delete ptcpnet;
ptcpnet = NULL;
return false;
}
bool mbool = ptcpnet->connectserver(ploginfo->ip, ploginfo->port);
delete ptcpnet;
ptcpnet = NULL;
//登陆
if(pnetcmd)
pnetcmd->loginin(ploginfo->ip, ploginfo->port, ploginfo->user, ploginfo->password, mbool);
memset(ploginfo, 0, sizeof(loginteminfo));
return true;
}
//用线程来登陆
void net_factory::entry()
{
bool boolempty = true;
while(1)
{
if(threadcheck())
break;
boolempty = true;
for(int i=0; i<MAXCONNECTSERVER; i++)
{
if(logindetectnet(&logininfo[i]))
boolempty = false;
}
if(boolempty)
{
Sleep(500);
continue;
}
}
}
//登陆
bool net_factory::login_net(ULONG ip, USHORT port, LPCTSTR user, LPCTSTR password)
{
for(int i=0; i<MAXCONNECTSERVER; i++)
{
if(logininfo[i].ip == ip && logininfo[i].port == port)
return false;
}
for(int i=0; i<MAXCONNECTSERVER; i++)
{
if(!logininfo[i].ip)
{
logininfo[i].ip = ip;
logininfo[i].port = port;
strcpy(logininfo[i].user, user);
strcpy(logininfo[i].password, password);
return true;
}
}
return true;
}
//登出
bool net_factory::loginout(ULONG ip, USHORT port)
{
if(pnetcmd == NULL)
return false;
for(int i=0; i<MAXCONNECTSERVER; i++)
{
if(logininfo[i].ip == ip && logininfo[i].port == port)
{
memset(&logininfo[i], 0, sizeof(loginteminfo));
break;
}
}
return pnetcmd->loginout(ip, port);
}
//打开实时图象
bool net_factory::openvideochannel(ULONG ip, USHORT port, int channel, HWND m_hwnd)
{
if(pnetcmd == NULL)
return false;
return pnetcmd->openvideochannel(ip, port, channel, m_hwnd);
}
//关闭实时图象
bool net_factory::closevideochannel(ULONG ip, USHORT port, int channel)
{
if(pnetcmd == NULL)
return false;
return pnetcmd->closevideochannel(ip, port, channel);
}
bool net_factory::netupdate_bound(ULONG ip, USHORT port, int channel)
{
if(pnetcmd == NULL)
return false;
return pnetcmd->update_bound(ip, port, channel);
}
//域名解析
ULONG net_factory::netgethostbyname(char *paddr, unsigned char *outaddr)
{
if(paddr == NULL || outaddr == NULL)
return 0;
/*struct hostent FAR * gethostbyname (
const char FAR * name
);
*/
struct hostent *p = gethostbyname(paddr);
if(p == NULL)
return 0;
BYTE addr[4] ;
memcpy(addr, p->h_addr, 4) ;
memcpy(outaddr, addr, 4) ;
return ((addr[3]<<24) + (addr[2]<<16) + (addr[1]<<8) + addr[0]);
}
bool net_factory::netsetselectchannel(ULONG ip, USHORT port, int channel)
{
if(pnetcmd == NULL)
return false;
return pnetcmd->selectchannel(ip, port, channel);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -