Example #1
0
 private static void ReadUnsignedVarint(
     Stream stream,
     Bcp.ReadState readState,
     ProcessReadVarint processReadVarint,
     BcpDelegate.ExceptionHandler exceptionHandler)
 {
     var buffer = new byte[1];
     var i = 0;
     uint result = 0U;
     AsyncCallback asyncCallback = null;
     asyncCallback = asyncResult =>
     {
         try
         {
             int numBytesRead = stream.EndRead(asyncResult);
             if (numBytesRead != 1)
             {
                 exceptionHandler(new EndOfStreamException());
             }
             uint b = buffer[0];
             if (i < 32)
             {
                 if (b >= 0x80)
                 {
                     result |= ((b & 0x7f) << i);
                     i += 7;
                     stream.BeginRead(buffer, 0, 1, asyncCallback, readState);
                 }
                 else
                 {
                     result |= (b << i);
                     processReadVarint(result);
                 }
             }
             else
             {
                 exceptionHandler(new BcpException.VarintTooBig());
             }
         }
         catch (Exception e)
         {
             exceptionHandler(e);
         }
     };
     try
     {
         stream.BeginRead(buffer, 0, 1, asyncCallback, readState);
     }
     catch (Exception e)
     {
         exceptionHandler(e);
     }
 }
Example #2
0
 public static void Write(Stream stream, Bcp.Acknowledge packet)
 {
     try
     {
         stream.WriteByte(Bcp.Acknowledge.HeadByte);
     }
     catch
     {
         stream.Close();
     }
 }
Example #3
0
 public static void WriteHead(Stream stream, Bcp.ConnectionHead head)
 {
     try
     {
         stream.Write(head.SessionId, 0, Bcp.NumBytesSessionId);
         WriteUnsignedVarint(stream, Convert.ToUInt32(head.IsRenew));
         WriteUnsignedVarint(stream, head.ConnectionId);
     }
     catch
     {
     }
 }
Example #4
0
 public static void ReadHead(Stream stream,
     Bcp.ReadState readState,
     BcpDelegate.ProcessReadHead processReadHead,
     BcpDelegate.ExceptionHandler exceptionHandler)
 {
     var sessionId = new byte[Bcp.NumBytesSessionId];
     ProcessReadAll processReadAll = delegate()
     {
         ProcessReadVarint processReadIsRenew = delegate(uint isRenew)
         {
             ProcessReadVarint processReadConnectionId = delegate(uint connectionId)
             {
                 readState.Cancel();
                 processReadHead(new Bcp.ConnectionHead(sessionId, Convert.ToBoolean(isRenew), connectionId));
             };
             ReadUnsignedVarint(stream, readState, processReadConnectionId, exceptionHandler);
         };
         ReadUnsignedVarint(stream, readState, processReadIsRenew, exceptionHandler);
     };
     ReadAll(stream, readState, sessionId, 0, Bcp.NumBytesSessionId, processReadAll, exceptionHandler);
 }
Example #5
0
        public static void Read(Stream stream, Bcp.ReadState readState, BcpDelegate.ProcessRead processRead, BcpDelegate.ExceptionHandler exceptionHandler)
        {
            var headBuffer = new byte[1];
            AsyncCallback asyncCallback = null;
            asyncCallback = asyncResult =>
            {
                try
                {
                    int numBytesRead = stream.EndRead(asyncResult);
                    if (numBytesRead != 1)
                    {
                        throw new EndOfStreamException();
                    }
                    switch (headBuffer[0])
                    {
                        case Bcp.Data.HeadByte:
                            {
                                ProcessReadVarint processReadLength = delegate(uint length)
                                {
                                    if (length > Bcp.MaxDataSize)
                                    {
                                        throw new BcpException.DataTooBig();
                                    }
                                    var buffer = new byte[length];
                                    ProcessReadAll processReadAll = delegate()
                                    {
                                        processRead(new Bcp.Data(new[] { (new ArraySegment<byte>(buffer)) }));
                                    };
                                    ReadAll(stream, readState, buffer, 0, (int)length, processReadAll, exceptionHandler);
                                };
                                ReadUnsignedVarint(stream, readState, processReadLength, exceptionHandler);
                                break;
                            }
                        case Bcp.RetransmissionData.HeadByte:
                            {
                                ProcessReadVarint processReadConnectionId = delegate(uint connectionId)
                                {
                                    ProcessReadVarint processReadPackId = delegate(uint packId)
                                    {
                                        ProcessReadVarint processReadLength = delegate(uint length)
                                        {
                                            if (length > Bcp.MaxDataSize)
                                            {
                                                throw new BcpException.DataTooBig();
                                            }
                                            var buffer = new byte[length];
                                            ProcessReadAll processReadAll = delegate()
                                            {
                                                processRead(new Bcp.RetransmissionData(connectionId, packId, new[] { (new ArraySegment<byte>(buffer)) }));
                                            };
                                            ReadAll(stream, readState, buffer, 0, (int)length, processReadAll, exceptionHandler);

                                        };
                                        ReadUnsignedVarint(stream, readState, processReadLength, exceptionHandler);
                                    };
                                    ReadUnsignedVarint(stream, readState, processReadPackId, exceptionHandler);
                                };
                                ReadUnsignedVarint(stream, readState, processReadConnectionId, exceptionHandler);
                                break;
                            }
                        case Bcp.RetransmissionFinish.HeadByte:
                            {
                                ProcessReadVarint processReadConnectionId = delegate(uint connectionId)
                                {
                                    ProcessReadVarint processReadPackId = delegate(uint packId)
                                    {
                                        processRead(new Bcp.RetransmissionFinish(connectionId, packId));
                                    };
                                    ReadUnsignedVarint(stream, readState, processReadPackId, exceptionHandler);
                                };
                                ReadUnsignedVarint(stream, readState, processReadConnectionId, exceptionHandler);
                                break;
                            }
                        case Bcp.Acknowledge.HeadByte:
                            processRead(new Bcp.Acknowledge());
                            break;
                        case Bcp.Finish.HeadByte:
                            processRead(new Bcp.Finish());
                            break;
                        case Bcp.ShutDown.HeadByte:
                            processRead(new Bcp.ShutDown());
                            break;
                        case Bcp.HeartBeat.HeadByte:
                            processRead(new Bcp.HeartBeat());
                            break;
                        default:
                            throw new BcpException.UnknownHeadByte();
                    }
                }
                catch (Exception e)
                {
                    exceptionHandler(e);
                }
            };
            try
            {
                stream.BeginRead(headBuffer, 0, 1, asyncCallback, readState);
            }
            catch (Exception e)
            {
                exceptionHandler(e);
            }
        }
