IRC parsing, tokenization, and state handling in C#

add example to README.md

+86
+86
README.md
··· 3 3 this is a c\# port of jesopo's [irctokens]( 4 4 https://github.com/jesopo/irctokens) 5 5 6 + ## usage 7 + 8 + ### tokenization 9 + 10 + using IrcTokens; 11 + 12 + ... 13 + 14 + var line = new Line("@id=123 :ben!~ben@host.tld PRIVMSG #channel :hello there!"); 15 + Console.WriteLine(line); 16 + Console.WriteLine(line.Format()); 17 + 18 + ### formatting 19 + 20 + var line = new Line {Command = "USER", Params = new List<string> {"user", "0", "*", "real name"}}; 21 + Console.WriteLine(line); 22 + Console.WriteLine(line.Format()); 23 + 24 + ### stateful 25 + 26 + see the full example in [Sample/Client.cs](Sample/Client.cs) 27 + 28 + public class Client 29 + { 30 + private readonly Socket _socket; 31 + private readonly StatefulDecoder _decoder; 32 + private readonly StatefulEncoder _encoder; 33 + private readonly byte[] _bytes; 34 + 35 + public Client() 36 + { 37 + _decoder = new StatefulDecoder(); 38 + _encoder = new StatefulEncoder(); 39 + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 40 + _bytes = new byte[1024]; 41 + } 42 + 43 + public void Start() 44 + { 45 + _socket.Connect("127.0.0.1", 6667); 46 + 47 + Send(new Line {Command = "USER", Params = new List<string> {"username", "0", "*", "real name"}}); 48 + Send(new Line {Command = "NICK", Params = new List<string> {"statefulbot"}}); 49 + 50 + while (true) 51 + { 52 + var bytesReceived = _socket.Receive(_bytes); 53 + var lines = _decoder.Push(_bytes); 54 + 55 + if (lines.Count == 0) 56 + { 57 + Console.WriteLine("! disconnected"); 58 + _socket.Shutdown(SocketShutdown.Both); 59 + break; 60 + } 61 + 62 + foreach (var line in lines) 63 + { 64 + Console.WriteLine($"< {line.Format()}"); 65 + 66 + switch (line.Command) 67 + { 68 + case "PING": 69 + Send(new Line {Command = "PONG", Params = line.Params}); 70 + break; 71 + case "001": 72 + Send(new Line {Command = "JOIN", Params = new List<string> {"#channel"}}); 73 + break; 74 + } 75 + } 76 + } 77 + } 78 + 79 + private void Send(Line line) 80 + { 81 + Console.WriteLine($"> {line.Format()}"); 82 + _encoder.Push(line); 83 + while (_encoder.PendingBytes.Length > 0) 84 + _encoder.Pop(_socket.Send(_encoder.PendingBytes)); 85 + } 86 + } 87 + 88 + ## contact 89 + 90 + come say hi on [\#irctokens on irc.tilde.chat](https://web.tilde.chat/?join=irctokens) 91 +