blob: 6eb20bb8a84fdc5b222d82e9d0b7192c010a3d54 [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>
25#include <boost/lexical_cast.hpp>
Marc Slemko5b126d62006-08-11 23:03:42 +000026
Aditya Agarwald622e962006-10-11 02:42:49 +000027#include "TLogging.h"
28
Mark Sleecfc01932006-09-01 22:18:16 +000029namespace facebook { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000030
David Reissfaebedd2007-09-17 23:20:38 +000031class TOutput {
32 public:
David Reiss9b209552008-04-08 06:26:05 +000033 TOutput() : f_(&errorTimeWrapper) {}
boz6ded7752007-06-05 22:41:18 +000034
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 Reiss9b209552008-04-08 06:26:05 +000043 inline static void errorTimeWrapper(const char* msg) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000044 time_t now;
45 char dbgtime[25];
46 time(&now);
47 ctime_r(&now, dbgtime);
48 dbgtime[24] = 0;
David Reiss9b209552008-04-08 06:26:05 +000049 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000050 }
David Reiss9b209552008-04-08 06:26:05 +000051
52 /** Just like strerror_r but returns a C++ string object. */
53 static std::string strerror_s(int errno_copy);
54
David Reissfaebedd2007-09-17 23:20:38 +000055 private:
boz6ded7752007-06-05 22:41:18 +000056 void (*f_)(const char *);
57};
58
59extern TOutput GlobalOutput;
60
Mark Sleef9831082007-02-20 20:59:21 +000061namespace protocol {
62 class TProtocol;
63}
64
Mark Sleeb9ff32a2006-11-16 01:00:24 +000065class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +000066 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000067 TException() {}
68
Mark Slee82a6c0f2007-04-04 21:08:21 +000069 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000070 message_(message) {}
71
Martin Kraemer8196a612006-12-09 01:57:58 +000072 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000073
Martin Kraemer92a2eac2007-02-05 20:58:41 +000074 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000075 if (message_.empty()) {
76 return "Default TException.";
77 } else {
78 return message_.c_str();
79 }
Mark Slee2f6404d2006-10-10 01:37:40 +000080 }
81
David Reissfaebedd2007-09-17 23:20:38 +000082 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000083 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000084
Marc Slemko5b126d62006-08-11 23:03:42 +000085};
86
Mark Sleef9831082007-02-20 20:59:21 +000087class TApplicationException : public TException {
David Reissfaebedd2007-09-17 23:20:38 +000088 public:
Mark Sleef9831082007-02-20 20:59:21 +000089
90 /**
91 * Error codes for the various types of exceptions.
92 */
93 enum TApplicationExceptionType {
94 UNKNOWN = 0,
Mark Sleef3d33632007-02-21 01:39:23 +000095 UNKNOWN_METHOD = 1,
96 INVALID_MESSAGE_TYPE = 2,
97 WRONG_METHOD_NAME = 3,
98 BAD_SEQUENCE_ID = 4,
99 MISSING_RESULT = 5,
Mark Sleef9831082007-02-20 20:59:21 +0000100 };
101
102 TApplicationException() :
103 TException(),
104 type_(UNKNOWN) {}
105
106 TApplicationException(TApplicationExceptionType type) :
David Reiss0c90f6f2008-02-06 22:18:40 +0000107 TException(),
Mark Sleef9831082007-02-20 20:59:21 +0000108 type_(type) {}
109
Mark Slee82a6c0f2007-04-04 21:08:21 +0000110 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000111 TException(message),
112 type_(UNKNOWN) {}
113
114 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +0000115 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000116 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()) {
dweatherford2323cf62007-12-05 03:40:19 +0000133 switch (type_) {
David Reissadad4ab2007-12-14 20:56:04 +0000134 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)";
dweatherford2323cf62007-12-05 03:40:19 +0000141 };
Mark Sleef9831082007-02-20 20:59:21 +0000142 } else {
143 return message_.c_str();
144 }
145 }
146
Mark Sleeba8f8d72007-04-03 00:34:00 +0000147 uint32_t read(protocol::TProtocol* iprot);
148 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000149
David Reissfaebedd2007-09-17 23:20:38 +0000150 protected:
Mark Sleef9831082007-02-20 20:59:21 +0000151 /**
152 * Error code
153 */
154 TApplicationExceptionType type_;
155
156};
157
158
David Reissd779cbe2007-08-31 01:42:55 +0000159// Forward declare this structure used by TDenseProtocol
160namespace reflection { namespace local {
161struct TypeSpec;
162}}
163
164
Marc Slemko5b126d62006-08-11 23:03:42 +0000165}} // facebook::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000166
Mark Sleef5f2be42006-09-05 21:05:31 +0000167#endif // #ifndef _THRIFT_THRIFT_H_