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