我必须在 Thrift 中使用客户端/服务器框架吗?这是我的代码。
handle(Req, State) ->
{Method, Req2} = cowboy_req:method(Req),
{ok, Echo, Req3} = cowboy_req:body (Req2), %% Echo is a serialized 'hello' in smallTest by json format.
{ok, Trans} = thrift_memory_buffer:new(Echo),
{ok, Protocol} = thrift_json_protocol:new(Trans),
{_Protocol1, {ok, Struct}} = thrift_protocol:read(Protocol, smallTest_types:struct_info('hello')), %% Error here
{ok, Req4} = echo(Method, Echo, Req3),
{ok, Req4, State}.
堆栈跟踪:
** Stacktrace: [{thrift_json_protocol,expect,
[{json_protocol,
{transport,thrift_memory_buffer,
{memory_buffer,
[<<"{\"4\":{\"str\":\"Hello World!\"}}">>]}},
[],undefined},
start_object],
[{file,
"c:/Dev/server/thrift/src/thrift_json_protocol.erl"},
{line,349}]},
{thrift_json_protocol,expect_many_1,4,
[{file,
"c:/Dev/server/thrift/src/thrift_json_protocol.erl"},
{line,371}]},
{thrift_json_protocol,expect_nodata,2,
[{file,
"c:/Dev/server/thrift/src/thrift_json_protocol.erl"},
{line,381}]},
{thrift_protocol,read_specific,2,
[{file,"c:/Dev/server/thrift/src/thrift_protocol.erl"},
{line,186}]},
{thrift_protocol,read,3,
[{file,"c:/Dev/server/thrift/src/thrift_protocol.erl"},
{line,110}]},
{toppage_handler,handle,2,
[{file,"c:/Dev/server/src/toppage_handler.erl"},
{line,19}]},
{cowboy_handler,handler_handle,4,
[{file,"c:/Dev/server/cowboy/src/cowboy_handler.erl"},
{line,119}]},
{cowboy_protocol,execute,4,
[{file,"c:/Dev/server/cowboy/src/cowboy_protocol.erl"},
{line,514}]}]
看了thrift_json_protocol.erl,发现thrift_protocol:read是通过thrift_json_protocol:expect_nodata调用的,传入了undefined tag,应该是jsx={event, ...}。看来只有thrift_json_protocol:read(This0, message_begin)才是真正读取数据.