interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/libs/vos/vos/remotesocketsite.hh

Go to the documentation of this file.
00001 #ifndef _REMOTESOCKETSITE_HH_
00002 #define _REMOTESOCKETSITE_HH_
00003 
00004 #include <stdexcept>
00005 
00006 #include <vos/vos/siteextension.hh>
00007 #include <vos/vos/message.hh>
00008 #include <vos/vos/messageblock.hh>
00009 #include <vos/vos/remotesite.hh>
00010 
00011 namespace VOS {
00012     /** @class RemoteSocketSiteExtension remotesocketsite.hh vos/vos/remotesocketsite.hh
00013         @ingroup libvos
00014 
00015         Extends a remote site to support communication over a TCP/IP socket.
00016     */
00017     class VOS_API RemoteSocketSiteExtension : public RemoteSiteExtension
00018     {
00019     private:
00020         int socketFD;
00021         VUtil::vRef<MessageBlock> partialMessage;
00022         unsigned int sockSendBufSz;
00023         boost::mutex outputBuffer_mutex;
00024         std::string outputBuffer;
00025         Site* remotesite;
00026         std::string hostname;
00027         std::string remoteport;
00028 
00029     public:
00030         /** Initialize the site extension with an existing connection.
00031             @param socket the socket file descriptor
00032             @param peername the socket address info about the connected peer
00033         */
00034         RemoteSocketSiteExtension(int socket, sockaddr_in* peername);
00035 
00036         /** Make an outgoing connection to the supplied hostname, on
00037             the supplied port.
00038             @throw NoSuchSiteError if the connection could not be made
00039          */
00040         RemoteSocketSiteExtension(const std::string& hostname, unsigned short int port);
00041 
00042         /** Destructor */
00043         virtual ~RemoteSocketSiteExtension();
00044 
00045         /** Called when this extension is attached to the site */
00046         virtual void siteExtensionAttachedTo(Site* st);
00047 
00048         /** @return the remote site this extension is attached to */
00049         VUtil::vRef<Site> getRemoteSite() { return VUtil::vRef<Site>(remotesite, true); }
00050 
00051         /** @return the socket file descriptor */
00052         int getSocket() { return socketFD; }
00053 
00054         /** @return true if there is buffered data that is waiting to be sent */
00055         bool needWriteFlush() { return (outputBuffer.size() > 0); }
00056 
00057         /** @param data the buffer to store the data read from the socket
00058             @param datasize the maximum number of bytes to read
00059             @return the number of bytes actually read
00060         */
00061         int readStream(char* data, unsigned int datasize);
00062 
00063         /** @param data the buffer to be written to the socket
00064             @param datasize the number of bytes to be written
00065             @return the actual number of bytes that were written
00066         */
00067         int writeStream(const char* data, unsigned int datasize);
00068 
00069         /** Cleans up after the socket disconnects. */
00070         void handleDisconnection();
00071 
00072         /** Reads and parses any messages pending on the socket.
00073             @return a list of messages/message blocks
00074         */
00075         virtual std::list< VUtil::vRef<MessageBlock> > readQueuedMessages(bool block);
00076 
00077         /** Writes data to the socket until it has flushed the
00078             outgoing data buffer or gets EWOULDBLOCK (indicating that
00079             operating system socket buffers are full).
00080 
00081             @param newdata data to send; will be appended to any old data still in the
00082             output buffer
00083             @param newsize the size of the data to send
00084          */
00085         void pushOutgoingBuffer(const char* newdata = 0, unsigned int newsize = 0);
00086 
00087         /** Actually send a message to the socket
00088             @param state the vobjectstate for the site object.  not used.
00089             @param m the message
00090          */
00091         virtual void doSendMessage(VobjectState& state, Message* m);
00092 
00093 
00094         /** Actually send a message block to the socket
00095             @param state the vobjectstate for the site object.  not used.
00096             @param m the message block
00097          */
00098         virtual void doSendMessage(VobjectState& state, MessageBlock* m);
00099 
00100         virtual bool isConnected();
00101     };
00102 };
00103 
00104 #ifndef VOS_EXPORTS
00105 IMPORT_METAOBJECT_FACTORIES(SocketProtocolHandler)
00106 #endif
00107 
00108 #endif