You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by es...@apache.org on 2009/11/11 19:32:25 UTC

svn commit: r835006 - in /incubator/thrift/trunk/lib/csharp: Makefile.am src/Thrift.csproj src/Transport/TFramedTransport.cs

Author: esteve
Date: Wed Nov 11 18:32:25 2009
New Revision: 835006

URL: http://svn.apache.org/viewvc?rev=835006&view=rev
Log:
THRIFT-210. c#: Add support for TFramedTransport

author: Michael Greene (euphoria)

Added:
    incubator/thrift/trunk/lib/csharp/src/Transport/TFramedTransport.cs
Modified:
    incubator/thrift/trunk/lib/csharp/Makefile.am
    incubator/thrift/trunk/lib/csharp/src/Thrift.csproj

Modified: incubator/thrift/trunk/lib/csharp/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/csharp/Makefile.am?rev=835006&r1=835005&r2=835006&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/csharp/Makefile.am (original)
+++ incubator/thrift/trunk/lib/csharp/Makefile.am Wed Nov 11 18:32:25 2009
@@ -42,6 +42,7 @@
             src/Transport/TSocket.cs \
             src/Transport/TTransportException.cs \
             src/Transport/TStreamTransport.cs \
+            src/Transport/TFramedTransport.cs \
             src/Transport/TServerTransport.cs \
             src/Transport/TServerSocket.cs \
             src/Transport/TTransportFactory.cs \

Modified: incubator/thrift/trunk/lib/csharp/src/Thrift.csproj
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/csharp/src/Thrift.csproj?rev=835006&r1=835005&r2=835006&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/csharp/src/Thrift.csproj (original)
+++ incubator/thrift/trunk/lib/csharp/src/Thrift.csproj Wed Nov 11 18:32:25 2009
@@ -58,6 +58,7 @@
     <Compile Include="TApplicationException.cs" />
     <Compile Include="TProcessor.cs" />
     <Compile Include="Transport\TBufferedTransport.cs" />
+    <Compile Include="Transport\TFramedTransport.cs" />
     <Compile Include="Transport\TServerSocket.cs" />
     <Compile Include="Transport\TServerTransport.cs" />
     <Compile Include="Transport\TSocket.cs" />
@@ -70,4 +71,4 @@
   <ProjectExtensions>
     <VisualStudio AllowExistingFolder="true" />
   </ProjectExtensions>
-</Project>
\ No newline at end of file
+</Project>

Added: incubator/thrift/trunk/lib/csharp/src/Transport/TFramedTransport.cs
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/csharp/src/Transport/TFramedTransport.cs?rev=835006&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/csharp/src/Transport/TFramedTransport.cs (added)
+++ incubator/thrift/trunk/lib/csharp/src/Transport/TFramedTransport.cs Wed Nov 11 18:32:25 2009
@@ -0,0 +1,114 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System.IO;
+
+namespace Thrift.Transport
+{
+	public class TFramedTransport : TTransport
+	{
+		protected TTransport transport = null;
+		protected MemoryStream writeBuffer = new MemoryStream(1024);
+		protected MemoryStream readBuffer = null;
+
+		public class Factory : TTransportFactory
+		{
+			public override TTransport GetTransport(TTransport trans)
+			{
+				return new TFramedTransport(trans);
+			}
+		}
+
+		public TFramedTransport(TTransport transport)
+		{
+			this.transport = transport;
+		}
+
+		public override void Open()
+		{
+			transport.Open();
+		}
+
+		public override bool IsOpen
+		{
+			get
+			{
+				return transport.IsOpen;
+			}
+		}
+
+		public override void Close()
+		{
+			transport.Close();
+		}
+
+		public override int Read(byte[] buf, int off, int len)
+		{
+			if (readBuffer != null)
+			{
+				int got = readBuffer.Read(buf, off, len);
+				if (got > 0)
+				{
+					return got;
+				}
+			}
+
+			// Read another frame of data
+			ReadFrame();
+
+			return readBuffer.Read(buf, off, len);
+		}
+
+		private void ReadFrame()
+		{
+			byte[] i32rd = new byte[4];
+			transport.ReadAll(i32rd, 0, 4);
+			int size =
+				((i32rd[0] & 0xff) << 24) |
+				((i32rd[1] & 0xff) << 16) |
+				((i32rd[2] & 0xff) <<  8) |
+				((i32rd[3] & 0xff));
+
+			byte[] buff = new byte[size];
+			transport.ReadAll(buff, 0, size);
+			readBuffer = new MemoryStream(buff);
+		}
+
+		public override void Write(byte[] buf, int off, int len)
+		{
+			writeBuffer.Write(buf, off, len);
+		}
+
+		public override void Flush()
+		{
+			byte[] buf = writeBuffer.GetBuffer();
+			int len = (int)writeBuffer.Length;
+			writeBuffer = new MemoryStream(writeBuffer.Capacity);
+
+			byte[] i32out = new byte[4];
+			i32out[0] = (byte)(0xff & (len >> 24));
+			i32out[1] = (byte)(0xff & (len >> 16));
+			i32out[2] = (byte)(0xff & (len >> 8));
+			i32out[3] = (byte)(0xff & (len));
+			transport.Write(i32out, 0, 4);
+			transport.Write(buf, 0, len);
+			transport.Flush();
+		}
+	}
+}