fuse_transceiver.cpp
00001 00002 /*************************************************************************** 00003 * fuse_transceiver.cpp - Fuse transceiver 00004 * 00005 * Created: Wed Nov 14 13:30:34 2007 00006 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <fvutils/net/fuse_transceiver.h> 00025 #include <fvutils/net/fuse_message_queue.h> 00026 #include <fvutils/net/fuse_message.h> 00027 #include <netcomm/socket/stream.h> 00028 #include <netcomm/utils/exceptions.h> 00029 00030 #include <netinet/in.h> 00031 #include <cstdlib> 00032 00033 using namespace fawkes; 00034 00035 /** @class FuseNetworkTransceiver <fvutils/net/fuse_transceiver.h> 00036 * FUSE Network Transceiver. 00037 * Utility class that provides methods to send and receive messages via 00038 * the network. Operates on message queues and a given socket. 00039 * 00040 * @ingroup FUSE 00041 * @ingroup FireVision 00042 * @author Tim Niemueller 00043 */ 00044 00045 /** Send messages. 00046 * @param s socket over which the data shall be transmitted. 00047 * @param msgq message queue that contains the messages that have to be sent 00048 * @exception ConnectionDiedException Thrown if any error occurs during the 00049 * operation since for any error the conncetion is considered dead. 00050 */ 00051 void 00052 FuseNetworkTransceiver::send(StreamSocket *s, FuseNetworkMessageQueue *msgq) 00053 { 00054 msgq->lock(); 00055 try { 00056 while ( ! msgq->empty() ) { 00057 FuseNetworkMessage *m = msgq->front(); 00058 m->pack(); 00059 const FUSE_message_t &f = m->fmsg(); 00060 unsigned int payload_size = m->payload_size(); 00061 s->write(&(f.header), sizeof(f.header)); 00062 s->write(f.payload, payload_size); 00063 m->unref(); 00064 msgq->pop(); 00065 } 00066 } catch (SocketException &e) { 00067 msgq->unlock(); 00068 throw ConnectionDiedException("Write failed"); 00069 } 00070 msgq->unlock(); 00071 } 00072 00073 00074 /** Receive data. 00075 * This method receives all messages currently available from the network, or 00076 * a limited number depending on max_num_msgs. If max_num_msgs is 0 then all 00077 * messages are read. Note that on a busy connection this may cause recv() to 00078 * never return! The default is to return after 8 messages. 00079 * The messages are stored in the supplied message queue. 00080 * @param s socket to gather messages from 00081 * @param msgq message queue to store received messages in 00082 * @param max_num_msgs maximum number of messages to read from stream in one go. 00083 * @exception ConnectionDiedException Thrown if any error occurs during the 00084 * operation since for any error the conncetion is considered dead. 00085 */ 00086 void 00087 FuseNetworkTransceiver::recv(StreamSocket *s, FuseNetworkMessageQueue *msgq, 00088 unsigned int max_num_msgs) 00089 { 00090 msgq->lock(); 00091 00092 try { 00093 unsigned int num_msgs = 0; 00094 do { 00095 FUSE_message_t msg; 00096 s->read(&(msg.header), sizeof(msg.header)); 00097 00098 unsigned int payload_size = ntohl(msg.header.payload_size); 00099 00100 if ( payload_size > 0 ) { 00101 00102 msg.payload = malloc(payload_size); 00103 s->read(msg.payload, payload_size); 00104 } else { 00105 msg.payload = NULL; 00106 } 00107 00108 FuseNetworkMessage *m = new FuseNetworkMessage(&msg); 00109 msgq->push(m); 00110 00111 ++num_msgs; 00112 } while ( s->available() && (num_msgs < max_num_msgs) ); 00113 } catch (SocketException &e) { 00114 msgq->unlock(); 00115 throw ConnectionDiedException("Read failed"); 00116 } 00117 msgq->unlock(); 00118 }

