blob: 624687bc94f74a1fc0b1cf4dc2fe531fa51ffc6d [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>
24
Aditya Agarwald622e962006-10-11 02:42:49 +000025#include "TLogging.h"
26
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000028
David Reissfaebedd2007-09-17 23:20:38 +000029class TOutput {
30 public:
David Reiss9b209552008-04-08 06:26:05 +000031 TOutput() : f_(&errorTimeWrapper) {}
boz6ded7752007-06-05 22:41:18 +000032
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
David Reiss01e55c12008-07-13 22:18:51 +000041 // It is important to have a const char* overload here instead of
42 // just the string version, otherwise errno could be corrupted
43 // if there is some problem allocating memory when constructing
44 // the string.
45 void perror(const char *message, int errno_copy);
46 inline void perror(const std::string &message, int errno_copy) {
47 perror(message.c_str(), errno_copy);
48 }
49
50 void printf(const char *message, ...);
51
David Reiss9b209552008-04-08 06:26:05 +000052 inline static void errorTimeWrapper(const char* msg) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000053 time_t now;
54 char dbgtime[25];
55 time(&now);
56 ctime_r(&now, dbgtime);
57 dbgtime[24] = 0;
David Reiss9b209552008-04-08 06:26:05 +000058 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000059 }
David Reiss9b209552008-04-08 06:26:05 +000060
61 /** Just like strerror_r but returns a C++ string object. */
62 static std::string strerror_s(int errno_copy);
63
David Reissfaebedd2007-09-17 23:20:38 +000064 private:
boz6ded7752007-06-05 22:41:18 +000065 void (*f_)(const char *);
66};
67
68extern TOutput GlobalOutput;
69
Mark Sleef9831082007-02-20 20:59:21 +000070namespace protocol {
71 class TProtocol;
72}
73
Mark Sleeb9ff32a2006-11-16 01:00:24 +000074class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +000075 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000076 TException() {}
77
Mark Slee82a6c0f2007-04-04 21:08:21 +000078 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000079 message_(message) {}
80
Martin Kraemer8196a612006-12-09 01:57:58 +000081 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000082
Martin Kraemer92a2eac2007-02-05 20:58:41 +000083 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000084 if (message_.empty()) {
85 return "Default TException.";
86 } else {
87 return message_.c_str();
88 }
Mark Slee2f6404d2006-10-10 01:37:40 +000089 }
90
David Reissfaebedd2007-09-17 23:20:38 +000091 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000092 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000093
Marc Slemko5b126d62006-08-11 23:03:42 +000094};
95
Mark Sleef9831082007-02-20 20:59:21 +000096class TApplicationException : public TException {
David Reissfaebedd2007-09-17 23:20:38 +000097 public:
Mark Sleef9831082007-02-20 20:59:21 +000098
99 /**
100 * Error codes for the various types of exceptions.
101 */
David Reiss322e5952008-12-05 02:54:09 +0000102 enum TApplicationExceptionType
103 { UNKNOWN = 0
104 , UNKNOWN_METHOD = 1
105 , INVALID_MESSAGE_TYPE = 2
106 , WRONG_METHOD_NAME = 3
107 , BAD_SEQUENCE_ID = 4
108 , MISSING_RESULT = 5
Mark Sleef9831082007-02-20 20:59:21 +0000109 };
110
111 TApplicationException() :
112 TException(),
113 type_(UNKNOWN) {}
114
115 TApplicationException(TApplicationExceptionType type) :
David Reiss0c90f6f2008-02-06 22:18:40 +0000116 TException(),
Mark Sleef9831082007-02-20 20:59:21 +0000117 type_(type) {}
118
Mark Slee82a6c0f2007-04-04 21:08:21 +0000119 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000120 TException(message),
121 type_(UNKNOWN) {}
122
123 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +0000124 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000125 TException(message),
126 type_(type) {}
127
128 virtual ~TApplicationException() throw() {}
129
130 /**
131 * Returns an error code that provides information about the type of error
132 * that has occurred.
133 *
134 * @return Error code
135 */
136 TApplicationExceptionType getType() {
137 return type_;
138 }
139
140 virtual const char* what() const throw() {
141 if (message_.empty()) {
dweatherford2323cf62007-12-05 03:40:19 +0000142 switch (type_) {
David Reissadad4ab2007-12-14 20:56:04 +0000143 case UNKNOWN : return "TApplicationException: Unknown application exception";
144 case UNKNOWN_METHOD : return "TApplicationException: Unknown method";
145 case INVALID_MESSAGE_TYPE : return "TApplicationException: Invalid message type";
146 case WRONG_METHOD_NAME : return "TApplicationException: Wrong method name";
147 case BAD_SEQUENCE_ID : return "TApplicationException: Bad sequence identifier";
148 case MISSING_RESULT : return "TApplicationException: Missing result";
149 default : return "TApplicationException: (Invalid exception type)";
dweatherford2323cf62007-12-05 03:40:19 +0000150 };
Mark Sleef9831082007-02-20 20:59:21 +0000151 } else {
152 return message_.c_str();
153 }
154 }
155
Mark Sleeba8f8d72007-04-03 00:34:00 +0000156 uint32_t read(protocol::TProtocol* iprot);
157 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000158
David Reissfaebedd2007-09-17 23:20:38 +0000159 protected:
Mark Sleef9831082007-02-20 20:59:21 +0000160 /**
161 * Error code
162 */
163 TApplicationExceptionType type_;
164
165};
166
167
David Reissd779cbe2007-08-31 01:42:55 +0000168// Forward declare this structure used by TDenseProtocol
169namespace reflection { namespace local {
170struct TypeSpec;
171}}
172
173
T Jake Lucianib5e62212009-01-31 22:36:20 +0000174}} // apache::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000175
Mark Sleef5f2be42006-09-05 21:05:31 +0000176#endif // #ifndef _THRIFT_THRIFT_H_