Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/**
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.
18
 */
19
 
20
using System.IO;
21
 
22
namespace Thrift.Transport
23
{
24
	public class TFramedTransport : TTransport
25
	{
26
		protected TTransport transport = null;
27
		protected MemoryStream writeBuffer = new MemoryStream(1024);
28
		protected MemoryStream readBuffer = null;
29
 
30
		public class Factory : TTransportFactory
31
		{
32
			public override TTransport GetTransport(TTransport trans)
33
			{
34
				return new TFramedTransport(trans);
35
			}
36
		}
37
 
38
		public TFramedTransport(TTransport transport)
39
		{
40
			this.transport = transport;
41
		}
42
 
43
		public override void Open()
44
		{
45
			transport.Open();
46
		}
47
 
48
		public override bool IsOpen
49
		{
50
			get
51
			{
52
				return transport.IsOpen;
53
			}
54
		}
55
 
56
		public override void Close()
57
		{
58
			transport.Close();
59
		}
60
 
61
		public override int Read(byte[] buf, int off, int len)
62
		{
63
			if (readBuffer != null)
64
			{
65
				int got = readBuffer.Read(buf, off, len);
66
				if (got > 0)
67
				{
68
					return got;
69
				}
70
			}
71
 
72
			// Read another frame of data
73
			ReadFrame();
74
 
75
			return readBuffer.Read(buf, off, len);
76
		}
77
 
78
		private void ReadFrame()
79
		{
80
			byte[] i32rd = new byte[4];
81
			transport.ReadAll(i32rd, 0, 4);
82
			int size =
83
				((i32rd[0] & 0xff) << 24) |
84
				((i32rd[1] & 0xff) << 16) |
85
				((i32rd[2] & 0xff) <<  8) |
86
				((i32rd[3] & 0xff));
87
 
88
			byte[] buff = new byte[size];
89
			transport.ReadAll(buff, 0, size);
90
			readBuffer = new MemoryStream(buff);
91
		}
92
 
93
		public override void Write(byte[] buf, int off, int len)
94
		{
95
			writeBuffer.Write(buf, off, len);
96
		}
97
 
98
		public override void Flush()
99
		{
100
			byte[] buf = writeBuffer.GetBuffer();
101
			int len = (int)writeBuffer.Length;
102
			writeBuffer = new MemoryStream(writeBuffer.Capacity);
103
 
104
			byte[] i32out = new byte[4];
105
			i32out[0] = (byte)(0xff & (len >> 24));
106
			i32out[1] = (byte)(0xff & (len >> 16));
107
			i32out[2] = (byte)(0xff & (len >> 8));
108
			i32out[3] = (byte)(0xff & (len));
109
			transport.Write(i32out, 0, 4);
110
			transport.Write(buf, 0, len);
111
			transport.Flush();
112
		}
113
	}
114
}