Repository: thrift
Updated Branches:
  refs/heads/master 4606d7db9 -> eb8e5ad87


THRIFT-2654 reduce number of server exceptions
Client: C#
Patch: Craig Peterson

This closes #177


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/eb8e5ad8
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/eb8e5ad8
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/eb8e5ad8

Branch: refs/heads/master
Commit: eb8e5ad87fc8d715a40416f789774da2856a6d69
Parents: 4606d7d
Author: Jens Geyer <[email protected]>
Authored: Mon Sep 29 21:50:15 2014 +0200
Committer: Jens Geyer <[email protected]>
Committed: Mon Sep 29 21:50:15 2014 +0200

----------------------------------------------------------------------
 lib/csharp/src/Server/TSimpleServer.cs     |  3 ++
 lib/csharp/src/Server/TThreadPoolServer.cs |  3 ++
 lib/csharp/src/Server/TThreadedServer.cs   |  3 ++
 lib/csharp/src/Transport/TTransport.cs     | 60 +++++++++++++++++--------
 4 files changed, 50 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/eb8e5ad8/lib/csharp/src/Server/TSimpleServer.cs
----------------------------------------------------------------------
diff --git a/lib/csharp/src/Server/TSimpleServer.cs 
b/lib/csharp/src/Server/TSimpleServer.cs
index 75f8241..42e0cbe 100644
--- a/lib/csharp/src/Server/TSimpleServer.cs
+++ b/lib/csharp/src/Server/TSimpleServer.cs
@@ -118,6 +118,9 @@ namespace Thrift.Server
                   //Process client requests until client disconnects
                   while (true)
                   {
+                    if (!inputTransport.Peek()) 
+                      break;
+
                     //Fire processContext server event
                     //N.B. This is the pattern implemented in C++ and the 
event fires provisionally.
                     //That is to say it may be many minutes between the event 
firing and the client request

http://git-wip-us.apache.org/repos/asf/thrift/blob/eb8e5ad8/lib/csharp/src/Server/TThreadPoolServer.cs
----------------------------------------------------------------------
diff --git a/lib/csharp/src/Server/TThreadPoolServer.cs 
b/lib/csharp/src/Server/TThreadPoolServer.cs
index f26a683..8542b6d 100644
--- a/lib/csharp/src/Server/TThreadPoolServer.cs
+++ b/lib/csharp/src/Server/TThreadPoolServer.cs
@@ -172,6 +172,9 @@ namespace Thrift.Server
         //Process client requests until client disconnects
         while (true)
         {
+          if (!inputTransport.Peek()) 
+            break;
+
           //Fire processContext server event
           //N.B. This is the pattern implemented in C++ and the event fires 
provisionally.
           //That is to say it may be many minutes between the event firing and 
the client request

http://git-wip-us.apache.org/repos/asf/thrift/blob/eb8e5ad8/lib/csharp/src/Server/TThreadedServer.cs
----------------------------------------------------------------------
diff --git a/lib/csharp/src/Server/TThreadedServer.cs 
b/lib/csharp/src/Server/TThreadedServer.cs
index 5e707f5..87bff31 100644
--- a/lib/csharp/src/Server/TThreadedServer.cs
+++ b/lib/csharp/src/Server/TThreadedServer.cs
@@ -204,6 +204,9 @@ namespace Thrift.Server
             //Process client requests until client disconnects
             while (true)
             {
+              if (!inputTransport.Peek()) 
+                break;
+
               //Fire processContext server event
               //N.B. This is the pattern implemented in C++ and the event 
fires provisionally.
               //That is to say it may be many minutes between the event firing 
and the client request

http://git-wip-us.apache.org/repos/asf/thrift/blob/eb8e5ad8/lib/csharp/src/Transport/TTransport.cs
----------------------------------------------------------------------
diff --git a/lib/csharp/src/Transport/TTransport.cs 
b/lib/csharp/src/Transport/TTransport.cs
index 745aa78..5bb8f9e 100644
--- a/lib/csharp/src/Transport/TTransport.cs
+++ b/lib/csharp/src/Transport/TTransport.cs
@@ -32,10 +32,27 @@ namespace Thrift.Transport
                        get;
                }
 
-               public bool Peek()
-               {
-                       return IsOpen;
-               }
+               private byte[] _peekBuffer = new byte[1];
+        private bool _hasPeekByte = false;
+
+        public bool Peek()
+        {
+            //If we already have a byte read but not consumed, do nothing.
+            if (_hasPeekByte) 
+                return true;
+
+            //If transport closed we can't peek.
+            if (!IsOpen) 
+                return false;
+
+            //Try to read one byte. If succeeds we will need to store it for 
the next read.
+            int bytes = Read(_peekBuffer, 0, 1);
+            if (bytes == 0)
+                return false;
+
+            _hasPeekByte = true;
+            return true;
+        }
 
                public abstract void Open();
 
@@ -46,21 +63,26 @@ namespace Thrift.Transport
                public int ReadAll(byte[] buf, int off, int len)
                {
                        int got = 0;
-                       int ret = 0;
-
-                       while (got < len)
-                       {
-                               ret = Read(buf, off + got, len - got);
-                               if (ret <= 0)
-                               {
-                                       throw new TTransportException(
-                                               
TTransportException.ExceptionType.EndOfFile,
-                                               "Cannot read, Remote side has 
closed");
-                               }
-                               got += ret;
-                       }
-
-                       return got;
+
+            //If we previously peeked a byte, we need to use that first.
+            if (_hasPeekByte)
+            {
+                buf[off + got++] = _peekBuffer[0];
+                _hasPeekByte = false;
+            }
+
+            while (got < len)
+            {
+                int ret = Read(buf, off + got, len - got);
+                if (ret <= 0)
+                {
+                    throw new TTransportException(
+                        TTransportException.ExceptionType.EndOfFile,
+                        "Cannot read, Remote side has closed");
+                }
+                got += ret;
+            }
+            return got;
                }
 
                public virtual void Write(byte[] buf) 

Reply via email to