| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#ifndef __NET_RPC_LIB_CLIENT_RPC_CLIENT_CONNECTION_HTTP_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_CLIENT_RPC_CLIENT_CONNECTION_HTTP_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <map> |
|---|
| 36 |
#include <string> |
|---|
| 37 |
#include <whisperlib/common/base/types.h> |
|---|
| 38 |
#include <whisperlib/common/base/log.h> |
|---|
| 39 |
#include <whisperlib/common/base/errno.h> |
|---|
| 40 |
#include <whisperlib/common/base/system.h> |
|---|
| 41 |
#include <whisperlib/common/base/gflags.h> |
|---|
| 42 |
#include <whisperlib/common/io/buffer/io_memory_stream.h> |
|---|
| 43 |
#include <whisperlib/common/sync/event.h> |
|---|
| 44 |
#include <whisperlib/common/sync/mutex.h> |
|---|
| 45 |
|
|---|
| 46 |
#include <whisperlib/net/http/http_server_protocol.h> |
|---|
| 47 |
#include <whisperlib/net/http/http_client_protocol.h> |
|---|
| 48 |
#include <whisperlib/net/base/selector.h> |
|---|
| 49 |
|
|---|
| 50 |
#include <whisperlib/net/rpc/lib/client/irpc_client_connection.h> |
|---|
| 51 |
|
|---|
| 52 |
namespace rpc { |
|---|
| 53 |
|
|---|
| 54 |
// This is a HTTP connection from client side working as transport layer |
|---|
| 55 |
// for RPC calls & results. |
|---|
| 56 |
// It sends and receives rpc::Message packets. |
|---|
| 57 |
|
|---|
| 58 |
class ClientConnectionHTTP : public rpc::IClientConnection { |
|---|
| 59 |
public: |
|---|
| 60 |
////////////////////////////////////////////////////////////// |
|---|
| 61 |
// |
|---|
| 62 |
// Methods available to any external thread (application). |
|---|
| 63 |
// |
|---|
| 64 |
ClientConnectionHTTP(net::Selector& selector, |
|---|
| 65 |
net::NetFactory& net_factory, |
|---|
| 66 |
net::PROTOCOL net_protocol, |
|---|
| 67 |
const http::ClientParams& params, |
|---|
| 68 |
const net::HostPort& addr, |
|---|
| 69 |
rpc::CODEC_ID codec_id, |
|---|
| 70 |
const string& httpRequestPath); |
|---|
| 71 |
virtual ~ClientConnectionHTTP(); |
|---|
| 72 |
|
|---|
| 73 |
// Returns a description of the last error on the http connection. |
|---|
| 74 |
const char* Error() const; |
|---|
| 75 |
|
|---|
| 76 |
// Close HTTP connection, if any. |
|---|
| 77 |
void Close(); |
|---|
| 78 |
|
|---|
| 79 |
// Returns the address of the http server where this client is/was connected. |
|---|
| 80 |
const net::HostPort& remote_address() const { |
|---|
| 81 |
return remote_addr_; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
protected: |
|---|
| 85 |
////////////////////////////////////////////////////////////////////// |
|---|
| 86 |
// |
|---|
| 87 |
// IRPCClientConnection interface methods |
|---|
| 88 |
// |
|---|
| 89 |
virtual void Send(const rpc::Message* p); |
|---|
| 90 |
virtual void Cancel(uint32 xid); |
|---|
| 91 |
|
|---|
| 92 |
////////////////////////////////////////////////////////////// |
|---|
| 93 |
// |
|---|
| 94 |
// Methods available only from the selector thread. |
|---|
| 95 |
// |
|---|
| 96 |
protected: |
|---|
| 97 |
// Send the given request in selector thread context. |
|---|
| 98 |
void SendRequest(http::ClientRequest* req, const rpc::Message* p); |
|---|
| 99 |
|
|---|
| 100 |
// Called by the selector after a http request receives full response |
|---|
| 101 |
// from the server. The request object is the same one you used |
|---|
| 102 |
// when launching the query (you probably want to delete it). |
|---|
| 103 |
void CallbackRequestDone(http::ClientRequest* req); |
|---|
| 104 |
protected: |
|---|
| 105 |
net::Selector& selector_; |
|---|
| 106 |
net::NetFactory& net_factory_; |
|---|
| 107 |
const net::PROTOCOL net_protocol_; |
|---|
| 108 |
const net::HostPort remote_addr_; |
|---|
| 109 |
|
|---|
| 110 |
const http::ClientParams* http_params_; |
|---|
| 111 |
http::ClientProtocol* http_protocol_; |
|---|
| 112 |
string http_request_path_; |
|---|
| 113 |
|
|---|
| 114 |
synch::Event disconnect_completed_; // signal TCP close done |
|---|
| 115 |
|
|---|
| 116 |
typedef map<http::ClientRequest*, const rpc::Message*> QueryMap; |
|---|
| 117 |
QueryMap queries_; |
|---|
| 118 |
|
|---|
| 119 |
private: |
|---|
| 120 |
DISALLOW_EVIL_CONSTRUCTORS(ClientConnectionHTTP); |
|---|
| 121 |
}; |
|---|
| 122 |
} |
|---|
| 123 |
#endif // __NET_RPC_LIB_CLIENT_RPC_CLIENT_CONNECTION_HTTP_H__ |
|---|