📄 sipudp_impl.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const SipUdp_impl_cxx_Version = "$Id: SipUdp_impl.cxx,v 1.82 2003/01/06 17:37:30 sprajpat Exp $";#include "global.h"#include <cassert>#include <sys/time.h>#include <string>#include <stdlib.h>#include <stdio.h>#include "Fifo.h"#include "LockHelper.hxx"#include "SipCommand.hxx"#include "SipMsg.hxx"#include "SipTransactionGC.hxx"#include "SipTransactionLevels.hxx"#include "SipUdp_impl.hxx"#include "SipVia.hxx"#include "Sptr.hxx"#include "StatusMsg.hxx"using namespace Vocal;/// this is a function to print messagesstring printMsgBrief(Sptr<SipMsg> msg); /* retransmitOff from TransceiverSymbols.hxx */bool SipUdp_impl::Udpretransmitoff = retransmitOff; /* retransmitTimeInitial from TransceiverSymbols.hxx */ int SipUdp_impl::Udpretransmitimeinitial = retransmitTimeInitial; /* retransmitTimeMax from TrasnceiverSymbols.hxx */int SipUdp_impl::Udpretransmittimemax = retransmitTimeMax; SipUdp_impl::SipUdp_impl(Fifo<SipMsgContainer*>* fifo, int port /*default argument*/ ) : randomLosePercent(0), udpStack( NULL, port ), recFifo(fifo), sendFifo(), shutdown(false){ udpStack.setModeBlocking(false); sendThread.spawn(sendThreadWrapper, this); receiveThread.spawn(rcvThreadWrapper, this);} SipUdp_impl::~SipUdp_impl() { shutdown = true; //insert shutdown message in the send fifo sendFifo.add(0); sendThread.join(); receiveThread.join();}void*SipUdp_impl::receiveMain(){ NetworkAddress sender; while (1) { int len = 0; static const string::size_type MTU = 3600; char buf[MTU+1]; buf[0] = '\0'; len = 0; len = udpStack.receiveTimeout( buf, MTU, &sender, 1, 0 ); if ( len == 0 ) { } else if ( len < 0 ) { cpLog(LOG_DEBUG_STACK, "receiveTimeout error"); } else if(recFifo->size() <= 10000 ) { buf[len] = '\0'; SipMsgContainer* sipMsg = new SipMsgContainer; sipMsg->msg.out = Data(buf, len); /****************************************************** * will need to move this stuff into the stack later */ Data toBeEaten = sipMsg->msg.out; cpLog( LOG_DEBUG_STACK, "Received UDP Message \n%s" , toBeEaten.logData()); sipMsg->msg.in = SipMsg::decode(toBeEaten); if(sipMsg->msg.in != 0) { sipMsg->msg.in->setReceivedIPName( sender.getIpName() ); sipMsg->msg.in->setReceivedIPPort( Data(sender.getPort()) ); recFifo->add(sipMsg); cpLog(LOG_INFO, "Received UDP Message:\n\n<- HOST[%s] PORT[%d]\n\n%s", sender.getIpName().c_str(), sender.getPort(), toBeEaten.logData()); } else { // this message failed to decode cpLog(LOG_DEBUG, "received message did not decode\n%s" , sipMsg->msg.out.logData()); /// since not aceepted, hence delete delete sipMsg; } } else { cpLog(LOG_DEBUG, "***Ignoring received msg ***:%d" , recFifo->size()); } if(shutdown) { return 0; } } return 0;} void*SipUdp_impl::sendThreadWrapper(void *p) { return static_cast<SipUdp_impl*>(p)->sendMain();} intSipUdp_impl::udpSend(SipMsgContainer *sipMsg){ // send it int ret_status = 0; try { if(sipMsg->msg.netAddr == 0){ cpLog(LOG_WARNING,"UDP SEND IS FAILED NULL MESSAGE :( To Send"); return 0; } const NetworkAddress& nAddr = *(sipMsg->msg.netAddr); assert(sipMsg->msg.out.length()); cpLog(LOG_INFO, "Sending UDP Message :\n\n-> HOST[%s] PORT[%d]\n\n%s", nAddr.getIpName().c_str(),nAddr.getPort(), sipMsg->msg.out.logData()); int lngth = sipMsg->msg.out.length(); LocalScopeAllocator lo; //sipMsg->myLock.lock(); //int count = sipMsg->retransCount; //sipMsg->myLock.unlock(); //if(count == 0) return 0; ret_status = udpStack.transmitTo(sipMsg->msg.out.getData(lo),lngth,&nAddr); } catch(NetworkAddress::UnresolvedException& ) { cpLog(LOG_ERR, "Failed to resolve the host name"); } catch (UdpStackExceptionPortsInUse&) { cpLog(LOG_DEBUG_STACK,"All ports in use"); } return ret_status;} void*SipUdp_impl::rcvThreadWrapper(void *p) { return static_cast<SipUdp_impl*>(p)->receiveMain();} void* SipUdp_impl::sendMain(){ while(1) { int ret_status = 0; RetransmitContents* retransmit = sendFifo.getNext(); if(shutdown) { return 0; } if (retransmit != 0) { SipMsgContainer *sipMsg = retransmit->getMsg(); if (sipMsg != 0) { int count = sipMsg->retransCount; if (count) { cpLog(LOG_DEBUG_STACK, "retransmission count = %d", count); //get host, and port, and send it off. if(!randomLosePercent || ((random() % 100) >= randomLosePercent)) { // ret_status = udpSend(sipMsg); switch(ret_status) { case 0: break; case 1: cpLog(LOG_INFO, "ECONNREFUSED! "); ret_status = 403; break; case 2: cpLog(LOG_INFO, "EHOSTDOWN! "); ret_status = 408; break; case 3: cpLog(LOG_INFO, "EHOSTUNREACH! "); ret_status = 404; break; default: cpLog(LOG_INFO, "ERROR!!!"); ret_status = 400; } if ( ret_status) { if ((sipMsg != 0) && (sipMsg->msg.type != SIP_STATUS)) { Data toBeEaten = sipMsg->msg.out; Sptr<SipMsg> mmsg = SipMsg::decode(toBeEaten); Sptr<SipCommand> commandMsg; commandMsg.dynamicCast(mmsg); SipMsgContainer * retVal = new SipMsgContainer; retVal->msg.in=new StatusMsg((*commandMsg), ret_status);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -