⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udpsocket.cxx

📁 一个SIP协议栈
💻 CXX
字号:
/********************************************************************* $Id: UDPSocket.cxx,v 1.2 1999/08/31 02:22:05 cullen Exp $ *********************************************************************  This library is free software; you can redistribute it and/or modify  it under the terms of the GNU Lesser General Public License as  published by the Free Software Foundation; either version 2 of the  License, or (at your option) any later version.  In addition to the  terms and conditions set forth in the GNU Lesser General Public  License, as a condition of using this library you are required to  grant to all users of this library or any implementation utilizing  or derived from this library a reciprocal, no cost, worldwide,  perpetual, non-exclusive, non-transferable, unrestricted license to  your claims of all patents and patent applications throughout the  world that are infringed by the library or any implementation  utilizing or derived from this library.  In the event you  redistribute this library or any implementation utilizing or derived  from this library, you must prominently display the foregoing terms  and conditions with the library or the implementation utilizing or  derived from this library.   In the event of a conflict of terms between the foregoing license  grant and the terms set forth in the GNU Lesser General Public  License, the foregoing terms and conditions shall be deemed to  govern.   This library is distributed in the hope that it will be useful, but  WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  Lesser General Public License for more details.   You should have received a copy of the GNU Lesser General Public  License along with this library; if not; write to the Free Software  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.   Copyright 1999 Vovida Networks, Inc.  All Rights Reserved. ********************************************************************* $Log: UDPSocket.cxx,v $ Revision 1.2  1999/08/31 02:22:05  cullen updated header **********************************************************************/#include <iostream.h>#include <sys/socket.h>#include <netinet/in.h>#include <sys/times.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include "IPAddress.h"#include "UDPSocket.h"#include "MHError.h"#include "Trace.h"using namespace mh323;//------------------------------------------------------------------------------//// UDPSocket////------------------------------------------------------------------------------////UDPSocket::UDPSocket( uint iPort = 0 ){    IN("UDPSocket::UDPSocket(uint)");    CriticalSection CrtSec(&m_xMutex);    m_iDomain=AF_INET;    m_iType=SOCK_DGRAM;    m_iProtocol=0;    m_ihSocket=::socket(m_iDomain,m_iType,m_iProtocol);    m_iTimer=0;    m_bStopReceived=false;    if (m_ihSocket == -1)    {        MHTRACE(9, "Socket Error " << strerror(errno) << endl);        SOCKET_EXCEPTION("Can't allocate socket", errno);    }    if(pipe(m_xPipeChan)==-1)    {        SOCKET_EXCEPTION("Can't open Pipe Channel", errno);    }           if (iPort)    {                bind (iPort);    }    }//------------------------------------------------------------------------------////void UDPSocket::bind( uint iPort){    IN("UDPSocket::bind( uint )");    CriticalSection CrtSec(&m_xMutex);    m_xInetAddr.setPort(iPort);    struct sockaddr_in xLocalAddr;    fcntl (m_ihSocket, F_SETFL, O_NONBLOCK);    memset((char*) &xLocalAddr,0, sizeof(xLocalAddr));    xLocalAddr.sin_family=AF_INET;    xLocalAddr.sin_port=htons(m_xInetAddr.getPort());    xLocalAddr.sin_addr.s_addr=htonl(INADDR_ANY);    if (::bind(m_ihSocket,(sockaddr*)&xLocalAddr,sizeof(sockaddr_in))== -1)    {        SOCKET_EXCEPTION("Can't bind socket", errno);    }    MHTRACE(9, "Binded succesfully to " << m_xInetAddr);    }//------------------------------------------------------------------------------////void UDPSocket::sendto(const void * cpBuf, int iBufLen,                        IPAddress &xDestination, int iFlag=0){    IN("UDPSocket::sendto(...)");    CriticalSection CrtSec(&m_xMutex);    while(1)    {        try        {            waitForOutputAvail();            break;        }        catch(WaitingStopped)        {            m_bStopReceived=true;            continue;        }    }    struct sockaddr_in xDest;    xDest.sin_family=AF_INET;    xDest.sin_port=htons(xDestination.getPort());    xDest.sin_addr.s_addr=xDestination.getAddress();    if(::sendto(m_ihSocket,cpBuf,iBufLen,iFlag,(sockaddr*)&xDest,sizeof(sockaddr_in))==-1)    {        SOCKET_EXCEPTION("Socket sending error ",errno);    }    if(m_bStopReceived)    {        m_bStopReceived=false;            stopWaiting();    }};//------------------------------------------------------------------------------////int  UDPSocket::recvfrom(void * cpBuf, int iBufLen, IPAddress &xSource, int iFlag=0){    IN("UDPSocket::recvfrom(void *, int , int)");    CriticalSection CrtSec(&m_xMutex);    struct sockaddr_in xSrc;    int Res=0;    unsigned int len=sizeof(xSrc);       while (1)    {        try        {            waitForInput();         }        catch (WaitingStopped)        {            m_bStopReceived=true;            continue;        }                if((Res=::recvfrom(m_ihSocket,(char *)cpBuf,iBufLen,iFlag, (sockaddr*)&xSrc,(socklen_t *) &len))==-1)        {            SOCKET_EXCEPTION("Socket reading Error",errno);        }        if(Res==0)        {            SOCKET_EXCEPTION("Wrong data length while reading Socket ",errno);        }        xSource.set(xSrc.sin_addr.s_addr,ntohs(xSrc.sin_port));        MHTRACE(9, "From:" << xSource );                return Res;            }        if(m_bStopReceived)    {        m_bStopReceived=false;            stopWaiting();    }};//------------------------------------------------------------------------------////void UDPSocket::addMulticast( IPAddress & xMulticast){    IN("UDPSocket::addMulticast( IPAddress&)");    struct ip_mreqn mreqn;    mreqn.imr_multiaddr.s_addr=xMulticast.getAddress();    mreqn.imr_address.s_addr=m_xInetAddr.getAddress();    mreqn.imr_ifindex = 0;    MHTRACE(9,"Adding address "<<m_xInetAddr<<" to multicast group "<<xMulticast);    setsockopt (m_ihSocket,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(ip_mreqn));    }//------------------------------------------------------------------------------////void UDPSocket::dropMulticast(IPAddress & xMulticast){    IN("UDPSocket::dropMulticast( IPAddress&)");    struct ip_mreqn mreqn;    mreqn.imr_multiaddr.s_addr=xMulticast.getAddress();    mreqn.imr_address.s_addr=m_xInetAddr.getAddress();    mreqn.imr_ifindex = 0;    MHTRACE(9,"Deleting address "<<m_xInetAddr<<" from multicast group "<<xMulticast);    setsockopt (m_ihSocket,IPPROTO_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(ip_mreqn));}//------------------------------------------------------------------------------////UDPSocket::~UDPSocket( void ){    close (m_ihSocket);}    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -