Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages | Examples

ArNetPacketReceiverUdp.cpp

Go to the documentation of this file.
00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 #include "ArExport.h"
00028 #include "ArNetPacketReceiverUdp.h"
00029 #ifndef WIN32
00030 #include <errno.h>
00031 #endif
00032 
00033 AREXPORT ArNetPacketReceiverUdp::ArNetPacketReceiverUdp()
00034 {
00035   mySocket = NULL;
00036   myProcessPacketCB = NULL;
00037   // one little odd note, this calls setBuf on the packet so that its
00038   // using our own buffer and not its own... this is the only place we
00039   // don't want it to own the buffer
00040   myPacket.setBuf(myBuff, sizeof(myBuff));
00041 }
00042 
00043 AREXPORT ArNetPacketReceiverUdp::~ArNetPacketReceiverUdp()
00044 {
00045 
00046 }
00047 
00054 AREXPORT void ArNetPacketReceiverUdp::setSocket(ArSocket *socket)
00055 {
00056   mySocket = socket;
00057 }
00058 
00063 AREXPORT ArSocket *ArNetPacketReceiverUdp::getSocket(void)
00064 {
00065   return mySocket;
00066 }
00067 
00068 
00072 AREXPORT void ArNetPacketReceiverUdp::setProcessPacketCB(
00073         ArFunctor2<ArNetPacket *, struct sockaddr_in *> *functor)
00074 {
00075   myProcessPacketCB = functor;
00076 }
00077 
00081 AREXPORT ArFunctor2<ArNetPacket *, struct sockaddr_in *> *ArNetPacketReceiverUdp::getProcessPacketCB(void)
00082 {
00083   return myProcessPacketCB;
00084 }
00085 
00086 AREXPORT bool ArNetPacketReceiverUdp::readData(void)
00087 {
00088   int ret;
00089   int packetLength;
00090   struct sockaddr_in sin;
00091 
00092   if (mySocket == NULL)
00093   {
00094     ArLog::log(ArLog::Verbose, "NULL Udp Socket");
00095     return false;
00096   }
00097   // while we can read a packet, do it
00098   while ((ret = mySocket->recvFrom(myBuff, sizeof(myBuff), &sin)) > 0)
00099   {
00100     packetLength = (myBuff[3] << 8) | myBuff[2] & 0xff;
00101     if (ret != packetLength)
00102       fprintf(stderr, "packet length %d not equal to udp packet length %d",
00103               packetLength, ret);
00104     myPacket.setLength(packetLength);
00105     myPacket.setBuf(myBuff, sizeof(myBuff));
00106     if (myPacket.verifyCheckSum()) 
00107     {
00108       myPacket.resetRead();
00109       /* put this in if you want to see the packets received
00110          //printf("Input ");
00111          myPacket.log();
00112       */
00113       // you can also do this next line if you only care about type
00114       //printf("Input %x\n", myPacket.getCommand());
00115       //myPacket.log();
00116       myPacket.resetRead();
00117       if (myProcessPacketCB != NULL)
00118       {
00119         myPacket.setPacketSource(ArNetPacket::UDP);
00120         myProcessPacketCB->invoke(&myPacket, &sin);
00121       }
00122     }
00123     else 
00124     {
00125       myPacket.resetRead();
00126       ArLog::log(ArLog::Normal, 
00127                  "ArNetPacketReceiverUdp::receivePacket: bad packet, bad checksum on packet %d", myPacket.getCommand());
00128     }
00129     
00130   }
00131 
00132 
00133   
00134   // see why we ran out of packets, if its bad return false so that it
00135   // is known the socket is bad now
00136   if (ret < 0)
00137   {
00138 #ifdef WIN32
00139     if (WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAECONNRESET)
00140       return true;
00141     else
00142       ArLog::log(ArLog::Terse, "Failed on read UDP, error %d", WSAGetLastError());
00143 #endif
00144 #ifndef WIN32
00145     if (errno == EAGAIN)
00146       return true;
00147     else
00148       ArLog::log(ArLog::Terse, "Failed on read UDP, error %d", errno);
00149 #endif
00150     ArLog::log(ArLog::Terse, "Failed on the udp read");
00151     return false;
00152   }
00153   else if (ret == 0)
00154   {
00155     printf("Read 0 byte UDP packet\n");
00156     return true;
00157   }
00158 
00159   return true;
00160   
00161 }

Generated on Tue Feb 20 10:51:50 2007 for ArNetworking by  doxygen 1.4.0