Home | History | Annotate | Download | only in dev
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef PPAPI_CPP_EXTENSIONS_DEV_SOCKET_DEV_H_
      6 #define PPAPI_CPP_EXTENSIONS_DEV_SOCKET_DEV_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "ppapi/c/extensions/dev/ppb_ext_socket_dev.h"
     12 #include "ppapi/cpp/extensions/dict_field.h"
     13 #include "ppapi/cpp/extensions/ext_output_traits.h"
     14 #include "ppapi/cpp/instance_handle.h"
     15 #include "ppapi/cpp/var.h"
     16 #include "ppapi/cpp/var_array_buffer.h"
     17 #include "ppapi/cpp/var_dictionary.h"
     18 
     19 namespace pp {
     20 namespace ext {
     21 
     22 template <class T>
     23 class ExtCompletionCallbackWithOutput;
     24 
     25 template <class T>
     26 class Optional;
     27 
     28 namespace socket {
     29 
     30 // Data types ------------------------------------------------------------------
     31 class SocketType_Dev {
     32  public:
     33   enum ValueType {
     34     NONE,
     35     TCP,
     36     UDP
     37   };
     38 
     39   SocketType_Dev();
     40   SocketType_Dev(ValueType in_value);
     41   ~SocketType_Dev();
     42 
     43   bool Populate(const PP_Var& var_value);
     44 
     45   Var CreateVar() const;
     46 
     47   ValueType value;
     48 
     49   static const char* const kTcp;
     50   static const char* const kUdp;
     51 };
     52 
     53 typedef VarDictionary CreateOptions_Dev;
     54 
     55 class CreateInfo_Dev {
     56  public:
     57   CreateInfo_Dev();
     58   ~CreateInfo_Dev();
     59 
     60   bool Populate(const PP_Ext_Socket_CreateInfo_Dev& value);
     61 
     62   Var CreateVar() const;
     63 
     64   static const char* const kSocketId;
     65 
     66   DictField<int32_t> socket_id;
     67 };
     68 
     69 class AcceptInfo_Dev {
     70  public:
     71   AcceptInfo_Dev();
     72   ~AcceptInfo_Dev();
     73 
     74   bool Populate(const PP_Ext_Socket_AcceptInfo_Dev& value);
     75 
     76   Var CreateVar() const;
     77 
     78   static const char* const kResultCode;
     79   static const char* const kSocketId;
     80 
     81   DictField<int32_t> result_code;
     82   OptionalDictField<int32_t> socket_id;
     83 };
     84 
     85 class ReadInfo_Dev {
     86  public:
     87   ReadInfo_Dev();
     88   ~ReadInfo_Dev();
     89 
     90   bool Populate(const PP_Ext_Socket_ReadInfo_Dev& value);
     91 
     92   Var CreateVar() const;
     93 
     94   static const char* const kResultCode;
     95   static const char* const kData;
     96 
     97   DictField<int32_t> result_code;
     98   DictField<VarArrayBuffer> data;
     99 };
    100 
    101 class WriteInfo_Dev {
    102  public:
    103   WriteInfo_Dev();
    104   ~WriteInfo_Dev();
    105 
    106   bool Populate(const PP_Ext_Socket_WriteInfo_Dev& value);
    107 
    108   Var CreateVar() const;
    109 
    110   static const char* const kBytesWritten;
    111 
    112   DictField<int32_t> bytes_written;
    113 };
    114 
    115 class RecvFromInfo_Dev {
    116  public:
    117   RecvFromInfo_Dev();
    118   ~RecvFromInfo_Dev();
    119 
    120   bool Populate(const PP_Ext_Socket_RecvFromInfo_Dev& value);
    121 
    122   Var CreateVar() const;
    123 
    124   static const char* const kResultCode;
    125   static const char* const kData;
    126   static const char* const kAddress;
    127   static const char* const kPort;
    128 
    129   DictField<int32_t> result_code;
    130   DictField<VarArrayBuffer> data;
    131   DictField<std::string> address;
    132   DictField<int32_t> port;
    133 };
    134 
    135 class SocketInfo_Dev {
    136  public:
    137   SocketInfo_Dev();
    138   ~SocketInfo_Dev();
    139 
    140   bool Populate(const PP_Ext_Socket_SocketInfo_Dev& value);
    141 
    142   Var CreateVar() const;
    143 
    144   static const char* const kSocketType;
    145   static const char* const kConnected;
    146   static const char* const kPeerAddress;
    147   static const char* const kPeerPort;
    148   static const char* const kLocalAddress;
    149   static const char* const kLocalPort;
    150 
    151   DictField<SocketType_Dev> socket_type;
    152   DictField<bool> connected;
    153   OptionalDictField<std::string> peer_address;
    154   OptionalDictField<int32_t> peer_port;
    155   OptionalDictField<std::string> local_address;
    156   OptionalDictField<int32_t> local_port;
    157 };
    158 
    159 class NetworkInterface_Dev {
    160  public:
    161   NetworkInterface_Dev();
    162   ~NetworkInterface_Dev();
    163 
    164   bool Populate(const PP_Ext_Socket_NetworkInterface_Dev& value);
    165 
    166   Var CreateVar() const;
    167 
    168   static const char* const kName;
    169   static const char* const kAddress;
    170 
    171   DictField<std::string> name;
    172   DictField<std::string> address;
    173 };
    174 
    175 // Functions -------------------------------------------------------------------
    176 class Socket_Dev {
    177  public:
    178   explicit Socket_Dev(const InstanceHandle& instance);
    179   ~Socket_Dev();
    180 
    181   typedef ExtCompletionCallbackWithOutput<CreateInfo_Dev> CreateCallback;
    182   int32_t Create(const SocketType_Dev& type,
    183                  const Optional<CreateOptions_Dev>& options,
    184                  const CreateCallback& callback);
    185 
    186   void Destroy(int32_t socket_id);
    187 
    188   typedef ExtCompletionCallbackWithOutput<int32_t> ConnectCallback;
    189   int32_t Connect(int32_t socket_id,
    190                   const std::string& hostname,
    191                   int32_t port,
    192                   const ConnectCallback& callback);
    193 
    194   typedef ExtCompletionCallbackWithOutput<int32_t> BindCallback;
    195   int32_t Bind(int32_t socket_id,
    196                const std::string& address,
    197                int32_t port,
    198                const BindCallback& callback);
    199 
    200   void Disconnect(int32_t socket_id);
    201 
    202   typedef ExtCompletionCallbackWithOutput<ReadInfo_Dev> ReadCallback;
    203   int32_t Read(int32_t socket_id,
    204                const Optional<int32_t>& buffer_size,
    205                const ReadCallback& callback);
    206 
    207   typedef ExtCompletionCallbackWithOutput<WriteInfo_Dev> WriteCallback;
    208   int32_t Write(int32_t socket_id,
    209                 const VarArrayBuffer& data,
    210                 const WriteCallback& callback);
    211 
    212   typedef ExtCompletionCallbackWithOutput<RecvFromInfo_Dev> RecvFromCallback;
    213   int32_t RecvFrom(int32_t socket_id,
    214                    const Optional<int32_t>& buffer_size,
    215                    const RecvFromCallback& callback);
    216 
    217   typedef ExtCompletionCallbackWithOutput<WriteInfo_Dev> SendToCallback;
    218   int32_t SendTo(int32_t socket_id,
    219                  const VarArrayBuffer& data,
    220                  const std::string& address,
    221                  int32_t port,
    222                  const SendToCallback& callback);
    223 
    224   typedef ExtCompletionCallbackWithOutput<int32_t> ListenCallback;
    225   int32_t Listen(int32_t socket_id,
    226                  const std::string& address,
    227                  int32_t port,
    228                  const Optional<int32_t>& backlog,
    229                  const ListenCallback& callback);
    230 
    231   typedef ExtCompletionCallbackWithOutput<AcceptInfo_Dev> AcceptCallback;
    232   int32_t Accept(int32_t socket_id, const AcceptCallback& callback);
    233 
    234   typedef ExtCompletionCallbackWithOutput<bool> SetKeepAliveCallback;
    235   int32_t SetKeepAlive(int32_t socket_id,
    236                        bool enable,
    237                        const Optional<int32_t>& delay,
    238                        const SetKeepAliveCallback& callback);
    239 
    240   typedef ExtCompletionCallbackWithOutput<bool> SetNoDelayCallback;
    241   int32_t SetNoDelay(int32_t socket_id,
    242                      bool no_delay,
    243                      const SetNoDelayCallback& callback);
    244 
    245   typedef ExtCompletionCallbackWithOutput<SocketInfo_Dev> GetInfoCallback;
    246   int32_t GetInfo(int32_t socket_id,
    247                   const GetInfoCallback& callback);
    248 
    249   typedef ExtCompletionCallbackWithOutput<std::vector<NetworkInterface_Dev> >
    250       GetNetworkListCallback;
    251   int32_t GetNetworkList(const GetNetworkListCallback& callback);
    252 
    253   typedef ExtCompletionCallbackWithOutput<int32_t> JoinGroupCallback;
    254   int32_t JoinGroup(int32_t socket_id,
    255                     const std::string& address,
    256                     const JoinGroupCallback& callback);
    257 
    258   typedef ExtCompletionCallbackWithOutput<int32_t> LeaveGroupCallback;
    259   int32_t LeaveGroup(int32_t socket_id,
    260                      const std::string& address,
    261                      const LeaveGroupCallback& callback);
    262 
    263   typedef ExtCompletionCallbackWithOutput<int32_t>
    264       SetMulticastTimeToLiveCallback;
    265   int32_t SetMulticastTimeToLive(
    266       int32_t socket_id,
    267       int32_t ttl,
    268       const SetMulticastTimeToLiveCallback& callback);
    269 
    270   typedef ExtCompletionCallbackWithOutput<int32_t>
    271       SetMulticastLoopbackModeCallback;
    272   int32_t SetMulticastLoopbackMode(
    273       int32_t socket_id,
    274       bool enabled,
    275       const SetMulticastLoopbackModeCallback& callback);
    276 
    277   typedef ExtCompletionCallbackWithOutput<std::vector<std::string> >
    278       GetJoinedGroupsCallback;
    279   int32_t GetJoinedGroups(int32_t socket_id,
    280                           const GetJoinedGroupsCallback& callback);
    281 
    282  private:
    283   InstanceHandle instance_;
    284 };
    285 
    286 }  // namespace socket
    287 }  // namespace ext
    288 }  // namespace pp
    289 
    290 #endif  // PPAPI_CPP_EXTENSIONS_DEV_SOCKET_DEV_H_
    291