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