Using Dotnetty to implement a Redis console application

Source: Internet
Author: User
Tags redis server
0: Demo run out of the results explained


The left-hand Blue command-line interface in the figure is linked with the Windows PowerShell command line.



1. Open the PowerShell command line interface and enter the command "telnet 127.0.0.1 6379".



If you do not have PowerShell, you can also use the CMD command line interface to test the effect of the Redis command.



Enter the ping command, and Redis receives it, which returns a Pong string. The purpose of the command is usually to test whether the connection to the server is still in effect. ping command



Entering the info command, Redis returns a large string of information on the Redis server. This command, which is used primarily to test the unpacking situation, describes how the unpacking is handled.



The command-line interface on the right-hand side of the diagram is the console application that the demo ran out of.



Two results A comparison, test out, our demo has got the correct results.



Ok, let's get into the play.


What's a dotnetty?


Dotnetty is a C # version of Netty.



Netty is a Java open source framework provided by JBoss. Netty provides asynchronous, event-driven network application frameworks and tools for rapid development of high-performance, high-reliability network servers and client programs. "Excerpt from Baidu Encyclopedia"



The author thinks that Netty is an important component of the Java Ecological Circle.



Native socket programming, high learning costs, using the original socket to do the project, that is, driving a green leather train, moving times dozen times ....



Using Netty to do projects, the development efficiency is undoubtedly a high-speed rail-like existence.



And it's hard to use native socket programming.





Two, write the cause of this demo


Study Dotnetty for a long time. Version 0.4 from Dotnetty. To the current version of 0.48. Implement a C/s side of your own example. There is no such thing as a good idea to achieve.



Today saw Haifeiwu "Netty source code in the implementation of the Redis protocol, and then want to follow the realization of A."



So, today's demo.



Yes, it's just a demo. does not replace Stackexchange.redis.


Third, learn about the Redis protocol


RESP is shorthand for the Redis serialization protocol. It is an intuitive text protocol that has the advantage of being very simple to implement and excellent in parsing performance.



The Redis protocol divides the transferred structure data into 5 minimum cell types, with the end of the unit being uniformly coupled with a carriage return line symbol \ r \ n, to indicate the end of the cell.



The single-line string begins with A + sign.



A multiline string begins with a $ symbol followed by a string length.



Integer values begin with the: symbol, followed by the string form of an integer.



The error message begins with the-symbol.



The array begins with a * number followed by the length of the array.



Specific introduction to the RESP protocol interested partners please step into the Haifeiwu another article Redis protocol specification (translated)



The above 2nd is the introduction from Haifeiwu


Four Demo code


1, defining enumeration Redismessagetype





 
1 internal enum RedisMessageType:byte
  2     {
  3 /// <summary>
  4 /// Single-line string starting with +
  5 /// </summary>
  6 SimpleString = 43,
  7
  8 /// <summary>
  9 /// Error message starting with -
10 /// </summary>
11 Error = 45,
12 /// <summary>
13 /// Integer data starting with : INTEGER
14 /// </summary>
15 Integer = 58,
16 /// <summary>
17 /// Multi-line string starting with $
18 /// </summary>
19 BulkString = 36,
20
21 /// <summary>
22 /// Array starting with *
23 /// </summary>
24 ArrayHeader = 42
25 }
View Code





2, define the Redisobject and define the virtual method WriteBuffer


1 public class RedisObject
 2     {
 3 public virtual void WriteBuffer(IByteBuffer output)
 4 {
 5 }
 6 }
 7
 8 public class RedisCommon : RedisObject
 9     {
10 public RedisCommon()
11 {
12 Commond = new List<string>();
13 }
14 public List<string> Commond { get; set; }
15 public override void WriteBuffer(IByteBuffer output)
16 {
17 //Request header format, *<number of arguments>\r\n
18 //const string headstr = "*{0}\r\n";
19 //Parameter information $<number of bytes of argument N>\r\n<argument data>\r\n
20 //const string bulkstr = "${0}\r\n{1}\r\n";
21 StringBuilder stringBuilder = new StringBuilder();
22 stringBuilder.AppendFormat("*{0}\r\n",Commond.Count);
23 foreach (var item in Commond)
twenty four             {
25 stringBuilder.AppendFormat("${0}\r\n{1}\r\n", item.Length, item);
26 }
27 //*1\r\n$4\r\nPING\r\n
28 byte[] bytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());
29 output.WriteBytes(bytes);
30 }
31 } 
View Code





3, defines the Redisencoder encoder, which integrates the Messagetobyteencoder<t> method. The main is to Redisobject, write to Ibytebuffer inside.


public class RedisEncoder:DotNetty.Codecs.MessageToByteEncoder<RedisObject>
    {
        protected override void Encode(IChannelHandlerContext context, RedisObject message, IByteBuffer output)
        {
            message.WriteBuffer(output);
            //context.WriteAndFlushAsync(output);
        }
    }





4, defines the Redisdecoder decoder, which inherits the Bytetomessagedecoder.



Bytetomessagedecoder is the need to implement their own solution to the sticky bag, unpacking. Relatively low level, but flexible.



Dotnetty also has other high-level decoders.



Like Messagetomessagedecoder, Datagrampacketdecoder,lengthfieldbasedframedecoder,linebasedframedecoder, Replayingdecoder,delimiterbasedframedecoder,stringdecoder.



In Li Linfeng Teacher's "Netty authoritative guide" a book, can learn.



Passing the test, we learned that the info command returned a multi-line string



Begins with the $ sign followed by the string length. Assuming that the Redis server is returning a multiline string, its return format is: ${string length}\r\n{string}\r\n



The code for parsing a multiline string is





Private string ReadMultiLine(IByteBuffer input)
        {
            Int64 strLength = ReadInteger(input);
            Int64 packLength = input.ReaderIndex + strLength + 2;
            //The length of the package is larger than the actual package. Skip him to prevent stacking.
            If ( input.WriterIndex> packLength)
            {
                input.SkipBytes(input.ReadableBytes);
            }
            If (strLength == -1)
            {
                Return null;
            }
            / / The length of the package is smaller than the actual package
            If (packLength > input.WriterIndex)
            {
                Throw new Exception("");
            }
            Int count = 0;
            Int whildCount = 0;
            StringBuilder stringBuilder = new StringBuilder();
            While (input.IsReadable())
            {
                String str= this.ReadString(input);
                Count += str.Length;
                stringBuilder.AppendLine(str);
                whildCount++;
            }

Return stringBuilder.ToString();
        }





6. Defining Redishandle Handler, he inherited the Simplechannelinboundhandler method. The Redisobject object to be solved after receiving the decoder.


public class RedisHandle : SimpleChannelInboundHandler<RedisObject>
    {
        protected override void ChannelRead0(IChannelHandlerContext ctx, RedisObject msg)
        {
            if (msg is ReidsString)
            {
                ReidsString reidsString = (ReidsString)msg;
                Console.WriteLine(reidsString.Content);
            }
        }
    }



Conclusion: Attach Source Address



Gitee.com/hesson/dotnetty.redis.demo



Thank you for the review of this article, and suggest changes


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.