michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_system_netd_h__ michael@0: #define mozilla_system_netd_h__ michael@0: michael@0: #include "nsISupportsImpl.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "base/message_loop.h" michael@0: #include "mozilla/FileUtils.h" michael@0: michael@0: #define MAX_COMMAND_SIZE 4096 michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: /* michael@0: * Represents raw data going to or coming from the Netd socket. michael@0: */ michael@0: struct NetdCommand michael@0: { michael@0: uint8_t mData[MAX_COMMAND_SIZE]; michael@0: michael@0: // Number of octets in mData. michael@0: size_t mSize; michael@0: }; michael@0: michael@0: class NetdConsumer michael@0: { michael@0: protected: michael@0: virtual ~NetdConsumer() { } michael@0: michael@0: public: michael@0: NS_INLINE_DECL_REFCOUNTING(NetdConsumer) michael@0: michael@0: virtual void MessageReceived(NetdCommand* aMessage) = 0; michael@0: }; michael@0: michael@0: class NetdWriteTask : public Task michael@0: { michael@0: virtual void Run(); michael@0: }; michael@0: michael@0: class NetdClient : public MessageLoopForIO::LineWatcher michael@0: { michael@0: virtual ~NetdClient(); michael@0: michael@0: public: michael@0: NS_INLINE_DECL_REFCOUNTING(NetdClient) michael@0: typedef std::queue NetdCommandQueue; michael@0: michael@0: NetdClient(); michael@0: static void Start(); michael@0: static void SendNetdCommandIOThread(NetdCommand* aMessage); michael@0: michael@0: private: michael@0: void WriteNetdCommand(); michael@0: virtual void OnError(); michael@0: virtual void OnLineRead(int aFd, nsDependentCSubstring& aMessage); michael@0: virtual void OnFileCanWriteWithoutBlocking(int aFd); michael@0: bool OpenSocket(); michael@0: michael@0: MessageLoopForIO *mIOLoop; michael@0: MessageLoopForIO::FileDescriptorWatcher mReadWatcher; michael@0: MessageLoopForIO::FileDescriptorWatcher mWriteWatcher; michael@0: ScopedClose mSocket; michael@0: NetdCommandQueue mOutgoingQ; michael@0: nsAutoPtr mCurrentNetdCommand; michael@0: size_t mCurrentWriteOffset; michael@0: size_t mReConnectTimes; michael@0: }; michael@0: michael@0: void StartNetd(NetdConsumer *); michael@0: void StopNetd(); michael@0: void SendNetdCommand(NetdCommand *); michael@0: michael@0: } // namespace ipc michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_system_netd_h__