blob: 70920d47b2f744efbf54435b38677f79e2bc44af [file] [log] [blame]
Kevin Clarkab4460d2009-03-20 02:28:41 +00001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Todd Lipcon53ae9f32009-12-07 00:42:38 +000018 *
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
Kevin Clarkab4460d2009-03-20 02:28:41 +000022 */
David Reiss7f42bcf2008-01-11 20:59:12 +000023
24using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using System.Text;
26using Thrift.Transport;
27
28namespace Thrift.Protocol
29{
Jens Geyer102bca42013-06-25 22:21:29 +020030 public abstract class TProtocol : IDisposable
David Reiss7f42bcf2008-01-11 20:59:12 +000031 {
32 protected TTransport trans;
33
34 protected TProtocol(TTransport trans)
35 {
36 this.trans = trans;
37 }
38
39 public TTransport Transport
40 {
41 get { return trans; }
42 }
43
Jens Geyer102bca42013-06-25 22:21:29 +020044 #region " IDisposable Support "
45 private bool _IsDisposed;
46
47 // IDisposable
48 public void Dispose()
49 {
50 Dispose(true);
51 }
52
53 protected virtual void Dispose(bool disposing)
54 {
55 if (!_IsDisposed)
56 {
57 if (disposing)
58 {
59 if (trans is IDisposable)
60 (trans as IDisposable).Dispose();
61 }
62 }
63 _IsDisposed = true;
64 }
65 #endregion
66
David Reiss7f42bcf2008-01-11 20:59:12 +000067 public abstract void WriteMessageBegin(TMessage message);
68 public abstract void WriteMessageEnd();
69 public abstract void WriteStructBegin(TStruct struc);
70 public abstract void WriteStructEnd();
71 public abstract void WriteFieldBegin(TField field);
72 public abstract void WriteFieldEnd();
73 public abstract void WriteFieldStop();
74 public abstract void WriteMapBegin(TMap map);
75 public abstract void WriteMapEnd();
76 public abstract void WriteListBegin(TList list);
77 public abstract void WriteListEnd();
78 public abstract void WriteSetBegin(TSet set);
79 public abstract void WriteSetEnd();
80 public abstract void WriteBool(bool b);
Jens Geyerf509df92013-04-25 20:38:55 +020081 public abstract void WriteByte(sbyte b);
David Reiss7f42bcf2008-01-11 20:59:12 +000082 public abstract void WriteI16(short i16);
83 public abstract void WriteI32(int i32);
84 public abstract void WriteI64(long i64);
85 public abstract void WriteDouble(double d);
Bryan Duxburyfd32d792010-09-18 20:51:25 +000086 public virtual void WriteString(string s) {
David Reisscba57272008-02-06 22:09:44 +000087 WriteBinary(Encoding.UTF8.GetBytes(s));
88 }
89 public abstract void WriteBinary(byte[] b);
David Reiss7f42bcf2008-01-11 20:59:12 +000090
91 public abstract TMessage ReadMessageBegin();
92 public abstract void ReadMessageEnd();
93 public abstract TStruct ReadStructBegin();
94 public abstract void ReadStructEnd();
95 public abstract TField ReadFieldBegin();
96 public abstract void ReadFieldEnd();
97 public abstract TMap ReadMapBegin();
98 public abstract void ReadMapEnd();
99 public abstract TList ReadListBegin();
100 public abstract void ReadListEnd();
101 public abstract TSet ReadSetBegin();
102 public abstract void ReadSetEnd();
103 public abstract bool ReadBool();
Jens Geyerf509df92013-04-25 20:38:55 +0200104 public abstract sbyte ReadByte();
David Reiss7f42bcf2008-01-11 20:59:12 +0000105 public abstract short ReadI16();
106 public abstract int ReadI32();
107 public abstract long ReadI64();
108 public abstract double ReadDouble();
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000109 public virtual string ReadString() {
Roger Meier284a9b52011-12-08 13:39:56 +0000110 var buf = ReadBinary();
111 return Encoding.UTF8.GetString(buf, 0, buf.Length);
112 }
David Reisscba57272008-02-06 22:09:44 +0000113 public abstract byte[] ReadBinary();
David Reiss7f42bcf2008-01-11 20:59:12 +0000114 }
115}