blob: deaaf78f8a62b302f09ae33232779baea96f422b [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_THRIFT_H_
8#define _THRIFT_THRIFT_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Mark Slee4f261c52007-04-13 00:33:24 +000010#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
Mark Slee8d7e1f62006-06-07 06:48:56 +000014#include <netinet/in.h>
Mark Slee4f261c52007-04-13 00:33:24 +000015#ifdef HAVE_INTTYPES_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000016#include <inttypes.h>
Mark Slee4f261c52007-04-13 00:33:24 +000017#endif
Mark Sleee8540632006-05-30 09:24:40 +000018#include <string>
19#include <map>
20#include <list>
21#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000022#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000023#include <exception>
David Reiss9b209552008-04-08 06:26:05 +000024#include <string>
Marc Slemko5b126d62006-08-11 23:03:42 +000025
Aditya Agarwald622e962006-10-11 02:42:49 +000026#include "TLogging.h"
27
Mark Sleecfc01932006-09-01 22:18:16 +000028namespace facebook { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000029
David Reissfaebedd2007-09-17 23:20:38 +000030class TOutput {
31 public:
David Reiss9b209552008-04-08 06:26:05 +000032 TOutput() : f_(&errorTimeWrapper) {}
boz6ded7752007-06-05 22:41:18 +000033
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 Reiss9b209552008-04-08 06:26:05 +000042 inline static void errorTimeWrapper(const char* msg) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000043 time_t now;
44 char dbgtime[25];
45 time(&now);
46 ctime_r(&now, dbgtime);
47 dbgtime[24] = 0;
David Reiss9b209552008-04-08 06:26:05 +000048 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000049 }
David Reiss9b209552008-04-08 06:26:05 +000050
51 /** Just like strerror_r but returns a C++ string object. */
52 static std::string strerror_s(int errno_copy);
53
David Reissfaebedd2007-09-17 23:20:38 +000054 private:
boz6ded7752007-06-05 22:41:18 +000055 void (*f_)(const char *);
56};
57
58extern TOutput GlobalOutput;
59
Mark Sleef9831082007-02-20 20:59:21 +000060namespace protocol {
61 class TProtocol;
62}
63
Mark Sleeb9ff32a2006-11-16 01:00:24 +000064class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +000065 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000066 TException() {}
67
Mark Slee82a6c0f2007-04-04 21:08:21 +000068 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000069 message_(message) {}
70
Martin Kraemer8196a612006-12-09 01:57:58 +000071 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000072
Martin Kraemer92a2eac2007-02-05 20:58:41 +000073 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000074 if (message_.empty()) {
75 return "Default TException.";
76 } else {
77 return message_.c_str();
78 }
Mark Slee2f6404d2006-10-10 01:37:40 +000079 }
80
David Reissfaebedd2007-09-17 23:20:38 +000081 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000082 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000083
Marc Slemko5b126d62006-08-11 23:03:42 +000084};
85
Mark Sleef9831082007-02-20 20:59:21 +000086class TApplicationException : public TException {
David Reissfaebedd2007-09-17 23:20:38 +000087 public:
Mark Sleef9831082007-02-20 20:59:21 +000088
89 /**
90 * Error codes for the various types of exceptions.
91 */
92 enum TApplicationExceptionType {
93 UNKNOWN = 0,
Mark Sleef3d33632007-02-21 01:39:23 +000094 UNKNOWN_METHOD = 1,
95 INVALID_MESSAGE_TYPE = 2,
96 WRONG_METHOD_NAME = 3,
97 BAD_SEQUENCE_ID = 4,
98 MISSING_RESULT = 5,
Mark Sleef9831082007-02-20 20:59:21 +000099 };
100
101 TApplicationException() :
102 TException(),
103 type_(UNKNOWN) {}
104
105 TApplicationException(TApplicationExceptionType type) :
David Reiss0c90f6f2008-02-06 22:18:40 +0000106 TException(),
Mark Sleef9831082007-02-20 20:59:21 +0000107 type_(type) {}
108
Mark Slee82a6c0f2007-04-04 21:08:21 +0000109 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000110 TException(message),
111 type_(UNKNOWN) {}
112
113 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +0000114 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000115 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()) {
dweatherford2323cf62007-12-05 03:40:19 +0000132 switch (type_) {
David Reissadad4ab2007-12-14 20:56:04 +0000133 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)";
dweatherford2323cf62007-12-05 03:40:19 +0000140 };
Mark Sleef9831082007-02-20 20:59:21 +0000141 } else {
142 return message_.c_str();
143 }
144 }
145
Mark Sleeba8f8d72007-04-03 00:34:00 +0000146 uint32_t read(protocol::TProtocol* iprot);
147 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000148
David Reissfaebedd2007-09-17 23:20:38 +0000149 protected:
Mark Sleef9831082007-02-20 20:59:21 +0000150 /**
151 * Error code
152 */
153 TApplicationExceptionType type_;
154
155};
156
157
David Reissd779cbe2007-08-31 01:42:55 +0000158// Forward declare this structure used by TDenseProtocol
159namespace reflection { namespace local {
160struct TypeSpec;
161}}
162
163
Marc Slemko5b126d62006-08-11 23:03:42 +0000164}} // facebook::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000165
Mark Sleef5f2be42006-09-05 21:05:31 +0000166#endif // #ifndef _THRIFT_THRIFT_H_