transportlayerprsock.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Original author: ekr@rtfm.com
  6. #ifndef transportlayerprsock_h__
  7. #define transportlayerprsock_h__
  8. #include "nspr.h"
  9. #include "prio.h"
  10. #include "nsASocketHandler.h"
  11. #include "nsCOMPtr.h"
  12. #include "nsISocketTransportService.h"
  13. #include "nsXPCOM.h"
  14. #include "m_cpp_utils.h"
  15. #include "transportflow.h"
  16. #include "transportlayer.h"
  17. namespace mozilla {
  18. class TransportLayerPrsock : public TransportLayer {
  19. public:
  20. TransportLayerPrsock() : fd_(nullptr), handler_() {}
  21. virtual ~TransportLayerPrsock() {
  22. Detach();
  23. }
  24. // Internal initializer
  25. virtual nsresult InitInternal();
  26. void Import(PRFileDesc *fd, nsresult *result);
  27. void Detach() {
  28. handler_->Detach();
  29. }
  30. // Implement TransportLayer
  31. virtual TransportResult SendPacket(const unsigned char *data, size_t len);
  32. TRANSPORT_LAYER_ID("prsock")
  33. private:
  34. DISALLOW_COPY_ASSIGN(TransportLayerPrsock);
  35. // Inner class
  36. class SocketHandler : public nsASocketHandler {
  37. public:
  38. SocketHandler(TransportLayerPrsock *prsock, PRFileDesc *fd) :
  39. prsock_(prsock), fd_(fd) {
  40. mPollFlags = PR_POLL_READ;
  41. }
  42. void Detach() {
  43. mCondition = NS_BASE_STREAM_CLOSED;
  44. prsock_ = nullptr;
  45. }
  46. // Implement nsASocket
  47. virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) override {
  48. if (prsock_) {
  49. prsock_->OnSocketReady(fd, outflags);
  50. }
  51. }
  52. virtual void OnSocketDetached(PRFileDesc *fd) override {
  53. if (prsock_) {
  54. prsock_->OnSocketDetached(fd);
  55. }
  56. PR_Close(fd_);
  57. }
  58. virtual void IsLocal(bool *aIsLocal) override {
  59. // TODO(jesup): better check? Does it matter? (likely no)
  60. *aIsLocal = false;
  61. }
  62. virtual uint64_t ByteCountSent() override { return 0; }
  63. virtual uint64_t ByteCountReceived() override { return 0; }
  64. // nsISupports methods
  65. NS_DECL_THREADSAFE_ISUPPORTS
  66. private:
  67. TransportLayerPrsock *prsock_;
  68. PRFileDesc *fd_;
  69. private:
  70. DISALLOW_COPY_ASSIGN(SocketHandler);
  71. virtual ~SocketHandler() {}
  72. };
  73. // Allow SocketHandler to talk to our APIs
  74. friend class SocketHandler;
  75. // Functions to be called by SocketHandler
  76. void OnSocketReady(PRFileDesc *fd, int16_t outflags);
  77. void OnSocketDetached(PRFileDesc *fd) {
  78. TL_SET_STATE(TS_CLOSED);
  79. }
  80. void IsLocal(bool *aIsLocal) {
  81. // TODO(jesup): better check? Does it matter? (likely no)
  82. *aIsLocal = false;
  83. }
  84. PRFileDesc *fd_;
  85. RefPtr<SocketHandler> handler_;
  86. nsCOMPtr<nsISocketTransportService> stservice_;
  87. };
  88. } // close namespace
  89. #endif