Example #6
0
 public static void Write(Stream stream, Bcp.IPacket packet)
 {
     try
     {
         Action<Stream, Bcp.IPacket> writeCallback;
         var isSuccess = writeCallbacks.TryGetValue(packet.GetType(), out writeCallback);
         Debug.Assert(isSuccess);
         writeCallback(stream, packet);
     }
     catch
     {
         stream.Close();
     }
 }
Example #7
0
        private static void ReadAll(
            Stream stream,
            Bcp.ReadState readState,
            byte[] buffer,
            int offset,
            int count,
            ProcessReadAll processReadAll,
            BcpDelegate.ExceptionHandler exceptionHandler)
        {
            AsyncCallback asyncCallback = null;
            asyncCallback = asyncResult =>
            {
                try
                {
                    int numBytesRead = stream.EndRead(asyncResult);
                    if (numBytesRead == 0)
                    {
                        exceptionHandler(new EndOfStreamException());
                    }
                    else
                    {
                        offset += numBytesRead;

                        if (offset < count)
                        {
                            stream.BeginRead(buffer, offset, count, asyncCallback, readState);
                        }
                        else
                        {
                            processReadAll();
                        }
                    }
                }
                catch (Exception e)
                {
                    exceptionHandler(e);
                }
            };
            try
            {
                stream.BeginRead(buffer, offset, count, asyncCallback, readState);
            }
            catch (Exception e)
            {
                exceptionHandler(e);
            }
        }
Example #8
0
 public static void Write(Stream stream, Bcp.HeartBeat packet)
 {
     try
     {
         stream.WriteByte(Bcp.HeartBeat.HeadByte);
     }
     catch
     {
         stream.Close();
     }
 }
Example #9
0
 public static void Write(Stream stream, Bcp.ShutDown packet)
 {
     try
     {
         stream.WriteByte(Bcp.ShutDown.HeadByte);
     }
     catch
     {
         stream.Close();
     }
 }
Example #10
0
 public static void Write(Stream stream, Bcp.Data packet)
 {
     try
     {
         int packetCount = 0;
         foreach(var buffer in packet.Buffers)
         {
             packetCount += buffer.Count;
         }
         stream.WriteByte(Bcp.Data.HeadByte);
         WriteUnsignedVarint(stream, (uint)packetCount);
         foreach (var buffer in packet.Buffers)
         {
             stream.Write(buffer.Array, buffer.Offset, buffer.Count);
         }
     }
     catch
     {
         stream.Close();
     }
 }
Example #11
0
 public static void Write(Stream stream, Bcp.RetransmissionFinish packet)
 {
     try
     {
         stream.WriteByte(Bcp.RetransmissionFinish.HeadByte);
         WriteUnsignedVarint(stream, packet.ConnectionId);
         WriteUnsignedVarint(stream, packet.PackId);
     }
     catch
     {
         stream.Close();
     }
 }
Example #12
0
 public static void Write(Stream stream, Bcp.Finish packet)
 {
     try
     {
         stream.WriteByte(Bcp.Finish.HeadByte);
     }
     catch
     {
         stream.Close();
     }
 }