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