#include "network.h"
#pragma comment(lib, "ws2_32.lib")
// MFC 套接字扩展
#define COMMPORT 25201
char * host_name = "234.5.6.201";
#define PAGING_AREANUM TERMINAL_COUNT
struct sockaddr_in m_addReceCmd,m_local,m_remoteAddr;
struct sockaddr_in server_address;
//ip_mreq command;
SOCKET m_sockRecCMD,m_sock , m_sockM;
UINT m_terminalID=33333;
int m_nIsAreaThread=0;
//////////////////////////////////////////////////////////////////////
// CClrButton
NetWork::NetWork()
{
//*********************************************
WSAData data;
::WSAStartup(MAKEWORD(2,2),&data);
//*********************************************
CWinThread *handle;
handle=::AfxBeginThread(ReceiveThread,this,THREAD_PRIORITY_NORMAL); //创建一个线程
if(handle==NULL)
TRACE("fail to creat NbtstatThread thread\n" );
handle=::AfxBeginThread(GetTerminalInfoThread,this,THREAD_PRIORITY_NORMAL); //创建一个线程
if(handle==NULL)
TRACE("fail to creat NbtstatThread thread\n" );
}
NetWork::~NetWork()
{
::WSACleanup();
}
UINT NetWork::ReceiveThread(LPVOID param)
{
TRACE("ReceivingThrd is running\n");
NetWork *pThis=(NetWork*)param;
int ncount;
int len;
char cBuf[15*1024];
//if((m_sockRecCMD = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0,0)) == INVALID_SOCKET){
// TRACE("sock failed with:%d\n", WSAGetLastError());
// return false;
//}
m_addReceCmd.sin_family = AF_INET;
m_addReceCmd.sin_port = htons(25202);
m_addReceCmd.sin_addr.s_addr = INADDR_ANY;
//if(bind(m_sockRecCMD, (struct sockaddr *)&m_addReceCmd, sizeof(m_addReceCmd)) == SOCKET_ERROR){
// TRACE("sock failed with:%d\n", WSAGetLastError());
// closesocket(m_sockRecCMD);
// WSACleanup();
// return false;
//}
if((m_sock = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0,
WSA_FLAG_MULTIPOINT_C_LEAF |
WSA_FLAG_MULTIPOINT_D_LEAF)) == INVALID_SOCKET){
TRACE("sock failed with:%d\n", WSAGetLastError());
//closesocket(m_sockRecCMD);
return false;
}
m_local.sin_family = AF_INET;
m_local.sin_port = htons(25201);
m_local.sin_addr.s_addr = INADDR_ANY;
if(bind(m_sock, (struct sockaddr *)&m_local, sizeof(m_local)) == SOCKET_ERROR){
TRACE("sock failed with:%d\n", WSAGetLastError());
// closesocket(m_sockRecCMD);
closesocket(m_sock);
WSACleanup();
return false;
}
int ttl = 3;
DWORD ret;
if(WSAIoctl(m_sock,SIO_MULTICAST_SCOPE,
&ttl,sizeof(ttl),
NULL,0,&ret,NULL,NULL) == SOCKET_ERROR){
TRACE("sock failed with:%d\n", WSAGetLastError());
// closesocket(m_sockRecCMD);
closesocket(m_sock);
return false;
}
m_remoteAddr.sin_family = AF_INET;
m_remoteAddr.sin_port = htons(COMMPORT);
m_remoteAddr.sin_addr.S_un.S_addr = inet_addr(host_name);
//join th multicast group.
if((m_sockM = WSAJoinLeaf(m_sock, (SOCKADDR *) &m_remoteAddr,
sizeof(m_remoteAddr), NULL, NULL, NULL, NULL,
JL_BOTH)) == INVALID_SOCKET){
TRACE("sock failed with:%d\n", WSAGetLastError());
// closesocket(m_sockRecCMD);
closesocket(m_sock);
return false;
}
len=sizeof(server_address);
while(1)
{
//address.sin_port=htons(COMMPORT);
if((ncount=recvfrom(m_sockM, cBuf, sizeof(cBuf), 0,(struct sockaddr*)&server_address, &len)) == -1)
{
perror("recvfrom");
TRACE("have recv network data,ncount=%d,len=%d\n",ncount,len);
}
TRACE("have recv network data,ncount=%d,len=%d\n",ncount,len);
pThis->HandleCMD((BYTE*)cBuf);
}
return true;
}
UINT NetWork::HandleCMD(BYTE* cRecCmdBuf)
{
UINT result = 0;
BCStateAnswer sa;
unsigned int nVol=0;
unsigned int i;
struct in_addr inaddr;
BOOL isSendResponse;
// char cVol[4];
BCStopPlay * spCmd;
UINT isFound=0;
BCBasicCMD * stuCmd = (BCBasicCMD *)cRecCmdBuf;
if((stuCmd->cmdID > m_stuMyState.nCurrCommandId) || (stuCmd->cmdID == 0))
{
if(stuCmd->cmdType == 3) //终端状态查询命令 主机每隔一秒向终端发一次查询,终端收到后应该回复,表示终端在线。
{
BCStateQuery* bcQuery= (BCStateQuery *)cRecCmdBuf;
// if(m_pDispInfo->nState==STATE_NOTCONNECT)
// usleep((m_terminalID%100)*20000);
sa.cmdType = 1;
sa.sender = m_terminalID;
sa.isOnLine =0;// * m_onLineState;
nVol=0;
nVol&=0xff;
nVol*=(UINT)655.35;
nVol+=nVol<<16;
sa.volume=nVol;
// memcpy(sa.stuAMPState,stuCurrAMPState,5*sizeof(int));
if(RequestSendTo((char*)&sa,sizeof(sa),25202)==-1)
{
inaddr.s_addr=server_address.sin_addr.s_addr;
printf ("receive address=%s\n",inet_ntoa(inaddr));
return 0;
}
BCStateQuery * sqCmd=(BCStateQuery *)cRecCmdBuf;
}
else
{
// m_nLostTick++;
spCmd = (BCStopPlay *)cRecCmdBuf;
// if(m_stuMyState.nCurrCommandId==stuCmd->cmdID)
// return 0;
//**************************************************************************************************
for(i=0; i<TERMINAL_COUNT && spCmd->terminals[i] != 0; i++)
{
if(spCmd->terminals[i] == m_terminalID)
{
isFound = true;
break;
}
}
//*************************************************************************************************
if(isFound || stuCmd->cmdType == 4)
{ //is send to me.
isSendResponse=true;
if((stuCmd->cmdType != 6)&&(stuCmd->cmdType != 8))
{
printf("HandleCmd ID:%d \tTYPE:%d\n", spCmd->basic.cmdID, spCmd->basic.cmdType);
}
isSendResponse = true;
switch(stuCmd->cmdType){
/* case 1: //控制终端播放指定频道的节目
{
printf("SetChannel Area:");
for(i=0; i<TERMINAL_COUNT && spCmd->terminals[i] != 0; i++)
{
printf("%d,",spCmd->terminals[i]);
}
printf("\n");
ReceMp3Data(cRecCmdBuf);
break;
}
case 2: //设置音量命令
{
BCSetVolume * svCmd = (BCSetVolume *)cRecCmdBuf;
unsigned int nVol=(svCmd->volume&0xffff)/(65536/100);
SetVolume(nVol);
sprintf(cVol,"%d",nVol);
WriteConfig("vol",cVol);
if( MainWindow::m_pMainWindowHander->m_nSetupWinOpenSign==TRUE)
SetupWindow::m_pSetupWindowHander->VolScrollBar->setValue(nVol);
break;
}
case 4: // 当终端请求时,主机向终端传送节目单
{
BCCards * cardsCmd = (BCCards *)cRecCmdBuf;
MusicCards=(BCCards *)cRecCmdBuf;
int ni;
ni=0;
// if(m_pDispInfo->cMachineType==1) //machine is 5401 then return
// break;
printf("GetCurrentCard=%d,cardsCmd->cardIndex=%d,GetCurrentPage=%d,cardsCmd->pageIndex=%d\n",GetCurrentCard(),cardsCmd->cardIndex,GetCurrentPage(),cardsCmd->pageIndex);
if((GetCurrentCard()==cardsCmd->cardIndex) && (GetCurrentPage()==cardsCmd->pageIndex))
{
while(ni<51 && cardsCmd->cCards[ni][0])
{
sprintf(m_pDispInfo.stuProgInfo[cardsCmd->cardIndex].cProgText[cardsCmd->pageIndex*50+ni],"%d %s",cardsCmd->pageIndex*50+ni+1,cardsCmd->cCards[ni]);
printf("m_pDispInfo.stuProgInfo[%d].cProgText=%s\n",cardsCmd->pageIndex*50+ni+1,cardsCmd->cCards[ni]);
if(cardsCmd->pageIndex*50+ni+1==1)
{
printf("cardsCmd->cCards[%d][0]=%d,[1]=%d,[3]=%d,4=%d\n",cardsCmd->pageIndex*50+ni+1,cardsCmd->cCards[ni][0],cardsCmd->cCards[ni][1],cardsCmd->cCards[ni][3],cardsCmd->cCards[ni][4]);
}
ni++;
}
if(!cardsCmd->hasMoreCard)
m_pDispInfo.nProgNum[cardsCmd->cardIndex]=cardsCmd->pageIndex*50+ni;
SethasMoreCard(cardsCmd->hasMoreCard);
}
break;
}
case 5: //停止播放命令 控制终端播放。
{
printf("Stop Order command,nState=%d,OrderState=%d\n",m_pDispInfo.nState,m_nLastOrderState);
if(m_pDispInfo.nState==STATE_PLAY && m_nLastOrderState)
{
// m_stuMessage.nData=3; //stop
// m_stuMessage.nType=MESS_ORDERPLAY;
m_nLastOrderState=0;
// msgsnd(m_nMsgId,&m_stuMessage,sizeof(STUMESSAGE)-sizeof(long int),0);
// printf("send to UCGUI Order fouce stop\n");
}
else if(m_pDispInfo.nState!=STATE_ORDER