📄 siptcpconnection.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000-2003 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 SipTcpConnection_cxx_Version = "$Id: SipTcpConnection.cxx,v 1.64.2.2 2003/02/21 03:44:58 bko Exp $";#include "InviteMsg.hxx"#include "LockHelper.hxx"#include "NetworkAddress.h"#include "SipCommand.hxx"#include "SipContact.hxx"#include "SipTcpConnection.hxx"#include "SipTransactionId.hxx"#include "SipTransactionLevels.hxx"#include "SipVia.hxx"#include "SystemInfo.hxx"#include "StatusMsg.hxx"#include "Tcp_ClientSocket.hxx"#include "Tcp_ServerSocket.hxx"#include "ThreadIf.hxx"#include "VFilter.hxx"#include "VFunctor.hxx"#include "VNetworkException.hxx"#include "VThread.hxx"#include "VThreadPool.hxx"#include "global.h"#include "support.hxx"#include "symbols.hxx"using namespace Vocal;//Note: If client or server needs to keep a persistent TCP connection for//all calls, define USE_PERSISTENT_TCP.If want to have Transaction based//TCP connections, comment out the following line.#define USE_PERSISTENT_TCP 1class NTcpStuff{ public: /// NTcpStuff() : tcpConnection(0) { } /// ~NTcpStuff() { } /// bool operator==(const NTcpStuff& other) { return (tcpConnection->getConnId() == other.tcpConnection->getConnId()); } /// Sptr < Connection > tcpConnection; /// Data tcpBuf; /// Data senderIp; /// VMutex myMutex;};typedef int TcpFd;class NTcpConnInfo{ public: /// NTcpConnInfo(); /// ~NTcpConnInfo(); /// void setConnNSenderIp(int fd, Sptr < Connection > conn, const Data& ip); /// Sptr < Connection > getStatusMsgConn(Sptr < SipMsg > msg); /// void setStatusMsgConn(Sptr < SipMsg > msg, int fd); /// int setTCPFds(fd_set* fdSet); /// Sptr<NTcpStuff> getConnInfo(int fd); /// void doCleanup(); private: /// Sptr < NTcpStuff > getCurrent(int fd); /// void delConn(int fd); /// void delIdMapEntry(const SipTransactionId& id); /// int tcpReadOrAccept(int tcpfd, TcpServerSocket* tcpStack); /// VMutex mutex; /// VMutex mapMutex; /// map < TcpFd, Sptr < NTcpStuff > > myMap; /// map < SipTransactionId, Sptr < NTcpStuff > > idMap; /// list<int> myCleanupList; /// VMutex cleanupMutex; Data nullData; /// friend class SipTcpConnection_impl_;};Sptr < Connection >NTcpConnInfo::getStatusMsgConn(Sptr < SipMsg > msg){ //form the trans id. SipTransactionId newId(*msg); LockHelper lLock(mapMutex); map < SipTransactionId, Sptr < NTcpStuff > > ::iterator i; cpLog(LOG_DEBUG, "NTcpConnInfo - IDMap size:%d", idMap.size()); i = idMap.find(newId); if (i != idMap.end()) { // found it, do something return i->second->tcpConnection; } else { cpLog(LOG_WARNING, "could not find TCP connection for status msg (%s) reply", msg->encode().logData()); return 0; }}voidNTcpConnInfo::doCleanup(){ LockHelper l(cleanupMutex); while(myCleanupList.size()) { int id = myCleanupList.front(); myCleanupList.pop_front(); delConn(id); }}voidNTcpConnInfo::setStatusMsgConn(Sptr < SipMsg > msg, int fd){ Sptr < NTcpStuff > t = getCurrent(fd); LockHelper lLock(mapMutex); if (t != 0) { SipTransactionId id(*msg); idMap[id] = t; cpLog(LOG_DEBUG, "NTcpConnInfo::setStatusMsgConn, idMapSize:%d", idMap.size() ); } else { cpLog(LOG_WARNING, "could not find valid tcp connection for message"); }}NTcpConnInfo::NTcpConnInfo() : nullData(TransNull){}NTcpConnInfo::~NTcpConnInfo(){ mutex.lock(); myMap.clear(); idMap.clear(); mutex.unlock();}voidNTcpConnInfo::delConn(int fd){ LockHelper l(mutex); map < TcpFd, Sptr < NTcpStuff > > ::iterator i = myMap.find( fd ); Sptr < NTcpStuff > del; if ( i == myMap.end() ) { return; // not found } del = i->second; myMap.erase(i); cpLog(LOG_DEBUG_STACK, "After del size:%d" ,myMap.size()); if (del != 0) { //Cleanup the transactionMap for the closed connection LockHelper l2(mapMutex); map <SipTransactionId, Sptr < NTcpStuff > > ::iterator j; map <SipTransactionId, Sptr < NTcpStuff > > ::iterator delItr; j = idMap.begin(); while (j != idMap.end()) { if (j->second == del) { delItr= j++; idMap.erase(delItr); } else j++; } del->tcpConnection->close(); }}voidNTcpConnInfo::delIdMapEntry(const SipTransactionId& id){ cpLog(LOG_DEBUG, "Deleting IdMap entry, size:%d", idMap.size()); // erase from the transactionId if needed LockHelper l2(mapMutex); map < SipTransactionId, Sptr < NTcpStuff > > ::iterator j; j = idMap.find(id); if (j != idMap.end()) { idMap.erase(j); }}Sptr < NTcpStuff >NTcpConnInfo::getCurrent(int fd){ LockHelper lLock(mutex); Sptr < NTcpStuff > myObj; map < TcpFd, Sptr < NTcpStuff > > ::iterator i = myMap.find( fd ); if ( i == myMap.end() ) { myObj = 0; } else { myObj = i->second; } return myObj;}voidNTcpConnInfo::setConnNSenderIp(int fd, Sptr < Connection > conn, const Data& ip){ LockHelper l(mutex); Sptr < NTcpStuff > myTcp; myTcp = new NTcpStuff; if (myTcp != 0) { myTcp->tcpConnection = conn; myTcp->senderIp = ip; } myMap[fd] = myTcp; cpLog(LOG_DEBUG_STACK, "Map size %d after adding %d" ,myMap.size(), fd);}Sptr<NTcpStuff> NTcpConnInfo::getConnInfo(int fd){ return(getCurrent(fd));}intNTcpConnInfo::tcpReadOrAccept(int tcpfd, TcpServerSocket* tcpStack){ if (!tcpfd) { return -1; } Sptr < Connection > newconn = 0; Sptr <NTcpStuff> connInfo = 0; cpLog(LOG_DEBUG_STACK, "Received tcp msg on fd: %d", tcpfd); if (tcpfd == tcpStack->getServerConn().getConnId()) { //this has happened on the default server socket. //create a new connection and accept on it. newconn = new Connection; int clientId = 0; // catch exception thrown when we run out of fds try { clientId = tcpStack->accept(*newconn); } catch (VNetworkException &) { return -1; } cpLog(LOG_DEBUG_STACK,"***** New Connection = %s *****\n", newconn->getDescription().c_str()); // set the sender ip string ipName = newconn->getIp(); //cache this in the tcpMap. setConnNSenderIp(clientId, newconn, ipName); // cpLog(LOG_DEBUG_STACK, "accepting new connection: ", clientId); // need to redo this conn object, etc. tcpfd = newconn->getConnId(); connInfo = getConnInfo(tcpfd); assert(connInfo != 0); //overwrite the conn object. } else { connInfo = getConnInfo(tcpfd); assert(connInfo != 0); newconn = connInfo->tcpConnection; //it already has the //correct connection. } int numBytes; if (newconn != 0) { char buf [1025]; buf[0] = '\0'; // read off some data, possibly break LockHelper l(connInfo->myMutex); Data& myBuf = connInfo->tcpBuf; numBytes = newconn->readn(buf, 1024); if (numBytes <= 0) { // probably should close here! cpLog(LOG_DEBUG_STACK, "closing fd"); delConn(tcpfd); return -1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -