github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/tao/message_channel.h (about) 1 // File: rpc_channel.h 2 // Author: Kevin Walsh <kwalsh@holycross.edu> 3 // 4 // Description: An interface for a Message channel. 5 // 6 // Copyright (c) 2013, Google Inc. All rights reserved. 7 // 8 // Licensed under the Apache License, Version 2.0 (the "License"); 9 // you may not use this file except in compliance with the License. 10 // You may obtain a copy of the License at 11 // 12 // http://www.apache.org/licenses/LICENSE-2.0 13 // 14 // Unless required by applicable law or agreed to in writing, software 15 // distributed under the License is distributed on an "AS IS" BASIS, 16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 // See the License for the specific language governing permissions and 18 // limitations under the License. 19 #ifndef TAO_MESSAGE_CHANNEL_H_ 20 #define TAO_MESSAGE_CHANNEL_H_ 21 22 #include <list> 23 #include <string> 24 25 #include "tao/util.h" 26 27 namespace tao { 28 using std::string; 29 30 /// An interface for a channel that can send and receive Message objects. 31 class MessageChannel { 32 public: 33 MessageChannel() : maxMessageSize_(DefaultMaxMessageSize) {} 34 virtual ~MessageChannel() {} // sub-classes should Close() here. 35 36 /// Close a channel. It is safe to call this multiple times. 37 virtual void Close() = 0; 38 39 /// Check if a channel is closed. 40 virtual bool IsClosed() const = 0; 41 42 /// Get the maximum message reception size. 43 size_t MaxMessageSize() const { return maxMessageSize_; } 44 45 /// Set the maximum message reception size. 46 void SetMaxMessageSize(size_t size) { maxMessageSize_ = size; } 47 48 /// Low-level functions for raw data. 49 /// @{ 50 51 /// Send raw data to the channel. 52 /// Failure will close the channel. 53 /// @param buffer The buffer containing data to send. 54 /// @param buffer_len The length of buffer. 55 virtual bool SendData(const void *buffer, size_t buffer_len) = 0; 56 57 /// Receive raw data from the channel. 58 /// No maximum message size applies, the caller is expected to supply a 59 /// reasonable buffer_len, which will be filled entirely. 60 /// Failure or eof will close the channel. 61 /// @param[out] buffer The buffer to fill with data. 62 /// @param buffer_len The number of bytes to be filled. 63 /// @param[out] eof Will be set to true iff end of stream reached. 64 virtual bool ReceiveData(void *buffer, size_t buffer_len, bool *eof); 65 66 /// @} 67 68 /// Mid-level functions for strings. 69 /// @{ 70 71 /// Send a raw string to the channel. 72 /// Failure will close the channel. 73 /// @param s The string to send. 74 virtual bool SendString(const string &s); 75 76 /// Receive a string over the channel. 77 /// Failure or eof will close the channel. 78 /// @param[out] s The string to receive the data. 79 /// @param[out] eof Will be set to true iff end of stream reached. 80 virtual bool ReceiveString(string *s, bool *eof); 81 82 /// @} 83 84 /// High-level functions for Messages. 85 /// @{ 86 87 /// Send a Message to the channel. 88 /// Failure will close the channel. 89 /// @param m The Message to send. 90 virtual bool SendMessage(const google::protobuf::Message &m); 91 92 /// Receive a Message over the channel. 93 /// Failure or eof will close the channel. 94 /// @param[out] m The received Message. 95 /// @param[out] eof Will be set to true iff end of stream reached. 96 virtual bool ReceiveMessage(google::protobuf::Message *m, bool *eof); 97 98 /// @} 99 100 /// Serialize channel parameters for passing across fork/exec or between 101 /// processes, if possible. This does not close the channel. Not all channel 102 /// types must necessarily be serializable. 103 /// @param params[out] The serialized parameters. 104 virtual bool SerializeToString(string *params) const { return false; } 105 106 /// Maximum 20 MB for message reception on this channel by default. 107 static constexpr size_t DefaultMaxMessageSize = 20 * 1024 * 1024; 108 109 protected: 110 /// The max Message (or string) reception size. 111 size_t maxMessageSize_; 112 113 /// Receive raw data from the channel. 114 /// No maximum message size applies, the caller is expected to supply a 115 /// reasonable buffer_len. Partial messages are accepted. 116 /// Failure or eof will close the channel. 117 /// @param[out] buffer The buffer to fill with data. 118 /// @param max_recv_len The maximum number of bytes to be filled. 119 /// @param[out] eof Will be set to true iff end of stream reached. 120 virtual bool ReceivePartialData(void *buffer, size_t max_recv_len, 121 size_t *recv_len, bool *eof) = 0; 122 }; 123 } // namespace tao 124 125 #endif // TAO_MESSAGE_CHANNEL_H_