|
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/. */ |
|
6 |
|
7 #ifndef mozilla_ipc_UnixSocketWatcher_h |
|
8 #define mozilla_ipc_UnixSocketWatcher_h |
|
9 |
|
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" |
|
21 |
|
22 namespace mozilla { |
|
23 namespace ipc { |
|
24 |
|
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 }; |
|
37 |
|
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 }; |
|
47 |
|
48 virtual ~UnixSocketWatcher(); |
|
49 |
|
50 virtual void Close() MOZ_OVERRIDE; |
|
51 |
|
52 ConnectionStatus GetConnectionStatus() const |
|
53 { |
|
54 return mConnectionStatus; |
|
55 } |
|
56 |
|
57 // Connect to a peer |
|
58 nsresult Connect(const struct sockaddr* aAddr, socklen_t aAddrLen); |
|
59 |
|
60 // Listen on socket for incoming connection requests |
|
61 nsresult Listen(const struct sockaddr* aAddr, socklen_t aAddrLen); |
|
62 |
|
63 // Callback method for accepted connections |
|
64 virtual void OnAccepted(int aFd, const sockaddr_any* aAddr, |
|
65 socklen_t aAddrLen) {}; |
|
66 |
|
67 // Callback method for successful connection requests |
|
68 virtual void OnConnected() {}; |
|
69 |
|
70 // Callback method for successful listen requests |
|
71 virtual void OnListening() {}; |
|
72 |
|
73 // Callback method for receiving from socket |
|
74 virtual void OnSocketCanReceiveWithoutBlocking() {}; |
|
75 |
|
76 // Callback method for sending on socket |
|
77 virtual void OnSocketCanSendWithoutBlocking() {}; |
|
78 |
|
79 protected: |
|
80 UnixSocketWatcher(MessageLoop* aIOLoop); |
|
81 UnixSocketWatcher(MessageLoop* aIOLoop, int aFd, |
|
82 ConnectionStatus aConnectionStatus); |
|
83 void SetSocket(int aFd, ConnectionStatus aConnectionStatus); |
|
84 |
|
85 private: |
|
86 void OnFileCanReadWithoutBlocking(int aFd) MOZ_OVERRIDE; |
|
87 void OnFileCanWriteWithoutBlocking(int aFd) MOZ_OVERRIDE; |
|
88 |
|
89 ConnectionStatus mConnectionStatus; |
|
90 }; |
|
91 |
|
92 } |
|
93 } |
|
94 |
|
95 #endif |