MOOS 0.2375
|
00001 00002 // 00003 // MOOS - Mission Oriented Operating Suite 00004 // 00005 // A suit of Applications and Libraries for Mobile Robotics Research 00006 // Copyright (C) 2001-2005 Massachusetts Institute of Technology and 00007 // Oxford University. 00008 // 00009 // This software was written by Paul Newman at MIT 2001-2002 and Oxford 00010 // University 2003-2005. email: pnewman@robots.ox.ac.uk. 00011 // 00012 // This file is part of a MOOS Core Component. 00013 // 00014 // This program is free software; you can redistribute it and/or 00015 // modify it under the terms of the GNU General Public License as 00016 // published by the Free Software Foundation; either version 2 of the 00017 // License, or (at your option) any later version. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 // General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00030 // MOOSCommObject.cpp: implementation of the CMOOSCommObject class. 00031 // 00033 #ifdef _WIN32 00034 #pragma warning(disable : 4786) 00035 #pragma warning(disable : 4503) 00036 #endif 00037 00038 #include <MOOSGenLib/MOOSGenLib.h> 00039 #include "MOOSCommPkt.h" 00040 #include "XPCTcpSocket.h" 00041 #include "MOOSCommObject.h" 00042 #include "MOOSException.h" 00043 #include <iostream> 00044 00046 // Construction/Destruction 00048 00049 CMOOSCommObject::CMOOSCommObject() 00050 { 00051 00052 } 00053 00054 CMOOSCommObject::~CMOOSCommObject() 00055 { 00056 00057 } 00058 00059 00060 bool CMOOSCommObject::ReadPkt(XPCTcpSocket *pSocket, CMOOSCommPkt &PktRx, int nSecondsTimeout) 00061 { 00062 #define CHUNK_READ 8192 00063 unsigned char Buffer[CHUNK_READ]; 00064 unsigned char *pBuffer = Buffer; 00065 00066 //now receive a message back.. 00067 int nRqd=0; 00068 while((nRqd=PktRx.GetBytesRequired())!=0) 00069 { 00070 int nRxd = 0; 00071 00072 try 00073 { 00074 if(nRqd<CHUNK_READ) 00075 { 00076 //read in in chunks of 1k 00077 if(nSecondsTimeout<0) 00078 { 00079 nRxd = pSocket->iRecieveMessage(pBuffer,nRqd); 00080 } 00081 else 00082 { 00083 nRxd = pSocket->iReadMessageWithTimeOut(pBuffer,nRqd,(double)nSecondsTimeout); 00084 } 00085 } 00086 else 00087 { 00088 //read in in chunks of 1k 00089 if(nSecondsTimeout<0) 00090 { 00091 nRxd = pSocket->iRecieveMessage(pBuffer,CHUNK_READ); 00092 } 00093 else 00094 { 00095 nRxd = pSocket->iReadMessageWithTimeOut(pBuffer,CHUNK_READ,(double)nSecondsTimeout); 00096 } 00097 } 00098 } 00099 catch(XPCException e) 00100 { 00101 MOOSTrace("Exception %s\n",e.sGetException()); 00102 throw CMOOSException("CMOOSCommObject::ReadPkt() Failed Rx"); 00103 } 00104 00105 switch(nRxd) 00106 { 00107 case -1: 00108 throw CMOOSException("Gross error...."); 00109 break; 00110 case 0: 00111 if(nSecondsTimeout>0) 00112 throw CMOOSException(MOOSFormat("remote side closed or lazy client ( waited more than %ds )",nSecondsTimeout)); 00113 else 00114 throw CMOOSException("remote side closed...."); 00115 break; 00116 default: 00117 PktRx.Fill(pBuffer,nRxd); 00118 break; 00119 } 00120 } 00121 00122 return true; 00123 } 00124 00125 bool CMOOSCommObject::SendPkt(XPCTcpSocket *pSocket, CMOOSCommPkt &PktTx) 00126 { 00127 int nSent = 0; 00128 00129 try 00130 { 00131 nSent = pSocket->iSendMessage(PktTx.m_pStream,PktTx.GetStreamLength()); 00132 } 00133 catch(XPCException e) 00134 { 00135 MOOSTrace("MOOSCommObject::SendPkt Exception caught %s\n",e.sGetException()); 00136 throw CMOOSException("CMOOSCommObject::SendPkt() Failed Tx"); 00137 } 00138 00139 if(nSent!=PktTx.GetStreamLength()) 00140 { 00141 throw CMOOSException("CMOOSCommObject::SendPkt() Failed Tx"); 00142 } 00143 00144 return true; 00145 } 00146 00147 bool CMOOSCommObject::SendMsg(XPCTcpSocket *pSocket,CMOOSMsg &Msg) 00148 { 00149 MOOSMSG_LIST MsgList; 00150 00151 MsgList.push_front(Msg); 00152 00153 CMOOSCommPkt Pkt; 00154 00155 Pkt.Serialize(MsgList,true); 00156 00157 00158 return SendPkt(pSocket,Pkt); 00159 } 00160 00161 00162 bool CMOOSCommObject::ReadMsg(XPCTcpSocket *pSocket,CMOOSMsg &Msg, int nSecondsTimeout) 00163 { 00164 MOOSMSG_LIST MsgList; 00165 00166 CMOOSCommPkt Pkt; 00167 00168 if(ReadPkt(pSocket,Pkt,nSecondsTimeout)) 00169 { 00170 00171 Pkt.Serialize(MsgList,false); 00172 00173 if(!MsgList.empty()) 00174 { 00175 Msg = MsgList.front(); 00176 } 00177 00178 } 00179 00180 00181 return !MsgList.empty(); 00182 } 00183 00184 00185 bool CMOOSCommObject::SocketsInit() 00186 { 00187 00188 #ifdef _WIN32 00189 WORD wVersionRequested; 00190 WSADATA wsaData; 00191 int err; 00192 00193 wVersionRequested = MAKEWORD( 2, 2 ); 00194 00195 err = WSAStartup( wVersionRequested, &wsaData ); 00196 if ( err != 0 ) { 00197 /* Tell the user that we could not find a usable */ 00198 /* WinSock DLL. */ 00199 00200 printf("failed socket init\n"); 00201 return false; 00202 } 00203 00204 /* Confirm that the WinSock DLL supports 2.2.*/ 00205 /* Note that if the DLL supports versions greater */ 00206 /* than 2.2 in addition to 2.2, it will still return */ 00207 /* 2.2 in wVersion since that is the version we */ 00208 /* requested. */ 00209 00210 if ( LOBYTE( wsaData.wVersion ) != 2 || 00211 HIBYTE( wsaData.wVersion ) != 2 ) { 00212 /* Tell the user that we could not find a usable */ 00213 /* WinSock DLL. 00214 */ 00215 00216 printf("failed socket init\n"); 00217 00218 WSACleanup( ); 00219 return false; 00220 } 00221 #endif 00222 return true; 00223 } 00224