1 #ifndef NYLPC_CJSONRPCPARSER_H_
\r
2 #define NYLPC_CJSONRPCPARSER_H_
\r
7 #include "NyLPC_stdlib.h"
\r
10 #define NyLPC_TJsonRpcErrorCode_PARSE_ERROR (-32700)
\r
11 #define NyLPC_TJsonRpcErrorCode_INVALID_REQUEST (-32600)
\r
12 #define NyLPC_TJsonRpcErrorCode_METHOD_NOT_FOUND (-32601)
\r
13 #define NyLPC_TJsonRpcErrorCode_INVALID_PARAMS (-32602)
\r
14 #define NyLPC_TJsonRpcErrorCode_INTERNAL_ERROR (-32603)
\r
15 #define NyLPC_TJsonRpcErrorCode_SERVER_ERROR_BASE (-32000)
\r
19 union NyLPC_TJsonRpcParserResult;
\r
25 typedef NyLPC_TBool (*NyLPC_TJsonRpcHandler)(const union NyLPC_TJsonRpcParserResult* i_rpc,void* i_param);
\r
28 * JSON RPC定義テーブルの一要素。
\r
29 * メソッド名とパラメータパターンのセットを定義します。
\r
30 * この構造体は配列としてNyLPC_TJsonRpcFunctionTableから参照されます。
\r
32 struct NyLPC_TJsonRpcMethodDef
\r
42 * <li>i - signed int32</li>
\r
43 * <li>u - unsigned int32</li>
\r
44 * <li>b - unsigned int8</li>
\r
45 * <li>B - BSTR (string ByteArray)</li>
\r
48 const char* param_patt;
\r
50 * 外部のDispatch関数で使われるRPC関数ハンドラ。
\r
52 NyLPC_TJsonRpcHandler handler;
\r
57 * JsonRPCクラスの定義テーブル。
\r
58 * JsonRPCは、 [namespace]::[]
\r
60 struct NyLPC_TJsonRpcClassDef
\r
62 const char* names_pace;
\r
63 const char* class_name;
\r
64 const struct NyLPC_TJsonRpcMethodDef* functions;
\r
71 #define NyLPC_TJsonRpcParserResult_TYPE_UNKNOWN 0
\r
72 #define NyLPC_TJsonRpcParserResult_TYPE_RESULT 1
\r
73 #define NyLPC_TJsonRpcParserResult_TYPE_METHOD 2
\r
75 /** NyLPC_TJsonRpcParserResultが格納できる引数の最大数。パラメータの最大数に等しくなります。 */
\r
76 #define NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX 32
\r
78 * NyLPC_TJsonRpcParserResultが格納できるパラメータの総バイト数。
\r
79 * (文字数+1)+(uint32|int32)*4+uint8の合計値です。
\r
81 #define NyLPC_TJsonRpcParserResult_PARAM_BUF 256
\r
84 * JSONRPC構文のパース結果を格納します。
\r
86 * 更新する場合は、param_indexまでの構造体のレイアウトを破壊しないようにしてください。
\r
88 union NyLPC_TJsonRpcParserResult
\r
90 NyLPC_TUInt8 type; //タイプ
\r
91 /** この構造体は type==NyLPC_TJsonRpcParserResult_TYPE_METHODのときに有効です。 */
\r
93 NyLPC_TUInt8 _type; //タイプ
\r
94 NyLPC_TUInt8 func_number; //確定した関数番号。
\r
95 NyLPC_TUInt8 _padding[2]; //パディング
\r
96 NyLPC_TInt32 id; //idパラメータの値
\r
97 NyLPC_TChar param_buf[NyLPC_TJsonRpcParserResult_PARAM_BUF];
\r
98 NyLPC_TUInt8 param_index[NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX];
\r
99 /** 関数の含まれるクラスへのポインタ。 */
\r
100 const struct NyLPC_TJsonRpcClassDef* class_def;
\r
103 NyLPC_TUInt8 _type; //タイプ
\r
104 NyLPC_TUInt8 _padding[3]; //パディング
\r
105 NyLPC_TInt32 id; //idパラメータの値
\r
106 NyLPC_TChar param_buf[NyLPC_TJsonRpcParserResult_PARAM_BUF];
\r
107 NyLPC_TUInt8 param_index[NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX];
\r
110 NyLPC_TUInt8 _type; //タイプ
\r
111 NyLPC_TInt8 _padding[3];//
\r
112 NyLPC_TInt32 _id; //idパラメータの値
\r
113 NyLPC_TChar param_buf[NyLPC_TJsonRpcParserResult_PARAM_BUF];
\r
114 NyLPC_TUInt8 param_index[NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX];
\r
118 * Resultに格納される結果がハンドラを持っているかを返します。
\r
120 #define NyLPC_TJsonRpcParserResult_hasMethodHandler(i_struct) ((i_struct)->method.class_def->functions[(i_struct)->method.func_number].handler!=NULL)
\r
122 * Resultに含まれるハンドラを呼び出します。
\r
128 * FALSEの場合、ループを終了してください。
\r
130 #define NyLPC_TJsonRpcParserResult_callMethodHandler(i_struct,i_param) (i_struct)->method.class_def->functions[(i_struct)->method.func_number].handler((i_struct),(i_param))
\r
133 * i_idx番目のパラメータをuint32としてo_valへ取り出します。
\r
135 NyLPC_TBool NyLPC_TJsonRpcParserResult_getUInt32(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TUInt32* o_val);
\r
137 * i_idx番目のパラメータをint32としてo_valへ取り出します。
\r
139 NyLPC_TBool NyLPC_TJsonRpcParserResult_getInt32(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TInt32* o_val);
\r
141 * i_idx番目のパラメータをchar[]としてo_valへ取り出します。
\r
143 NyLPC_TBool NyLPC_TJsonRpcParserResult_getStr(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TChar** o_val, NyLPC_TUInt8* o_len);
\r
145 * i_idx番目のパラメータをuchar[]としてo_valへ取り出します。
\r
147 NyLPC_TBool NyLPC_TJsonRpcParserResult_getByteArray(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TUInt8** o_val, NyLPC_TUInt8* o_len);
\r
150 * i_idx番目のパラメータをuint8としてo_valへ取り出します。
\r
152 NyLPC_TBool NyLPC_TJsonRpcParserResult_getByte(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TUInt8* o_val);
\r
154 /********************************************************************************
\r
156 * NyLPC_TcJsonRpcParser
\r
158 ********************************************************************************/
\r
162 #define NyLPC_cJsonRpcParser_TYPE_INT32 'd'
\r
163 #define NyLPC_cJsonRpcParser_TYPE_UINT32 'u'
\r
164 #define NyLPC_cJsonRpcParser_TYPE_STRING 's'
\r
165 #define NyLPC_cJsonRpcParser_TYPE_BYTE 'b'
\r
166 #define NyLPC_cJsonRpcParser_TYPE_BSTRING 'B'
\r
169 * JsonRPCメッセージをパースします。パース出来るメッセージは以下の通りです。
\r
170 * クラスは、RPC関数定義テーブルに従ってメッセージを分析し、テーブルに存在する関数のみを返却することができます。
\r
175 * {"method":METHOD,"version":VERSION,"params":PARAMS,"id":ID}
\r
176 * METHOD VERSION as string
\r
178 * PARAMS as Array of (string|uint32|int32)
\r
182 typedef struct NyLPC_TcJsonRpcParser NyLPC_TcJsonRpcParser_t;
\r
188 typedef NyLPC_TUInt8 NyLPC_TcJsonRpcParser_TStatus;
\r
190 #define NyLPC_TcJsonRpcParser_ST_START 0x01 //開始ブランケット受信待ち
\r
191 #define NyLPC_TcJsonRpcParser_ST_END 0x02 //終了受信済
\r
192 #define NyLPC_TcJsonRpcParser_ST_ERROR 0x03 //エラー発生
\r
193 #define NyLPC_TcJsonRpcParser_ST_NAME_Q 0x04 //名前クオート受信待ち
\r
194 #define NyLPC_TcJsonRpcParser_ST_NAME_STR 0x05 //名前受信中
\r
195 #define NyLPC_TcJsonRpcParser_ST_NV_SEP 0x06 //名前と値のセパレータ待ち
\r
196 #define NyLPC_TcJsonRpcParser_ST_VAL 0x07 //値開始待ち
\r
197 #define NyLPC_TcJsonRpcParser_ST_VAL_STR 0x08 //文字列受信
\r
198 #define NyLPC_TcJsonRpcParser_ST_VAL_UINT 0x09 //UINT受信中
\r
199 #define NyLPC_TcJsonRpcParser_ST_VAL_INT 0x10 //INT受信中
\r
200 #define NyLPC_TcJsonRpcParser_ST_NEXT 0x11 //次のNAMEもしくは終了ブランケット
\r
201 #define NyLPC_TcJsonRpcParser_ST_PARAMS 0x12 //PARAM要素パース中
\r
205 /** NyLPC_TcJsonRpcParserの定数値です。 字句解析ワークメモリの長さ。256未満8*n-4の数を指定してください。 */
\r
206 #define NyLPC_TcJsonRpcParser_WORK_MAX (48-4)
\r
211 struct NyLPC_TcJsonRpcParser
\r
213 const struct NyLPC_TJsonRpcClassDef** _class_def;
\r
216 NyLPC_TChar buf[NyLPC_TcJsonRpcParser_WORK_MAX]; //文字解析メモリ
\r
217 NyLPC_TUInt8 n; //字句解析の文字数
\r
218 NyLPC_TUInt8 st; //字句解析のサブステータス
\r
219 NyLPC_TUInt8 vt; //数字エスケープ解釈のテンポラリ
\r
220 NyLPC_TUInt8 _padding; //
\r
226 NyLPC_TUInt32 uint32;
\r
228 NyLPC_TcJsonRpcParser_TStatus _st; //パーサステータス
\r
229 NyLPC_TUInt8 _pst; //PARAMS解析ステータス
\r
230 NyLPC_TUInt8 _name_id; //解析中のNAME_ID
\r
231 NyLPC_TUInt8 _pcounter; //パラメタ解析に使うワークカウンタ
\r
232 union NyLPC_TJsonRpcParserResult* _result;//出力格納先
\r
242 * @param i_class_def
\r
243 * クラステーブルの配列です。NULLで終端します。
\r
245 void NyLPC_cJsonRpcParser_initialize(
\r
246 NyLPC_TcJsonRpcParser_t* i_inst,
\r
247 const struct NyLPC_TJsonRpcClassDef** i_class_def);
\r
249 #define NyLPC_cJsonRpcParser_finalize(i)
\r
253 * パース結果の出力先構造体のアドレスです。
\r
255 void NyLPC_cJsonRpcParser_initParser(NyLPC_TcJsonRpcParser_t* i_inst, union NyLPC_TJsonRpcParserResult* i_result);
\r
257 * パーサに文字列を入力します。入力後は、NyLPC_cJsonRpcParser_getStatusでパーサの状態をチェックしてください。
\r
259 void NyLPC_cJsonRpcParser_putChar(NyLPC_TcJsonRpcParser_t* i_inst, char i_c);
\r
261 #define NyLPC_cJsonRpcParser_getStatus(i) ((i)->_st)
\r
262 /** クラス定義テーブルを返します。*/
\r
263 #define NyLPC_cJsonRpcParser_getClassDef(i) ((i)->_class_def)
\r
268 #endif /* __cplusplus */
\r
270 #endif /* NYLPC_CJSONRPCPARSER4_H_ */
\r