📄 socket.cxx
字号:
/********************************************************************* $Id: Socket.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: Socket.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 <iomanip.h>#include "IPAddress.h"#include "Socket.h"#include "MHError.h"#include "Trace.h"using namespace mh323;//------------------------------------------------------------------------------//// Socket////------------------------------------------------------------------------------////Socket::Socket( uint iPort = 0 ){ }//------------------------------------------------------------------------------//////------------------------------------------------------------------------------////void Socket::waitForOutputAvail( void ){ IN("Socket::waitForOutputAvail()"); CriticalSection CrtSec(&m_xMutex); fd_set select_set; struct timeval timeRec; struct timeval * pxTimeRec; if(m_iTimer) // If timer was set { MHTRACE(9,"Timer was set"); TimeInterval iTime=GetTimer(); timeRec.tv_sec = iTime/1000; timeRec.tv_usec= (iTime%1000)*1000; pxTimeRec=&timeRec; } else { MHTRACE(9,"Timer was not set"); pxTimeRec=NULL; } FD_ZERO( &select_set ); FD_SET( m_ihSocket, &select_set ); FD_SET( m_xPipeChan[0], &select_set ); int rc = select(FD_SETSIZE,0, &select_set,0, pxTimeRec ); if (rc > 0) { if (FD_ISSET(m_ihSocket, &select_set)) { return; } if (FD_ISSET(m_xPipeChan[0], &select_set)) { char c; MHTRACE(9,"Pipe Channel Command"); read(m_xPipeChan[0],&c,1); switch(c) { case 'c': MHTRACE(9,"Socket operation cancelled"); SOCKET_CANCELLED; break; case 's': MHTRACE(9,"Socket operation stopped"); SOCKET_STOPPED; break; default: SOCKET_EXCEPTION("Incorrect command from pipe channel",-1); break; } } } if (rc < 0 ) { SOCKET_EXCEPTION("Error in Select",errno); } MHTRACE(9,"Timeout exception"); SOCKET_TIMEOUT;}//------------------------------------------------------------------------------////void Socket::waitForInput( void ){ IN("Socket::waitForInput()"); CriticalSection CrtSec(&m_xMutex); fd_set select_set; struct timeval timeRec; struct timeval * pxTimeRec; if(isTimerValid()) { if(m_iTimer) // If timer was set { MHTRACE(9,"Timer was set"); TimeInterval iTime=GetTimer(); timeRec.tv_sec = iTime/1000; timeRec.tv_usec= (iTime%1000)*1000; pxTimeRec=&timeRec; } else { MHTRACE(9,"Timer was not set"); pxTimeRec=NULL; } FD_ZERO( &select_set ); FD_SET( m_ihSocket, &select_set ); FD_SET( m_xPipeChan[0], &select_set ); int rc = select(FD_SETSIZE, &select_set, 0, 0, pxTimeRec ); if (rc > 0) { if(FD_ISSET(m_ihSocket, &select_set)) { MHTRACE(9,"Exiting because of socket activity, rc=" << rc); return; } if (FD_ISSET(m_xPipeChan[0], &select_set)) { MHTRACE(9,"Exiting because of pipe activity"); char c; MHTRACE(9,"Pipe Channel Command"); read(m_xPipeChan[0],&c,1); switch(c) { case 'c': MHTRACE(9,"Socket operation cancelled"); SOCKET_CANCELLED; break; case 's': MHTRACE(9,"Socket operation stopped"); SOCKET_STOPPED; break; default: SOCKET_EXCEPTION("Incorrect command from pipe channel",-1); break; } } } if (rc < 0 ) { MHTRACE(9,"Exiting because of select error"); SOCKET_EXCEPTION("Error in Select",errno); } } MHTRACE(9,"Timeout exception"); SOCKET_TIMEOUT;}//------------------------------------------------------------------------------//////------------------------------------------------------------------------------////void Socket::printOn(ostream & xStr){ IN ("Socket::printOn(ostream & )"); CriticalSection CrtSec(&m_xMutex); xStr << " Socket handler: " << m_ihSocket << endl; // Socket Descriptor xStr << " Domain : " << m_iDomain << endl; xStr << " Type : " << m_iType << endl; xStr << " Protocol : " << m_iProtocol << endl; xStr << " Timer : " << m_iTimer << endl; xStr << " StopReceived : " << m_bStopReceived << endl; if (m_iTimer) { xStr << " Time leave : " << GetTimer() << endl; } xStr << " * - - IPAddress - - * " << endl; xStr << m_xInetAddr << endl; }//------------------------------------------------------------------------------////Socket::~Socket (){ CriticalSection CrtSec(&m_xMutex); ::close(m_ihSocket); ::close(m_xPipeChan[0]); ::close(m_xPipeChan[1]);};//------------------------------------------------------------------------------////void Socket::SetTimer(TimeInterval Time){ IN ("Socket::SetTimer(TimeInterval)"); MHTRACE(9,"this="<<this); CriticalSection CrtSec(&m_xMutex); struct tms xTime; clock_t iClocks=times(&xTime); m_xInitTime.tv_sec=iClocks/CLK_TCK; m_xInitTime.tv_usec=(iClocks%CLK_TCK)*(1000000/CLK_TCK); m_iTimer=Time; MHTRACE(9, "Timer initiated on " << Time <<" msec" ); MHTRACE(9, "Current time is (sec) " << setprecision(12) << (double)m_xInitTime.tv_sec+(double)(m_xInitTime.tv_usec)/1000000);}//------------------------------------------------------------------------------////TimeInterval Socket::GetTimer(void){ IN ("Socket::GetTimer()"); struct timeval time; struct tms xTime; clock_t iClocks=times(&xTime); time.tv_sec=iClocks/CLK_TCK; time.tv_usec=(iClocks%CLK_TCK)*(1000000/CLK_TCK); TimeInterval diff= ((TimeInterval)time.tv_sec-(TimeInterval)m_xInitTime.tv_sec)*1000+ ((TimeInterval)time.tv_usec-(TimeInterval)m_xInitTime.tv_usec)/1000; MHTRACE(9,"Timer value " << m_iTimer-diff); return m_iTimer-diff;}//------------------------------------------------------------------------------////void Socket::ClearTimer(){ IN ("Socket::ClearTimer()"); m_iTimer=0; }//------------------------------------------------------------------------------////bool Socket::isTimerValid(){ IN("Socket::isTimerValid()"); if(m_iTimer && GetTimer()<=0) { return false; } if (m_iTimer) { TimeInterval tim=GetTimer(); // Do not optimize( for Correct Trace!!! ) MHTRACE(9,"Timer value " << tim); } else { MHTRACE(9,"Timer was not set"); } return true;}//------------------------------------------------------------------------------////bool Socket::isInputAvail(){ IN("Socket::isInputAvail()"); CriticalSection CrtSec(&m_xMutex); struct timeval timeRec; fd_set select_set; if (m_iTimer && GetTimer()<=0) { SOCKET_TIMEOUT; }; timeRec.tv_sec = 0; timeRec.tv_usec= 0; FD_ZERO( &select_set ); FD_SET( m_ihSocket, &select_set ); int rc = select(FD_SETSIZE, &select_set, 0, 0, &timeRec ); if (rc > 0 && FD_ISSET(m_ihSocket, &select_set)) { MHTRACE(9,"Returning true"); return true; } if (rc < 0 ) { SOCKET_EXCEPTION("Error in Select",errno); } MHTRACE(9,"Returning false"); return false;};void Socket::cancel(){ write(m_xPipeChan[1],"c",1);};void Socket::stopWaiting(){ write(m_xPipeChan[1],"s",1);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -