ipc/unixfd/UnixSocketWatcher.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_ipc_UnixSocketWatcher_h
     8 #define mozilla_ipc_UnixSocketWatcher_h
    10 #include <sys/socket.h>
    11 #include <sys/types.h>
    12 #include <sys/un.h>
    13 #include <netinet/in.h>
    14 #ifdef MOZ_B2G_BT_BLUEZ
    15 #include <bluetooth/bluetooth.h>
    16 #include <bluetooth/sco.h>
    17 #include <bluetooth/l2cap.h>
    18 #include <bluetooth/rfcomm.h>
    19 #endif
    20 #include "UnixFdWatcher.h"
    22 namespace mozilla {
    23 namespace ipc {
    25 union sockaddr_any {
    26   sockaddr_storage storage; // address-family only
    27   sockaddr_un un;
    28   sockaddr_in in;
    29   sockaddr_in6 in6;
    30 #ifdef MOZ_B2G_BT_BLUEZ
    31   sockaddr_sco sco;
    32   sockaddr_rc rc;
    33   sockaddr_l2 l2;
    34 #endif
    35   // ... others
    36 };
    38 class UnixSocketWatcher : public UnixFdWatcher
    39 {
    40 public:
    41   enum ConnectionStatus {
    42     SOCKET_IS_DISCONNECTED = 0,
    43     SOCKET_IS_LISTENING,
    44     SOCKET_IS_CONNECTING,
    45     SOCKET_IS_CONNECTED
    46   };
    48   virtual ~UnixSocketWatcher();
    50   virtual void Close() MOZ_OVERRIDE;
    52   ConnectionStatus GetConnectionStatus() const
    53   {
    54     return mConnectionStatus;
    55   }
    57   // Connect to a peer
    58   nsresult Connect(const struct sockaddr* aAddr, socklen_t aAddrLen);
    60   // Listen on socket for incoming connection requests
    61   nsresult Listen(const struct sockaddr* aAddr, socklen_t aAddrLen);
    63   // Callback method for accepted connections
    64   virtual void OnAccepted(int aFd, const sockaddr_any* aAddr,
    65                           socklen_t aAddrLen) {};
    67   // Callback method for successful connection requests
    68   virtual void OnConnected() {};
    70   // Callback method for successful listen requests
    71   virtual void OnListening() {};
    73   // Callback method for receiving from socket
    74   virtual void OnSocketCanReceiveWithoutBlocking() {};
    76   // Callback method for sending on socket
    77   virtual void OnSocketCanSendWithoutBlocking() {};
    79 protected:
    80   UnixSocketWatcher(MessageLoop* aIOLoop);
    81   UnixSocketWatcher(MessageLoop* aIOLoop, int aFd,
    82                     ConnectionStatus aConnectionStatus);
    83   void SetSocket(int aFd, ConnectionStatus aConnectionStatus);
    85 private:
    86   void OnFileCanReadWithoutBlocking(int aFd) MOZ_OVERRIDE;
    87   void OnFileCanWriteWithoutBlocking(int aFd) MOZ_OVERRIDE;
    89   ConnectionStatus mConnectionStatus;
    90 };
    92 }
    93 }
    95 #endif

mercurial