public ProtobufRequestInfo <TMessage, TBuilder> Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
        {
            rest = 0;
            var readOffset = offset - m_OffsetDelta;

            CodedInputStream stream = CodedInputStream.CreateInstance(readBuffer, readOffset, length);
            var varint32            = (int)stream.ReadRawVarint32();

            if (varint32 <= 0)
            {
                return(null);
            }

            var headLen = (int)stream.Position - readOffset;

            rest = length - varint32 - headLen + m_ParsedLength;

            if (rest >= 0)
            {
                byte[] body = stream.ReadRawBytes(varint32);

                TMessage message     = CreateBuilder().MergeFrom(body).BuildParsed();
                var      requestInfo = new ProtobufRequestInfo <TMessage, TBuilder>(message);
                InternalReset();
                return(requestInfo);
            }
            else
            {
                m_ParsedLength += length;
                m_OffsetDelta   = m_ParsedLength;
                rest            = 0;

                var expectedOffset = offset + length;
                var newOffset      = m_OrigOffset + m_OffsetDelta;

                if (newOffset < expectedOffset)
                {
                    Buffer.BlockCopy(readBuffer, offset - m_ParsedLength + length, readBuffer, m_OrigOffset, m_ParsedLength);
                }

                return(null);
            }
        }
예제 #2
0
        private static void AppServerOnNewRequestReceived(ProtobufAppSession session, ProtobufRequestInfo requestInfo)
        {
            switch (requestInfo.Type)
            {
            case DefeatMessage.Types.Type.BackMessage:
                Console.WriteLine("BackMessage:{0}", requestInfo.Body.BackMessage.Content);
                break;

            case DefeatMessage.Types.Type.CallMessage:
                Console.WriteLine("CallMessage:{0}", requestInfo.Body.CallMessage.Content);

                var backMessage = BackMessage.CreateBuilder()
                                  .SetContent("Hello I am form C# server by SuperSocket").Build();
                var message = DefeatMessage.CreateBuilder()
                              .SetType(DefeatMessage.Types.Type.BackMessage)
                              .SetBackMessage(backMessage).Build();

                using (var stream = new MemoryStream())
                {
                    CodedOutputStream os = CodedOutputStream.CreateInstance(stream);

                    os.WriteMessageNoTag(message);

                    os.Flush();

                    byte[] data = stream.ToArray();
                    session.Send(new ArraySegment <byte>(data));
                }


                break;
            }
        }