1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/unixfd/UnixSocketWatcher.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_ipc_UnixSocketWatcher_h 1.11 +#define mozilla_ipc_UnixSocketWatcher_h 1.12 + 1.13 +#include <sys/socket.h> 1.14 +#include <sys/types.h> 1.15 +#include <sys/un.h> 1.16 +#include <netinet/in.h> 1.17 +#ifdef MOZ_B2G_BT_BLUEZ 1.18 +#include <bluetooth/bluetooth.h> 1.19 +#include <bluetooth/sco.h> 1.20 +#include <bluetooth/l2cap.h> 1.21 +#include <bluetooth/rfcomm.h> 1.22 +#endif 1.23 +#include "UnixFdWatcher.h" 1.24 + 1.25 +namespace mozilla { 1.26 +namespace ipc { 1.27 + 1.28 +union sockaddr_any { 1.29 + sockaddr_storage storage; // address-family only 1.30 + sockaddr_un un; 1.31 + sockaddr_in in; 1.32 + sockaddr_in6 in6; 1.33 +#ifdef MOZ_B2G_BT_BLUEZ 1.34 + sockaddr_sco sco; 1.35 + sockaddr_rc rc; 1.36 + sockaddr_l2 l2; 1.37 +#endif 1.38 + // ... others 1.39 +}; 1.40 + 1.41 +class UnixSocketWatcher : public UnixFdWatcher 1.42 +{ 1.43 +public: 1.44 + enum ConnectionStatus { 1.45 + SOCKET_IS_DISCONNECTED = 0, 1.46 + SOCKET_IS_LISTENING, 1.47 + SOCKET_IS_CONNECTING, 1.48 + SOCKET_IS_CONNECTED 1.49 + }; 1.50 + 1.51 + virtual ~UnixSocketWatcher(); 1.52 + 1.53 + virtual void Close() MOZ_OVERRIDE; 1.54 + 1.55 + ConnectionStatus GetConnectionStatus() const 1.56 + { 1.57 + return mConnectionStatus; 1.58 + } 1.59 + 1.60 + // Connect to a peer 1.61 + nsresult Connect(const struct sockaddr* aAddr, socklen_t aAddrLen); 1.62 + 1.63 + // Listen on socket for incoming connection requests 1.64 + nsresult Listen(const struct sockaddr* aAddr, socklen_t aAddrLen); 1.65 + 1.66 + // Callback method for accepted connections 1.67 + virtual void OnAccepted(int aFd, const sockaddr_any* aAddr, 1.68 + socklen_t aAddrLen) {}; 1.69 + 1.70 + // Callback method for successful connection requests 1.71 + virtual void OnConnected() {}; 1.72 + 1.73 + // Callback method for successful listen requests 1.74 + virtual void OnListening() {}; 1.75 + 1.76 + // Callback method for receiving from socket 1.77 + virtual void OnSocketCanReceiveWithoutBlocking() {}; 1.78 + 1.79 + // Callback method for sending on socket 1.80 + virtual void OnSocketCanSendWithoutBlocking() {}; 1.81 + 1.82 +protected: 1.83 + UnixSocketWatcher(MessageLoop* aIOLoop); 1.84 + UnixSocketWatcher(MessageLoop* aIOLoop, int aFd, 1.85 + ConnectionStatus aConnectionStatus); 1.86 + void SetSocket(int aFd, ConnectionStatus aConnectionStatus); 1.87 + 1.88 +private: 1.89 + void OnFileCanReadWithoutBlocking(int aFd) MOZ_OVERRIDE; 1.90 + void OnFileCanWriteWithoutBlocking(int aFd) MOZ_OVERRIDE; 1.91 + 1.92 + ConnectionStatus mConnectionStatus; 1.93 +}; 1.94 + 1.95 +} 1.96 +} 1.97 + 1.98 +#endif