blob: bb6a2c8cb0b2be7f7d5b183947f4170184507221 [file] [log] [blame]
Jens Geyer7a094832013-09-08 00:36:22 +02001package thrift
2
3type TSerializer struct {
4 Transport TTransport
5 Protocol TProtocol
6}
7
8type TStruct interface {
9 Write(p TProtocol) error
10 Read(p TProtocol) error
11}
12
13func NewTSerializer() *TSerializer {
14 var transport TTransport
15 transport = NewTMemoryBufferLen(1024)
16
17 protocol := NewTBinaryProtocolFactoryDefault().GetProtocol(transport)
18
19 return &TSerializer{
20 transport,
21 protocol}
22}
23
24func (t *TSerializer) WriteString(msg TStruct) (s string, err error) {
25 s = ""
26 err = nil
27
28 if err = msg.Write(t.Protocol); err != nil {
29 return
30 }
31
32 if err = t.Protocol.Flush(); err != nil {
33 return
34 }
35 if err = t.Transport.Flush(); err != nil {
36 return
37 }
38
39 var buf []byte
40 var place int
41 buf = make([]byte, 1024)
42 if place, err = t.Transport.Read(buf); err != nil {
43 return
44 }
45
46 s = string(buf[:place])
47 return
48}
49
50func (t *TSerializer) Write(msg TStruct) (b []byte, err error) {
51 err = nil
52
53 if err = msg.Write(t.Protocol); err != nil {
54 return
55 }
56
57 if err = t.Protocol.Flush(); err != nil {
58 return
59 }
60
61 if err = t.Transport.Flush(); err != nil {
62 return
63 }
64
65 var buf []byte
66 var place int
67 buf = make([]byte, 1024)
68 if place, err = t.Transport.Read(buf); err != nil {
69 return
70 }
71
72 b = buf[:place]
73 return
74}