|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef mozilla_system_netd_h__ |
|
6 #define mozilla_system_netd_h__ |
|
7 |
|
8 #include "nsISupportsImpl.h" |
|
9 #include "nsAutoPtr.h" |
|
10 #include "base/message_loop.h" |
|
11 #include "mozilla/FileUtils.h" |
|
12 |
|
13 #define MAX_COMMAND_SIZE 4096 |
|
14 |
|
15 namespace mozilla { |
|
16 namespace ipc { |
|
17 |
|
18 /* |
|
19 * Represents raw data going to or coming from the Netd socket. |
|
20 */ |
|
21 struct NetdCommand |
|
22 { |
|
23 uint8_t mData[MAX_COMMAND_SIZE]; |
|
24 |
|
25 // Number of octets in mData. |
|
26 size_t mSize; |
|
27 }; |
|
28 |
|
29 class NetdConsumer |
|
30 { |
|
31 protected: |
|
32 virtual ~NetdConsumer() { } |
|
33 |
|
34 public: |
|
35 NS_INLINE_DECL_REFCOUNTING(NetdConsumer) |
|
36 |
|
37 virtual void MessageReceived(NetdCommand* aMessage) = 0; |
|
38 }; |
|
39 |
|
40 class NetdWriteTask : public Task |
|
41 { |
|
42 virtual void Run(); |
|
43 }; |
|
44 |
|
45 class NetdClient : public MessageLoopForIO::LineWatcher |
|
46 { |
|
47 virtual ~NetdClient(); |
|
48 |
|
49 public: |
|
50 NS_INLINE_DECL_REFCOUNTING(NetdClient) |
|
51 typedef std::queue<NetdCommand*> NetdCommandQueue; |
|
52 |
|
53 NetdClient(); |
|
54 static void Start(); |
|
55 static void SendNetdCommandIOThread(NetdCommand* aMessage); |
|
56 |
|
57 private: |
|
58 void WriteNetdCommand(); |
|
59 virtual void OnError(); |
|
60 virtual void OnLineRead(int aFd, nsDependentCSubstring& aMessage); |
|
61 virtual void OnFileCanWriteWithoutBlocking(int aFd); |
|
62 bool OpenSocket(); |
|
63 |
|
64 MessageLoopForIO *mIOLoop; |
|
65 MessageLoopForIO::FileDescriptorWatcher mReadWatcher; |
|
66 MessageLoopForIO::FileDescriptorWatcher mWriteWatcher; |
|
67 ScopedClose mSocket; |
|
68 NetdCommandQueue mOutgoingQ; |
|
69 nsAutoPtr<NetdCommand> mCurrentNetdCommand; |
|
70 size_t mCurrentWriteOffset; |
|
71 size_t mReConnectTimes; |
|
72 }; |
|
73 |
|
74 void StartNetd(NetdConsumer *); |
|
75 void StopNetd(); |
|
76 void SendNetdCommand(NetdCommand *); |
|
77 |
|
78 } // namespace ipc |
|
79 } // namespace mozilla |
|
80 |
|
81 #endif // mozilla_system_netd_h__ |