Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_THRIFT_H_ |
| 8 | #define _THRIFT_THRIFT_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 10 | #ifdef HAVE_CONFIG_H |
| 11 | #include "config.h" |
| 12 | #endif |
| 13 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 14 | #include <netinet/in.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 15 | #ifdef HAVE_INTTYPES_H |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 16 | #include <inttypes.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 17 | #endif |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <map> |
| 20 | #include <list> |
| 21 | #include <set> |
Mark Slee | 4ecbebc | 2006-09-05 00:14:21 +0000 | [diff] [blame] | 22 | #include <vector> |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 23 | #include <exception> |
| 24 | |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 25 | #include "TLogging.h" |
| 26 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame^] | 27 | namespace apache { namespace thrift { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 28 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 29 | class TOutput { |
| 30 | public: |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 31 | TOutput() : f_(&errorTimeWrapper) {} |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 32 | |
| 33 | inline void setOutputFunction(void (*function)(const char *)){ |
| 34 | f_ = function; |
| 35 | } |
| 36 | |
| 37 | inline void operator()(const char *message){ |
| 38 | f_(message); |
| 39 | } |
| 40 | |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 41 | // It is important to have a const char* overload here instead of |
| 42 | // just the string version, otherwise errno could be corrupted |
| 43 | // if there is some problem allocating memory when constructing |
| 44 | // the string. |
| 45 | void perror(const char *message, int errno_copy); |
| 46 | inline void perror(const std::string &message, int errno_copy) { |
| 47 | perror(message.c_str(), errno_copy); |
| 48 | } |
| 49 | |
| 50 | void printf(const char *message, ...); |
| 51 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 52 | inline static void errorTimeWrapper(const char* msg) { |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 53 | time_t now; |
| 54 | char dbgtime[25]; |
| 55 | time(&now); |
| 56 | ctime_r(&now, dbgtime); |
| 57 | dbgtime[24] = 0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 58 | fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 59 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 60 | |
| 61 | /** Just like strerror_r but returns a C++ string object. */ |
| 62 | static std::string strerror_s(int errno_copy); |
| 63 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 64 | private: |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 65 | void (*f_)(const char *); |
| 66 | }; |
| 67 | |
| 68 | extern TOutput GlobalOutput; |
| 69 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 70 | namespace protocol { |
| 71 | class TProtocol; |
| 72 | } |
| 73 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 74 | class TException : public std::exception { |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 75 | public: |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 76 | TException() {} |
| 77 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 78 | TException(const std::string& message) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 79 | message_(message) {} |
| 80 | |
Martin Kraemer | 8196a61 | 2006-12-09 01:57:58 +0000 | [diff] [blame] | 81 | virtual ~TException() throw() {} |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 82 | |
Martin Kraemer | 92a2eac | 2007-02-05 20:58:41 +0000 | [diff] [blame] | 83 | virtual const char* what() const throw() { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 84 | if (message_.empty()) { |
| 85 | return "Default TException."; |
| 86 | } else { |
| 87 | return message_.c_str(); |
| 88 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 89 | } |
| 90 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 91 | protected: |
Mark Slee | 2abc9df | 2006-12-16 01:06:49 +0000 | [diff] [blame] | 92 | std::string message_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 93 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 96 | class TApplicationException : public TException { |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 97 | public: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Error codes for the various types of exceptions. |
| 101 | */ |
David Reiss | 322e595 | 2008-12-05 02:54:09 +0000 | [diff] [blame] | 102 | enum TApplicationExceptionType |
| 103 | { UNKNOWN = 0 |
| 104 | , UNKNOWN_METHOD = 1 |
| 105 | , INVALID_MESSAGE_TYPE = 2 |
| 106 | , WRONG_METHOD_NAME = 3 |
| 107 | , BAD_SEQUENCE_ID = 4 |
| 108 | , MISSING_RESULT = 5 |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | TApplicationException() : |
| 112 | TException(), |
| 113 | type_(UNKNOWN) {} |
| 114 | |
| 115 | TApplicationException(TApplicationExceptionType type) : |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 116 | TException(), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 117 | type_(type) {} |
| 118 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 119 | TApplicationException(const std::string& message) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 120 | TException(message), |
| 121 | type_(UNKNOWN) {} |
| 122 | |
| 123 | TApplicationException(TApplicationExceptionType type, |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 124 | const std::string& message) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 125 | TException(message), |
| 126 | type_(type) {} |
| 127 | |
| 128 | virtual ~TApplicationException() throw() {} |
| 129 | |
| 130 | /** |
| 131 | * Returns an error code that provides information about the type of error |
| 132 | * that has occurred. |
| 133 | * |
| 134 | * @return Error code |
| 135 | */ |
| 136 | TApplicationExceptionType getType() { |
| 137 | return type_; |
| 138 | } |
| 139 | |
| 140 | virtual const char* what() const throw() { |
| 141 | if (message_.empty()) { |
dweatherford | 2323cf6 | 2007-12-05 03:40:19 +0000 | [diff] [blame] | 142 | switch (type_) { |
David Reiss | adad4ab | 2007-12-14 20:56:04 +0000 | [diff] [blame] | 143 | case UNKNOWN : return "TApplicationException: Unknown application exception"; |
| 144 | case UNKNOWN_METHOD : return "TApplicationException: Unknown method"; |
| 145 | case INVALID_MESSAGE_TYPE : return "TApplicationException: Invalid message type"; |
| 146 | case WRONG_METHOD_NAME : return "TApplicationException: Wrong method name"; |
| 147 | case BAD_SEQUENCE_ID : return "TApplicationException: Bad sequence identifier"; |
| 148 | case MISSING_RESULT : return "TApplicationException: Missing result"; |
| 149 | default : return "TApplicationException: (Invalid exception type)"; |
dweatherford | 2323cf6 | 2007-12-05 03:40:19 +0000 | [diff] [blame] | 150 | }; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 151 | } else { |
| 152 | return message_.c_str(); |
| 153 | } |
| 154 | } |
| 155 | |
Mark Slee | ba8f8d7 | 2007-04-03 00:34:00 +0000 | [diff] [blame] | 156 | uint32_t read(protocol::TProtocol* iprot); |
| 157 | uint32_t write(protocol::TProtocol* oprot) const; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 158 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 159 | protected: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Error code |
| 162 | */ |
| 163 | TApplicationExceptionType type_; |
| 164 | |
| 165 | }; |
| 166 | |
| 167 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 168 | // Forward declare this structure used by TDenseProtocol |
| 169 | namespace reflection { namespace local { |
| 170 | struct TypeSpec; |
| 171 | }} |
| 172 | |
| 173 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame^] | 174 | }} // apache::thrift |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 175 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 176 | #endif // #ifndef _THRIFT_THRIFT_H_ |