|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
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 file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 // Original author: ekr@rtfm.com |
|
8 |
|
9 #ifndef transportlayerprsock_h__ |
|
10 #define transportlayerprsock_h__ |
|
11 |
|
12 #include "nspr.h" |
|
13 #include "prio.h" |
|
14 |
|
15 #include "nsASocketHandler.h" |
|
16 #include "nsCOMPtr.h" |
|
17 #include "nsISocketTransportService.h" |
|
18 #include "nsXPCOM.h" |
|
19 |
|
20 #include "m_cpp_utils.h" |
|
21 #include "transportflow.h" |
|
22 #include "transportlayer.h" |
|
23 |
|
24 namespace mozilla { |
|
25 |
|
26 class TransportLayerPrsock : public TransportLayer { |
|
27 public: |
|
28 TransportLayerPrsock() : fd_(nullptr), handler_() {} |
|
29 |
|
30 virtual ~TransportLayerPrsock() { |
|
31 Detach(); |
|
32 } |
|
33 |
|
34 |
|
35 // Internal initializer |
|
36 virtual nsresult InitInternal(); |
|
37 |
|
38 void Import(PRFileDesc *fd, nsresult *result); |
|
39 |
|
40 void Detach() { |
|
41 handler_->Detach(); |
|
42 } |
|
43 |
|
44 // Implement TransportLayer |
|
45 virtual TransportResult SendPacket(const unsigned char *data, size_t len); |
|
46 |
|
47 TRANSPORT_LAYER_ID("prsock") |
|
48 |
|
49 private: |
|
50 DISALLOW_COPY_ASSIGN(TransportLayerPrsock); |
|
51 |
|
52 // Inner class |
|
53 class SocketHandler : public nsASocketHandler { |
|
54 public: |
|
55 SocketHandler(TransportLayerPrsock *prsock, PRFileDesc *fd) : |
|
56 prsock_(prsock), fd_(fd) { |
|
57 mPollFlags = PR_POLL_READ; |
|
58 } |
|
59 virtual ~SocketHandler() {} |
|
60 |
|
61 void Detach() { |
|
62 mCondition = NS_BASE_STREAM_CLOSED; |
|
63 prsock_ = nullptr; |
|
64 } |
|
65 |
|
66 // Implement nsASocket |
|
67 virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) { |
|
68 if (prsock_) { |
|
69 prsock_->OnSocketReady(fd, outflags); |
|
70 } |
|
71 } |
|
72 |
|
73 virtual void OnSocketDetached(PRFileDesc *fd) { |
|
74 if (prsock_) { |
|
75 prsock_->OnSocketDetached(fd); |
|
76 } |
|
77 PR_Close(fd_); |
|
78 } |
|
79 |
|
80 virtual void IsLocal(bool *aIsLocal) { |
|
81 // TODO(jesup): better check? Does it matter? (likely no) |
|
82 *aIsLocal = false; |
|
83 } |
|
84 |
|
85 virtual uint64_t ByteCountSent() { return 0; } |
|
86 virtual uint64_t ByteCountReceived() { return 0; } |
|
87 |
|
88 // nsISupports methods |
|
89 NS_DECL_THREADSAFE_ISUPPORTS |
|
90 |
|
91 private: |
|
92 TransportLayerPrsock *prsock_; |
|
93 PRFileDesc *fd_; |
|
94 private: |
|
95 DISALLOW_COPY_ASSIGN(SocketHandler); |
|
96 }; |
|
97 |
|
98 // Allow SocketHandler to talk to our APIs |
|
99 friend class SocketHandler; |
|
100 |
|
101 // Functions to be called by SocketHandler |
|
102 void OnSocketReady(PRFileDesc *fd, int16_t outflags); |
|
103 void OnSocketDetached(PRFileDesc *fd) { |
|
104 TL_SET_STATE(TS_CLOSED); |
|
105 } |
|
106 void IsLocal(bool *aIsLocal) { |
|
107 // TODO(jesup): better check? Does it matter? (likely no) |
|
108 *aIsLocal = false; |
|
109 } |
|
110 |
|
111 PRFileDesc *fd_; |
|
112 nsRefPtr<SocketHandler> handler_; |
|
113 nsCOMPtr<nsISocketTransportService> stservice_; |
|
114 |
|
115 }; |
|
116 |
|
117 |
|
118 |
|
119 } // close namespace |
|
120 #endif |