www.gusucode.com > 用ActiveX重新包装了一个点对点通讯的DLL文件源码程序 > 用ActiveX重新包装了一个点对点通讯的DLL文件源码程序/P2P语音通信/Interface.cpp
/***************************************************** 作者:你家的玻璃不是我砸的@vckbase.com 联系方法:xiny120@hotmail.com BLOG:http://blog.vckbase.com/zaboli http://www.hotlove.cn 程序简介: 用ActiveX重新包装了一个点对点通讯的DLL文件(该DLL文件是VCKBASE上的一位前辈写的) 偶只是把它改成了一个MFC的ActiveX,这样可以方便的在VB,vbs,js等脚本语言,特别是用在IE 里面特别方便了。 此程序主要向你说明,其实ActiveX没有多么的神秘。你遵守的它的规则,什么功能都能添加到 ActiveX中,并且和写其它的普通的程序没有什么区别! 缺点:因为同时只能有一个程序打开音频设备,所以不能开两个实例进行聊天。本来打算改改好,只 发布成品ActiveX,不发布原代码的。可最近鸟事很多,没时间。。所以连源代码都贡献了~~~ 大家可以自己完善一下。如果您做了什么好的改进,请您给作者我发一份,我会十分感谢!!! ********************************************************/ // Interface.cpp: implementation of the CInterface class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Interface.h" #include "head.h" #include "MyWaveIn.h" // Added by ClassView #include "WaveOut.h" // Added by ClassView #include "ListenSocket.h" // Added by ClassView #include "SendClient.h" #include "UdpSocket.h" #include "RecSocket.h" #include ".\interface.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif //extern CInterface m_talk; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CInterface::CInterface(): m_bIni (FALSE) { m_bWork = FALSE; m_pUdp = new CUdpSocket(); m_pRec = new CRecSocket(); g_pOut = new CWaveOut(); m_pIn = new CMyWaveIn(m_pUdp); m_sopListen = new CListenSocket(this,m_pIn,m_pUdp); m_sopSend = new CSendClient(m_pIn,this); } CInterface::~CInterface() { if (m_bIni) { m_sopListen->Close (); //m_sopSend->Close(); } if (m_bWork) { End(); } // m_pIn->StopRec (); m_pUdp->CloseSocket (); m_pRec->CloseSocket (); // g_pOut->StopPlay (); delete m_sopSend; delete m_sopListen; delete m_pIn; delete g_pOut; delete m_pUdp; delete m_pRec; } BOOL CInterface::Start(CString ip,UINT nPort,UINT nUdpPort) { char name[128]; int iLen = 128; int i = 0; CString loip; BOOL bRe; bRe = FALSE; if (m_bWork) { TRACE("The talk has worked.\n"); goto Exit; } m_sIp = ip; if (SOCKET_ERROR == gethostname(name,iLen)) { TRACE("I can't get the name.\n"); goto Exit; } struct hostent *phost; phost = gethostbyname (name); if (phost == NULL) { TRACE("gethostbyname error .\n"); goto Exit; } i = 0; /* while (phost->h_addr_list[i]) { loip = inet_ntoa (*(struct in_addr *)phost->h_addr_list[i++]); if (loip == ip) goto Exit;//为了不让其和自己说话??? } */ if (!m_sopSend->Create ()) { goto Exit; } m_sopSend->Connect (ip,nPort); if (GetLastError() != WSAEWOULDBLOCK) { AfxMessageBox("m_sopSend->Connect (ip,nPort); 失败"); goto Exit1; } m_pUdp->SetIp (ip,m_pRec->GetListenPort()); if (!g_pOut->StartPlay ()) { goto Exit3; }; if (!m_pIn->StartRec ()) { goto Exit4; }; bRe = TRUE; m_bWork = TRUE; goto Exit; Exit4: g_pOut->StopPlay (); Exit3: ; Exit1: m_sopSend->Close (); Exit: return bRe; } BOOL CInterface::End() { if (!m_bWork) { TRACE("The talk hasn't worked.\n"); return FALSE; } m_pIn->EnableSend (FALSE); m_pIn->StopRec(); m_sopSend->Close (); m_sopSend->m_bConnect = FALSE; m_sopListen->CloseClient (); g_pOut->StopPlay (); m_bWork = FALSE; return TRUE; } BOOL CInterface::Ini() { if (m_bIni) { TRACE("You have ini the talk model.\n"); return FALSE; } BOOL bCreated=FALSE; int i; for(i=TALK_COM_PORT;!bCreated&&(i<TALK_COM_PORT_LAST);i++) { try { bCreated=m_sopListen->Create (i); DWORD nErr = GetLastError(); TRACE("m_sopListen->Create error :%d",nErr); } catch(...) { } } if(!bCreated) goto Exit; i--; m_nListenPort = i; m_sopListen->Listen (); if (!m_pRec->Ini (g_pOut)) { goto Exit1; } if (!m_pUdp->Ini ()) { goto Exit2; } /* if (!g_pOut->StartPlay ()) { goto Exit3; }; if (!m_pIn->StartRec ()) { goto Exit4; }; */ m_bIni = TRUE; goto Exit; /* Exit4: g_pOut->StopPlay (); Exit3: m_pUdp->CloseSocket ();*/ Exit2: m_pRec->CloseSocket (); Exit1: m_sopListen->Close (); Exit: return m_bIni; } BOOL CInterface::IsConnect(CString ip) { if (IDYES == MessageBox(NULL,"talk?","talk",MB_YESNO)) { return TRUE; } else { return FALSE; } } void CInterface::TalkStart(CString ip) { CString temp; temp.Format ("Talk now ,Ip: %s.",ip); AfxMessageBox(temp); } void CInterface::TalkBeClose() { AfxMessageBox("Talk be close."); } void CInterface::BeClose() { g_soLock.Lock (); if (m_bWork) { m_pIn->EnableSend (FALSE); m_sopSend->Close (); m_sopSend->m_bConnect = FALSE; m_sopListen->CloseClient (); m_bWork = FALSE; TalkBeClose(); } g_soLock.Unlock (); } void CInterface::ConnectResult(int nErrorCode) { if (nErrorCode != 0) { m_bWork = FALSE; TalkOnConnect (FALSE); return ; } TalkOnConnect (TRUE); char buffer[32]; memset(buffer,0,32); struct TalkFrame *frame; frame = (struct TalkFrame *)buffer; sprintf(frame->cFlag,"TalkFrame"); frame->iLen = 0; frame->iCom = TC_NORMAL_TALK; m_sopSend->Send (buffer,sizeof(struct TalkFrame)); } void CInterface::TalkOnConnect(BOOL bRe) { CString t; t.Format ("connect %d",bRe); AfxMessageBox(t); } UINT CInterface::GetListenPort(void) { if(this->m_bIni) return this->m_nListenPort; return -1; } CRecSocket * CInterface::GetRecSocket(void) { if(this->m_bIni) return this->m_pRec; return NULL; } int CInterface::GetIP(CString & strIP) { UINT nPort; if(m_sopListen!=NULL) m_sopListen->GetSockName(strIP,nPort); return 0; }