00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <Basics.H>
00026
00027
00028
00029
00030
00031 class SRPC_listener;
00032
00033 class SRPC_impl {
00034 friend class SRPC;
00035 public:
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 enum item_code {
00061 ic_null = 0,
00062 ic_hello = 10,
00063 ic_start_call = 11,
00064 ic_end_call = 12,
00065 ic_end_ack = 13,
00066 ic_seq_start = 20,
00067 ic_seq_end = 21,
00068 ic_int16 = 28,
00069 ic_int16_array = 29,
00070 ic_int32 = 30,
00071 ic_int32_seq = 31,
00072 ic_chars = 32,
00073 ic_chars_seq = 33,
00074 ic_bytes = 34,
00075 ic_bytes_seq = 35,
00076 ic_text = 36,
00077 ic_socket = 40,
00078
00079
00080 ic_int64 = 41,
00081 ic_int32_array = 42,
00082 ic_int64_array = 43,
00083
00084 ic_bool_true = 44,
00085 ic_bool_false = 45,
00086
00087 ic_failure = 255
00088 };
00089
00090
00091
00092 enum rpc_state {
00093 initial = 0,
00094 ready = 1,
00095 data_out = 2,
00096 seq_out = 3,
00097 data_in = 4,
00098 seq_in = 5,
00099 failed = 6
00100 };
00101
00102 enum ops {
00103 op_start_call = 0,
00104 op_await_call = 1,
00105 op_send_int32 = 2,
00106 op_recv_int32 = 3,
00107 op_send_chars = 4,
00108 op_recv_chars = 5,
00109 op_recv_chars_here = 6,
00110 op_send_bytes = 7,
00111 op_recv_bytes = 8,
00112 op_recv_bytes_here = 9,
00113 op_send_text = 10,
00114 op_recv_text = 11,
00115 op_send_socket = 12,
00116 op_recv_socket = 13,
00117 op_send_int32_seq = 14,
00118 op_recv_int32_seq = 15,
00119 op_send_chars_seq = 16,
00120 op_recv_chars_seq = 17,
00121 op_send_bytes_seq = 18,
00122 op_recv_bytes_seq = 19,
00123 op_send_seq_start = 20,
00124 op_recv_seq_start = 21,
00125 op_send_seq_end = 22,
00126 op_recv_seq_end = 23,
00127 op_send_end = 24,
00128 op_recv_end = 25,
00129 op_send_failure = 26,
00130
00131
00132 op_send_int16 = 27,
00133 op_recv_int16 = 28,
00134 op_send_int16_array = 29,
00135 op_recv_int16_array = 30,
00136
00137
00138 op_send_int64 = 31,
00139 op_recv_int64 = 32,
00140
00141
00142 op_send_int32_array = 33,
00143 op_recv_int32_array = 34,
00144 op_send_int64_array = 35,
00145 op_recv_int64_array = 36,
00146
00147
00148 op_send_bool = 37,
00149 op_recv_bool = 38,
00150
00151 };
00152
00153
00154
00155
00156
00157
00158
00159
00160 SRPC_impl(bool i_am_caller);
00161 ~SRPC_impl() throw (SRPC::failure);
00162
00163
00164
00165 void send_item_code(item_code ic, bool flush = false)
00166 throw (TCP_sock::failure);
00167 item_code read_item_code()
00168 throw (TCP_sock::failure, SRPC::failure);
00169
00170
00171 void recv_item_code(item_code expected, bool *got_end = NULL)
00172 throw (TCP_sock::failure, SRPC::failure);
00173
00174
00175
00176
00177 item_code recv_item_code(item_code expected1, item_code expected2,
00178 bool *got_end)
00179 throw (TCP_sock::failure, SRPC::failure);
00180
00181
00182
00183
00184
00185
00186
00187
00188 void send_int16(Basics::int16 i, bool flush = false)
00189 throw (TCP_sock::failure);
00190 Basics::int16 recv_int16() throw (TCP_sock::failure);
00191
00192 void send_int32(Basics::int32 i, bool flush = false)
00193 throw (TCP_sock::failure);
00194 Basics::int32 recv_int32() throw (TCP_sock::failure);
00195
00196 void send_int64(Basics::int64 i, bool flush = false)
00197 throw (TCP_sock::failure);
00198 Basics::int64 recv_int64() throw (TCP_sock::failure);
00199
00200
00201
00202 void send_bytes(const char *bs, int len, bool flush = false)
00203 throw (TCP_sock::failure);
00204 void recv_bytes(char *bs, int len)
00205 throw (TCP_sock::failure, SRPC::failure);
00206
00207
00208
00209
00210
00211 void check_state(int op) throw (SRPC::failure);
00212 void ensure_sending_state() throw ();
00213 void ensure_recving_state() throw ();
00214
00215
00216
00217 void throw_failure(int reason, const Text &msg, bool local_only = false)
00218 throw (SRPC::failure);
00219 void report_TCP_failure(TCP_sock::failure &tf)
00220 throw (SRPC::failure);
00221
00222
00223
00224
00225
00226 rpc_state state;
00227 SRPC::failure f;
00228
00229 bool is_caller;
00230 bool is_sender;
00231 bool version_sent;
00232 bool version_checked;
00233
00234 item_code buffered_ic;
00235
00236 TCP_sock *s;
00237 SRPC_listener *listener;
00238
00239 bool use_read_timeout;
00240 bool use_read_timeout_between_calls;
00241 unsigned int read_timeout_secs;
00242 };
00243
00244
00245
00246
00247
00248 class SRPC_listener {
00249 public:
00250 TCP_sock *sock;
00251 bool my_sock;
00252 u_short port;
00253
00254
00255 static SRPC_listener *create(u_short port, TCP_sock *ls = NULL)
00256 throw (TCP_sock::failure);
00257
00258 static bool destroy(SRPC_listener *listener)
00259 throw (TCP_sock::failure);
00260
00261 private:
00262 SRPC_listener *next;
00263 int n;
00264
00265
00266 static Basics::mutex m;
00267 static SRPC_listener *listeners;
00268 };
00269
00270
00271
00272 typedef SRPC_impl::item_code item_code;
00273 typedef char *chars;