blob: 4d2ff2042b0e0052c14f9786e898482a69597bbf [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
Mark Sleecfc01932006-09-01 22:18:16 +000027namespace facebook { 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 Reiss9b209552008-04-08 06:26:05 +000041 inline static void errorTimeWrapper(const char* msg) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000042 time_t now;
43 char dbgtime[25];
44 time(&now);
45 ctime_r(&now, dbgtime);
46 dbgtime[24] = 0;
David Reiss9b209552008-04-08 06:26:05 +000047 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000048 }
David Reiss9b209552008-04-08 06:26:05 +000049
50 /** Just like strerror_r but returns a C++ string object. */
51 static std::string strerror_s(int errno_copy);
52
David Reissfaebedd2007-09-17 23:20:38 +000053 private:
boz6ded7752007-06-05 22:41:18 +000054 void (*f_)(const char *);
55};
56
57extern TOutput GlobalOutput;
58
Mark Sleef9831082007-02-20 20:59:21 +000059namespace protocol {
60 class TProtocol;
61}
62
Mark Sleeb9ff32a2006-11-16 01:00:24 +000063class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +000064 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000065 TException() {}
66
Mark Slee82a6c0f2007-04-04 21:08:21 +000067 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000068 message_(message) {}
69
Martin Kraemer8196a612006-12-09 01:57:58 +000070 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000071
Martin Kraemer92a2eac2007-02-05 20:58:41 +000072 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000073 if (message_.empty()) {
74 return "Default TException.";
75 } else {
76 return message_.c_str();
77 }
Mark Slee2f6404d2006-10-10 01:37:40 +000078 }
79
David Reissfaebedd2007-09-17 23:20:38 +000080 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000081 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000082
Marc Slemko5b126d62006-08-11 23:03:42 +000083};
84
Mark Sleef9831082007-02-20 20:59:21 +000085class TApplicationException : public TException {
David Reissfaebedd2007-09-17 23:20:38 +000086 public:
Mark Sleef9831082007-02-20 20:59:21 +000087
88 /**
89 * Error codes for the various types of exceptions.
90 */
91 enum TApplicationExceptionType {
92 UNKNOWN = 0,
Mark Sleef3d33632007-02-21 01:39:23 +000093 UNKNOWN_METHOD = 1,
94 INVALID_MESSAGE_TYPE = 2,
95 WRONG_METHOD_NAME = 3,
96 BAD_SEQUENCE_ID = 4,
97 MISSING_RESULT = 5,
Mark Sleef9831082007-02-20 20:59:21 +000098 };
99
100 TApplicationException() :
101 TException(),
102 type_(UNKNOWN) {}
103
104 TApplicationException(TApplicationExceptionType type) :
David Reiss0c90f6f2008-02-06 22:18:40 +0000105 TException(),
Mark Sleef9831082007-02-20 20:59:21 +0000106 type_(type) {}
107
Mark Slee82a6c0f2007-04-04 21:08:21 +0000108 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000109 TException(message),
110 type_(UNKNOWN) {}
111
112 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +0000113 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000114 TException(message),
115 type_(type) {}
116
117 virtual ~TApplicationException() throw() {}
118
119 /**
120 * Returns an error code that provides information about the type of error
121 * that has occurred.
122 *
123 * @return Error code
124 */
125 TApplicationExceptionType getType() {
126 return type_;
127 }
128
129 virtual const char* what() const throw() {
130 if (message_.empty()) {
dweatherford2323cf62007-12-05 03:40:19 +0000131 switch (type_) {
David Reissadad4ab2007-12-14 20:56:04 +0000132 case UNKNOWN : return "TApplicationException: Unknown application exception";
133 case UNKNOWN_METHOD : return "TApplicationException: Unknown method";
134 case INVALID_MESSAGE_TYPE : return "TApplicationException: Invalid message type";
135 case WRONG_METHOD_NAME : return "TApplicationException: Wrong method name";
136 case BAD_SEQUENCE_ID : return "TApplicationException: Bad sequence identifier";
137 case MISSING_RESULT : return "TApplicationException: Missing result";
138 default : return "TApplicationException: (Invalid exception type)";
dweatherford2323cf62007-12-05 03:40:19 +0000139 };
Mark Sleef9831082007-02-20 20:59:21 +0000140 } else {
141 return message_.c_str();
142 }
143 }
144
Mark Sleeba8f8d72007-04-03 00:34:00 +0000145 uint32_t read(protocol::TProtocol* iprot);
146 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000147
David Reissfaebedd2007-09-17 23:20:38 +0000148 protected:
Mark Sleef9831082007-02-20 20:59:21 +0000149 /**
150 * Error code
151 */
152 TApplicationExceptionType type_;
153
154};
155
156
David Reissd779cbe2007-08-31 01:42:55 +0000157// Forward declare this structure used by TDenseProtocol
158namespace reflection { namespace local {
159struct TypeSpec;
160}}
161
162
Marc Slemko5b126d62006-08-11 23:03:42 +0000163}} // facebook::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000164
Mark Sleef5f2be42006-09-05 21:05:31 +0000165#endif // #ifndef _THRIFT_THRIFT_H_