TestSocketInput.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* -*- Mode: C++; tab-width: 2; 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
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include <stdio.h>
  6. #ifdef WIN32
  7. #include <windows.h>
  8. #endif
  9. #include "nscore.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsISocketTransportService.h"
  12. #include "nsIEventQueueService.h"
  13. #include "nsIServiceManager.h"
  14. #include "nsIComponentRegistrar.h"
  15. #include "nsITransport.h"
  16. #include "nsIRequest.h"
  17. #include "nsIStreamListener.h"
  18. #include "nsIInputStream.h"
  19. static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
  20. static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
  21. static int gKeepRunning = 1;
  22. class InputTestConsumer : public nsIStreamListener
  23. {
  24. public:
  25. InputTestConsumer();
  26. virtual ~InputTestConsumer();
  27. // ISupports interface...
  28. NS_DECL_ISUPPORTS
  29. // IStreamListener interface...
  30. NS_DECL_NSIREQUESTOBSERVER
  31. NS_DECL_NSISTREAMLISTENER
  32. };
  33. InputTestConsumer::InputTestConsumer()
  34. {
  35. }
  36. InputTestConsumer::~InputTestConsumer()
  37. {
  38. }
  39. NS_IMPL_ISUPPORTS(InputTestConsumer, nsIRequestObserver, nsIStreamListener)
  40. NS_IMETHODIMP
  41. InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
  42. {
  43. printf("+++ OnStartRequest +++\n");
  44. return NS_OK;
  45. }
  46. NS_IMETHODIMP
  47. InputTestConsumer::OnDataAvailable(nsIRequest *request,
  48. nsISupports* context,
  49. nsIInputStream *aIStream,
  50. uint64_t aSourceOffset,
  51. uint32_t aLength)
  52. {
  53. char buf[1025];
  54. while (aLength > 0) {
  55. uint32_t amt;
  56. aIStream->Read(buf, 1024, &amt);
  57. if (amt == 0) break;
  58. buf[amt] = '\0';
  59. printf(buf);
  60. aLength -= amt;
  61. }
  62. return NS_OK;
  63. }
  64. NS_IMETHODIMP
  65. InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
  66. nsresult aStatus)
  67. {
  68. gKeepRunning = 0;
  69. printf("+++ OnStopRequest status %x +++\n", aStatus);
  70. return NS_OK;
  71. }
  72. int
  73. main(int argc, char* argv[])
  74. {
  75. nsresult rv;
  76. if (argc < 2) {
  77. printf("usage: %s <host>\n", argv[0]);
  78. return -1;
  79. }
  80. int port;
  81. char* hostName = argv[1];
  82. //nsString portString(argv[2]);
  83. //port = portString.ToInteger(&rv);
  84. port = 13;
  85. {
  86. nsCOMPtr<nsIServiceManager> servMan;
  87. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  88. nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
  89. NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
  90. if (registrar)
  91. registrar->AutoRegister(nullptr);
  92. // Create the Event Queue for this thread...
  93. nsCOMPtr<nsIEventQueueService> eventQService =
  94. do_GetService(kEventQueueServiceCID, &rv);
  95. if (NS_FAILED(rv)) return rv;
  96. nsCOMPtr<nsIEventQueue> eventQ;
  97. rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eventQ));
  98. if (NS_FAILED(rv)) return rv;
  99. nsCOMPtr<nsISocketTransportService> sts =
  100. do_GetService(kSocketTransportServiceCID, &rv);
  101. if (NS_FAILED(rv)) return rv;
  102. nsITransport* transport;
  103. rv = sts->CreateTransport(hostName, port, nullptr, 0, 0, &transport);
  104. if (NS_SUCCEEDED(rv)) {
  105. nsCOMPtr<nsIRequest> request;
  106. transport->AsyncRead(new InputTestConsumer, nullptr, 0, -1, 0, getter_AddRefs(request));
  107. NS_RELEASE(transport);
  108. }
  109. // Enter the message pump to allow the URL load to proceed.
  110. while ( gKeepRunning ) {
  111. PLEvent *gEvent;
  112. eventQ->WaitForEvent(&gEvent);
  113. eventQ->HandleEvent(gEvent);
  114. }
  115. } // this scopes the nsCOMPtrs
  116. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  117. rv = NS_ShutdownXPCOM(nullptr);
  118. NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
  119. return 0;
  120. }