ipc/unixsocket/UnixSocket.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla_ipc_UnixSocket_h
michael@0 8 #define mozilla_ipc_UnixSocket_h
michael@0 9
michael@0 10
michael@0 11 #include <stdlib.h>
michael@0 12 #include "nsAutoPtr.h"
michael@0 13 #include "nsString.h"
michael@0 14 #include "nsThreadUtils.h"
michael@0 15 #include "mozilla/ipc/UnixSocketWatcher.h"
michael@0 16 #include "mozilla/RefPtr.h"
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19 namespace ipc {
michael@0 20
michael@0 21 class UnixSocketRawData
michael@0 22 {
michael@0 23 public:
michael@0 24 // Number of octets in mData.
michael@0 25 size_t mSize;
michael@0 26 size_t mCurrentWriteOffset;
michael@0 27 nsAutoArrayPtr<uint8_t> mData;
michael@0 28
michael@0 29 /**
michael@0 30 * Constructor for situations where only size is known beforehand
michael@0 31 * (for example, when being assigned strings)
michael@0 32 */
michael@0 33 UnixSocketRawData(size_t aSize) :
michael@0 34 mSize(aSize),
michael@0 35 mCurrentWriteOffset(0)
michael@0 36 {
michael@0 37 mData = new uint8_t[mSize];
michael@0 38 }
michael@0 39
michael@0 40 /**
michael@0 41 * Constructor for situations where size and data is known
michael@0 42 * beforehand (for example, when being assigned strings)
michael@0 43 */
michael@0 44 UnixSocketRawData(const void* aData, size_t aSize)
michael@0 45 : mSize(aSize),
michael@0 46 mCurrentWriteOffset(0)
michael@0 47 {
michael@0 48 MOZ_ASSERT(aData || !mSize);
michael@0 49 mData = new uint8_t[mSize];
michael@0 50 memcpy(mData, aData, mSize);
michael@0 51 }
michael@0 52 };
michael@0 53
michael@0 54 class UnixSocketImpl;
michael@0 55
michael@0 56 /**
michael@0 57 * UnixSocketConnector defines the socket creation and connection/listening
michael@0 58 * functions for a UnixSocketConsumer. Due to the fact that socket setup can
michael@0 59 * vary between protocols (unix sockets, tcp sockets, bluetooth sockets, etc),
michael@0 60 * this allows the user to create whatever connection mechanism they need while
michael@0 61 * still depending on libevent for non-blocking communication handling.
michael@0 62 *
michael@0 63 * FIXME/Bug 793980: Currently only virtual, since we only support bluetooth.
michael@0 64 * Should make connection functions for other unix sockets so we can support
michael@0 65 * things like RIL.
michael@0 66 */
michael@0 67 class UnixSocketConnector
michael@0 68 {
michael@0 69 public:
michael@0 70 UnixSocketConnector()
michael@0 71 {}
michael@0 72
michael@0 73 virtual ~UnixSocketConnector()
michael@0 74 {}
michael@0 75
michael@0 76 /**
michael@0 77 * Establishs a file descriptor for a socket.
michael@0 78 *
michael@0 79 * @return File descriptor for socket
michael@0 80 */
michael@0 81 virtual int Create() = 0;
michael@0 82
michael@0 83 /**
michael@0 84 * Since most socket specifics are related to address formation into a
michael@0 85 * sockaddr struct, this function is defined by subclasses and fills in the
michael@0 86 * structure as needed for whatever connection it is trying to build
michael@0 87 *
michael@0 88 * @param aIsServer True is we are acting as a server socket
michael@0 89 * @param aAddrSize Size of the struct
michael@0 90 * @param aAddr Struct to fill
michael@0 91 * @param aAddress If aIsServer is false, Address to connect to. nullptr otherwise.
michael@0 92 *
michael@0 93 * @return True if address is filled correctly, false otherwise
michael@0 94 */
michael@0 95 virtual bool CreateAddr(bool aIsServer,
michael@0 96 socklen_t& aAddrSize,
michael@0 97 sockaddr_any& aAddr,
michael@0 98 const char* aAddress) = 0;
michael@0 99
michael@0 100 /**
michael@0 101 * Does any socket type specific setup that may be needed, only for socket
michael@0 102 * created by ConnectSocket()
michael@0 103 *
michael@0 104 * @param aFd File descriptor for opened socket
michael@0 105 *
michael@0 106 * @return true is successful, false otherwise
michael@0 107 */
michael@0 108 virtual bool SetUp(int aFd) = 0;
michael@0 109
michael@0 110 /**
michael@0 111 * Perform socket setup for socket created by ListenSocket(), after listen().
michael@0 112 *
michael@0 113 * @param aFd File descriptor for opened socket
michael@0 114 *
michael@0 115 * @return true is successful, false otherwise
michael@0 116 */
michael@0 117 virtual bool SetUpListenSocket(int aFd) = 0;
michael@0 118
michael@0 119 /**
michael@0 120 * Get address of socket we're currently connected to. Return null string if
michael@0 121 * not connected.
michael@0 122 *
michael@0 123 * @param aAddr Address struct
michael@0 124 * @param aAddrStr String to store address to
michael@0 125 */
michael@0 126 virtual void GetSocketAddr(const sockaddr_any& aAddr,
michael@0 127 nsAString& aAddrStr) = 0;
michael@0 128
michael@0 129 };
michael@0 130
michael@0 131 enum SocketConnectionStatus {
michael@0 132 SOCKET_DISCONNECTED = 0,
michael@0 133 SOCKET_LISTENING = 1,
michael@0 134 SOCKET_CONNECTING = 2,
michael@0 135 SOCKET_CONNECTED = 3
michael@0 136 };
michael@0 137
michael@0 138 class UnixSocketConsumer
michael@0 139 {
michael@0 140 protected:
michael@0 141 virtual ~UnixSocketConsumer();
michael@0 142
michael@0 143 public:
michael@0 144 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(UnixSocketConsumer)
michael@0 145
michael@0 146 UnixSocketConsumer();
michael@0 147
michael@0 148 SocketConnectionStatus GetConnectionStatus() const
michael@0 149 {
michael@0 150 MOZ_ASSERT(NS_IsMainThread());
michael@0 151 return mConnectionStatus;
michael@0 152 }
michael@0 153
michael@0 154 int GetSuggestedConnectDelayMs() const
michael@0 155 {
michael@0 156 MOZ_ASSERT(NS_IsMainThread());
michael@0 157 return mConnectDelayMs;
michael@0 158 }
michael@0 159
michael@0 160 /**
michael@0 161 * Function to be called whenever data is received. This is only called on the
michael@0 162 * main thread.
michael@0 163 *
michael@0 164 * @param aMessage Data received from the socket.
michael@0 165 */
michael@0 166 virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) = 0;
michael@0 167
michael@0 168 /**
michael@0 169 * Queue data to be sent to the socket on the IO thread. Can only be called on
michael@0 170 * originating thread.
michael@0 171 *
michael@0 172 * @param aMessage Data to be sent to socket
michael@0 173 *
michael@0 174 * @return true if data is queued, false otherwise (i.e. not connected)
michael@0 175 */
michael@0 176 bool SendSocketData(UnixSocketRawData* aMessage);
michael@0 177
michael@0 178 /**
michael@0 179 * Convenience function for sending strings to the socket (common in bluetooth
michael@0 180 * profile usage). Converts to a UnixSocketRawData struct. Can only be called
michael@0 181 * on originating thread.
michael@0 182 *
michael@0 183 * @param aMessage String to be sent to socket
michael@0 184 *
michael@0 185 * @return true if data is queued, false otherwise (i.e. not connected)
michael@0 186 */
michael@0 187 bool SendSocketData(const nsACString& aMessage);
michael@0 188
michael@0 189 /**
michael@0 190 * Starts a task on the socket that will try to connect to a socket in a
michael@0 191 * non-blocking manner.
michael@0 192 *
michael@0 193 * @param aConnector Connector object for socket type specific functions
michael@0 194 * @param aAddress Address to connect to.
michael@0 195 * @param aDelayMs Time delay in milli-seconds.
michael@0 196 *
michael@0 197 * @return true on connect task started, false otherwise.
michael@0 198 */
michael@0 199 bool ConnectSocket(UnixSocketConnector* aConnector,
michael@0 200 const char* aAddress,
michael@0 201 int aDelayMs = 0);
michael@0 202
michael@0 203 /**
michael@0 204 * Starts a task on the socket that will try to accept a new connection in a
michael@0 205 * non-blocking manner.
michael@0 206 *
michael@0 207 * @param aConnector Connector object for socket type specific functions
michael@0 208 *
michael@0 209 * @return true on listen started, false otherwise
michael@0 210 */
michael@0 211 bool ListenSocket(UnixSocketConnector* aConnector);
michael@0 212
michael@0 213 /**
michael@0 214 * Queues the internal representation of socket for deletion. Can be called
michael@0 215 * from main thread.
michael@0 216 */
michael@0 217 void CloseSocket();
michael@0 218
michael@0 219 /**
michael@0 220 * Callback for socket connect/accept success. Called after connect/accept has
michael@0 221 * finished. Will be run on main thread, before any reads take place.
michael@0 222 */
michael@0 223 virtual void OnConnectSuccess() = 0;
michael@0 224
michael@0 225 /**
michael@0 226 * Callback for socket connect/accept error. Will be run on main thread.
michael@0 227 */
michael@0 228 virtual void OnConnectError() = 0;
michael@0 229
michael@0 230 /**
michael@0 231 * Callback for socket disconnect. Will be run on main thread.
michael@0 232 */
michael@0 233 virtual void OnDisconnect() = 0;
michael@0 234
michael@0 235 /**
michael@0 236 * Called by implementation to notify consumer of success.
michael@0 237 */
michael@0 238 void NotifySuccess();
michael@0 239
michael@0 240 /**
michael@0 241 * Called by implementation to notify consumer of error.
michael@0 242 */
michael@0 243 void NotifyError();
michael@0 244
michael@0 245 /**
michael@0 246 * Called by implementation to notify consumer of disconnect.
michael@0 247 */
michael@0 248 void NotifyDisconnect();
michael@0 249
michael@0 250 /**
michael@0 251 * Get the current sockaddr for the socket
michael@0 252 */
michael@0 253 void GetSocketAddr(nsAString& aAddrStr);
michael@0 254
michael@0 255 private:
michael@0 256 uint32_t CalculateConnectDelayMs() const;
michael@0 257
michael@0 258 UnixSocketImpl* mImpl;
michael@0 259 SocketConnectionStatus mConnectionStatus;
michael@0 260 PRIntervalTime mConnectTimestamp;
michael@0 261 uint32_t mConnectDelayMs;
michael@0 262 };
michael@0 263
michael@0 264 } // namespace ipc
michael@0 265 } // namepsace mozilla
michael@0 266
michael@0 267 #endif // mozilla_ipc_Socket_h

mercurial