UINT AMILinkThread(LPARAM lparam)
{
CCallSchedulingSystemDlg *Dlg (CCallSchedulingSystemDlg *)lparam;
//大循环出现网络连接断开或点击设置中的应用后会重新连接服务器并生成新的socket
while(Dlg->AMIlinking)
{
closesocket(Dlg->AMIsock);
//连接server
Dlg->AMIsocksocket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addrinet_addr(Dlg->m_strServer.GetString());
addrSrv.sin_familyAF_INET;
addrSrv.sin_porthtons(5038);
int error-1, len;
len sizeof(int);
timeval tm;
fd_set set;
unsigned long ul 1;
ioctlsocket(Dlg->AMIsock, FIONBIO, //设置为非阻塞模式
bool ret false;
int iRet connect(Dlg->AMIsock, (struct sockaddr *)
if( iRet -1)
{
tm.tv_sec 8; //超时时间8秒
tm.tv_usec 0;
FD_ZERO(
FD_SET(Dlg->AMIsock,
if( select(Dlg->AMIsock1, NULL, {0};
int iRet getsockopt(Dlg->AMIsock, SOL_SOCKET, SO_ERROR, bb, (socklen_t *)
if(iRet 0)
ret true;
else
ret false;
}
else
{
ret false;
}
}
else
{
ret true;
}
//ul 0;
//ioctlsocket(Dlg->AMIsock, FIONBIO, //设置为阻塞模式
if(!ret)
{
//连接失败
//closesocket(Dlg->AMIsock);
continue; //重新连接
}
memset(Dlg->lastbuff, 0 , sizeof(Dlg->lastbuff));
mysql_close(
if(!mysql_real_connect(3306为端口号可自行设定
{
//AfxMessageBox("数据库连接失败");
Dlg->isConnectd FALSE;
continue;
}
else
{
Dlg->isConnectd TRUE;
if(!mysql_real_connect( FALSE;
continue;
}
}
Dlg->canUsersInit 1;
//socket缓存
char lastbuff[BUFFERSIZE] {0};
char buff[BUFFERSIZE] {0};
len 0;
//接收数据
fd_set rset;
int nReady;
struct timeval timeout;
while(Dlg->AMIlinking)
{
FD_ZERO(
timeout.tv_sec 0;
timeout.tv_usec 200;
//listen socket 加入fdset
FD_SET(Dlg->AMIsock,
nReady select(Dlg->AMIsock1,
if(nReady 0) //超时
{
continue;
}
else if(nReady -1) //出错
{
//closesocket(Dlg->AMIsock);
Dlg->isConnectd FALSE;
break;
}
else if( FD_ISSET(Dlg->AMIsock, 1;
//Dlg->GetMessagedata();
/*int result recv(Dlg->AMIsock,bufflen,BUFFERSIZE-len-1,0);
//如果网络连接断开
if(SOCKET_ERROR result)
{
//AfxMessageBox("SOCKET连接出错");
break;
}
else if(result > 0)
{
CString str;
if(strstr(buff,"Asterisk Call Manager"))
{
str "Action: login\r\nUsername: phpagi\r\nSecret: phpagi\r\n\r\n";
send(Dlg->AMIsock,str.GetBuffer(0),str.GetLength(),0);
continue;
}
else if(strstr(buff, "Authentication accepted"))
{
//str "Action: sippeers\r\n\r\naction: DAHDIShowChannels\r\n\r\naction: CoreShowChannels\r\n\r\nAction: MeetmeList\r\nConference: "m_strUser"\r\n\r\n";
str "Action: sippeers\r\n\r\naction: DAHDIShowChannels\r\n\r\naction: CoreShowChannels\r\n\r\nAction: MeetmeList\r\n\r\n";
send(Dlg->AMIsock,str.GetBuffer(0),str.GetLength(),0);
continue;
}
//处理
char *head buff;
char *pStr NULL;
//处理head指向的内存的过程
while((pStr strstr(head, "\r\n\r\n")))
{
*(pStr2) \0;
Dlg->StrToEvent(head);
Dlg->EventProcess();
head pStr 4;
if(*head \0)
break;
}
memset(lastbuff,0,BUFFERSIZE);
len strlen(head);
if(*head ! \0)
{
memcpy(lastbuff, head, len);
}
}*/
}
}
//连接断开
//closesocket(Dlg->AMIsock);
Dlg->isConnectd FALSE;
}
return 0;
}