HTTPConnection2.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file HTTPConnection2.h
  11. /// \brief Contains HTTPConnection2, used to communicate with web servers
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_HTTPConnection2==1 && _RAKNET_SUPPORT_TCPInterface==1
  15. #ifndef __HTTP_CONNECTION_2
  16. #define __HTTP_CONNECTION_2
  17. #include "Export.h"
  18. #include "RakString.h"
  19. #include "RakMemoryOverride.h"
  20. #include "RakNetTypes.h"
  21. #include "DS_List.h"
  22. #include "DS_Queue.h"
  23. #include "PluginInterface2.h"
  24. #include "SimpleMutex.h"
  25. namespace RakNet
  26. {
  27. /// Forward declarations
  28. class TCPInterface;
  29. struct SystemAddress;
  30. /// \brief Use HTTPConnection2 to communicate with a web server.
  31. /// \details Start an instance of TCPInterface via the Start() command.
  32. /// This class will handle connecting to transmit a request
  33. class RAK_DLL_EXPORT HTTPConnection2 : public PluginInterface2
  34. {
  35. public:
  36. // GetInstance() and DestroyInstance(instance*)
  37. STATIC_FACTORY_DECLARATIONS(HTTPConnection2)
  38. HTTPConnection2();
  39. virtual ~HTTPConnection2();
  40. /// \brief Connect to, then transmit a request to a TCP based server
  41. /// \param[in] tcp An instance of TCPInterface that previously had TCPInterface::Start() called
  42. /// \param[in] stringToTransmit What string to transmit. See RakString::FormatForPOST(), RakString::FormatForGET(), RakString::FormatForDELETE()
  43. /// \param[in] host The IP address to connect to
  44. /// \param[in] port The port to connect to
  45. /// \param[in] useSSL If to use SSL to connect. OPEN_SSL_CLIENT_SUPPORT must be defined to 1 in RakNetDefines.h or RakNetDefinesOverrides.h
  46. /// \param[in] ipVersion 4 for IPV4, 6 for IPV6
  47. /// \param[in] useAddress Assume we are connected to this address and send to it, rather than do a lookup
  48. /// \param[in] userData
  49. /// \return false if host is not a valid IP address or domain name
  50. bool TransmitRequest(const char* stringToTransmit, const char* host, unsigned short port=80, bool useSSL=false, int ipVersion=4, SystemAddress useAddress=UNASSIGNED_SYSTEM_ADDRESS, void *userData=0);
  51. /// \brief Check for and return a response from a prior call to TransmitRequest()
  52. /// As TCP is stream based, you may get a webserver reply over several calls to TCPInterface::Receive()
  53. /// HTTPConnection2 will store Packet::data and return the response to you either when the connection to the webserver is lost, or enough data has been received()
  54. /// This will only potentially return true after a call to ProcessTCPPacket() or OnLostConnection()
  55. /// \param[out] stringTransmitted The original string transmitted
  56. /// \param[out] hostTransmitted The parameter of the same name passed to TransmitRequest()
  57. /// \param[out] responseReceived The response, if any
  58. /// \param[out] hostReceived The SystemAddress from ProcessTCPPacket() or OnLostConnection()
  59. /// \param[out] contentOffset The offset from the start of responseReceived to the data body. Equivalent to searching for \r\n\r\n in responseReceived.
  60. /// \param[out] userData Whatever you passed to TransmitRequest
  61. /// \return true if there was a response. false if not.
  62. bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset, void **userData );
  63. bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset );
  64. /// \brief Return if any requests are pending
  65. bool IsBusy(void) const;
  66. /// \brief Return if any requests are waiting to be read by the user
  67. bool HasResponse(void) const;
  68. struct Request
  69. {
  70. RakString stringToTransmit;
  71. RakString stringReceived;
  72. RakString host;
  73. SystemAddress hostEstimatedAddress;
  74. SystemAddress hostCompletedAddress;
  75. unsigned short port;
  76. bool useSSL;
  77. int contentOffset;
  78. int contentLength;
  79. int ipVersion;
  80. void *userData;
  81. bool chunked;
  82. size_t thisChunkSize;
  83. size_t bytesReadForThisChunk;
  84. };
  85. /// \internal
  86. virtual PluginReceiveResult OnReceive(Packet *packet);
  87. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  88. virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
  89. virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason);
  90. protected:
  91. bool IsConnected(SystemAddress sa);
  92. void SendRequest(Request *request);
  93. void RemovePendingRequest(SystemAddress sa);
  94. void SendNextPendingRequest(void);
  95. void SendPendingRequestToConnectedSystem(SystemAddress sa);
  96. DataStructures::Queue<Request*> pendingRequests;
  97. DataStructures::List<Request*> sentRequests;
  98. DataStructures::List<Request*> completedRequests;
  99. SimpleMutex pendingRequestsMutex, sentRequestsMutex, completedRequestsMutex;
  100. };
  101. } // namespace RakNet
  102. #endif
  103. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号