Urbi SDK Remote for C++  2.7.5
uclient.hh
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2006-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00012 
00013 #ifndef URBI_UCLIENT_HH
00014 # define URBI_UCLIENT_HH
00015 
00016 # include <libport/asio.hh>
00017 # include <libport/pthread.h>
00018 # include <libport/semaphore.hh>
00019 # include <libport/utime.hh>
00020 
00021 # include <urbi/uabstractclient.hh>
00022 
00023 namespace urbi
00024 {
00026 
00029   class URBI_SDK_API UClient
00030     : public UAbstractClient
00031     , public libport::Socket
00032   {
00033   public:
00035     struct URBI_SDK_API options
00036     {
00043       options(bool server = false);
00044 # define UCLIENT_OPTION(Type, Name)             \
00045     public:                                     \
00046       options& Name(Type b);                    \
00047       Type Name() const;                        \
00048     private:                                    \
00049       Type Name ## _;
00050 
00052       UCLIENT_OPTION(bool, server);
00054       UCLIENT_OPTION(bool, start);
00056       UCLIENT_OPTION(bool, asynchronous);
00057     };
00058 
00059 # define UCLIENT_OPTION_IMPL(Class, Type, Name) \
00060   Class::options&                               \
00061   Class::options::Name(Type v)                  \
00062   {                                             \
00063     Name ## _ = v;                              \
00064     return *this;                               \
00065   }                                             \
00066                                                 \
00067   Type                                          \
00068   Class::options::Name() const                  \
00069   {                                             \
00070     return Name ## _;                           \
00071   }
00072 
00080     UClient(const std::string& host = default_host(),
00081             unsigned port = URBI_PORT,
00082             size_t buflen = URBI_BUFLEN,
00083             // FIXME: See http://llvm.org/bugs/show_bug.cgi?id=8692,
00084             // revert this in the future.
00085             // FIXME: ndmefyl: changed from options::options(false) -
00086             // wtf a direct call to a ctor - which doesn't work - at
00087             // least with 4.5.1 - to option(false). If this is related
00088             // to the LLVM bug, please check you're not breaking gcc
00089             // compilation while fixing.
00090             const UClient::options& opt = options(false));
00091 
00092     virtual ~UClient();
00093 
00094     virtual void waitForKernelVersion() const;
00095 
00097     error_type start();
00098   protected:
00099     virtual error_type onClose();
00100 
00101   public:
00102     int closeUClient ();
00103 
00104     ATTRIBUTE_PRINTF(2, 3)
00105     virtual void printf(const char* format, ...);
00106 
00107     virtual unsigned int getCurrentTime() const;
00108 
00112     virtual void setKeepAliveCheck(unsigned pingInterval,
00113                                    unsigned pongTimeout);
00114 
00115     using UAbstractClient::send;
00116 
00118     void setSynchronousSend(bool enable);
00119   protected:
00120     virtual int effectiveSend(const void* buffer, size_t size);
00121 
00122     libport::Socket* mySocketFactory();
00123 
00124     virtual void onConnect();
00125 
00126     virtual void onError(boost::system::error_code erc);
00127 
00128     virtual size_t onRead(const void*, size_t length);
00129 
00130   protected:
00133     libport::utime_t ping_interval_;
00134 
00136     libport::utime_t pong_timeout_;
00137 
00138     typedef boost::shared_ptr<UClient*> link_type;
00139     link_type link_;
00140 
00142     void pongTimeout(link_type l);
00143 
00144     void sendPing(link_type l);
00145 
00146     libport::utime_t ping_sent_;
00147 
00148     libport::AsyncCallHandler pong_timeout_handler_;
00149     libport::AsyncCallHandler send_ping_handler_;
00150 
00151     libport::Semaphore ping_sem_;
00152 
00153   private:
00156     error_type connect_();
00159     error_type listen_();
00161     void resetAsyncCalls_();
00163     bool asynchronous_;
00165     bool synchronous_send_;
00166   };
00167 
00168 } // namespace urbi
00169 #endif