Line data Source code
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 0 : virtual ~TransportLayerPrsock() {
31 0 : Detach();
32 0 : }
33 :
34 :
35 : // Internal initializer
36 : nsresult InitInternal() override;
37 :
38 : void Import(PRFileDesc *fd, nsresult *result);
39 :
40 0 : void Detach() {
41 0 : handler_->Detach();
42 0 : }
43 :
44 : // Implement TransportLayer
45 : TransportResult SendPacket(const unsigned char *data, size_t len) override;
46 :
47 0 : TRANSPORT_LAYER_ID("prsock")
48 :
49 : private:
50 : DISALLOW_COPY_ASSIGN(TransportLayerPrsock);
51 :
52 : // Inner class
53 : class SocketHandler : public nsASocketHandler {
54 : public:
55 0 : SocketHandler(TransportLayerPrsock *prsock, PRFileDesc *fd) :
56 0 : prsock_(prsock), fd_(fd) {
57 0 : mPollFlags = PR_POLL_READ;
58 0 : }
59 :
60 0 : void Detach() {
61 0 : mCondition = NS_BASE_STREAM_CLOSED;
62 0 : prsock_ = nullptr;
63 0 : }
64 :
65 : // Implement nsASocket
66 0 : virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) override {
67 0 : if (prsock_) {
68 0 : prsock_->OnSocketReady(fd, outflags);
69 : }
70 0 : }
71 :
72 0 : virtual void OnSocketDetached(PRFileDesc *fd) override {
73 0 : if (prsock_) {
74 0 : prsock_->OnSocketDetached(fd);
75 : }
76 0 : PR_Close(fd_);
77 0 : }
78 :
79 0 : virtual void IsLocal(bool *aIsLocal) override {
80 : // TODO(jesup): better check? Does it matter? (likely no)
81 0 : *aIsLocal = false;
82 0 : }
83 :
84 0 : virtual uint64_t ByteCountSent() override { return 0; }
85 0 : virtual uint64_t ByteCountReceived() override { return 0; }
86 :
87 : // nsISupports methods
88 : NS_DECL_THREADSAFE_ISUPPORTS
89 :
90 : private:
91 : TransportLayerPrsock *prsock_;
92 : PRFileDesc *fd_;
93 : private:
94 : DISALLOW_COPY_ASSIGN(SocketHandler);
95 0 : virtual ~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 0 : void OnSocketDetached(PRFileDesc *fd) {
104 0 : TL_SET_STATE(TS_CLOSED);
105 0 : }
106 : void IsLocal(bool *aIsLocal) {
107 : // TODO(jesup): better check? Does it matter? (likely no)
108 : *aIsLocal = false;
109 : }
110 :
111 : PRFileDesc *fd_;
112 : RefPtr<SocketHandler> handler_;
113 : nsCOMPtr<nsISocketTransportService> stservice_;
114 :
115 : };
116 :
117 :
118 :
119 : } // close namespace
120 : #endif
|