about summary refs log blame commit diff
path: root/Examples/Tokens/Client.cs
blob: 5ad0cf04071cc34bf470ea89534853f1f0407569 (plain) (tree)
1
2
3
4
5
6
7
8
9
                
                         
                       
                
 
                      


                       
                                       

                                                  
                                        




                                             

                                                                                                  




                                               
                                                          
 

                                                                       



                                                            
 
                                       


                                                          
                                    


                          

                                                                 






                                                            
                                                                   

                                   
                                                                 

                                       
                                                                                     









                                                    
                                                    
                                                                                    


         
using System;
using System.Net.Sockets;
using System.Threading;
using IRCTokens;

namespace TokensSample
{
    public class Client
    {
        private readonly byte[] _bytes;
        private readonly StatefulDecoder _decoder;
        private readonly StatefulEncoder _encoder;
        private readonly Socket _socket;

        public Client()
        {
            _decoder = new StatefulDecoder();
            _encoder = new StatefulEncoder();
            _socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            _bytes   = new byte[1024];
        }

        public void Start()
        {
            _socket.Connect("127.0.0.1", 6667);
            while (!_socket.Connected) Thread.Sleep(1000);

            Send(new Line("NICK", "tokensbot"));
            Send(new Line("USER", "tokensbot", "0", "*", "real name"));

            while (true)
            {
                var bytesReceived = _socket.Receive(_bytes);

                if (bytesReceived == 0)
                {
                    Console.WriteLine("! disconnected");
                    _socket.Shutdown(SocketShutdown.Both);
                    _socket.Close();
                    break;
                }

                var lines = _decoder.Push(_bytes, bytesReceived);

                foreach (var line in lines)
                {
                    Console.WriteLine($"< {line.Format()}");

                    switch (line.Command)
                    {
                        case "PING":
                            Send(new Line("PONG", line.Params[0]));
                            break;
                        case "001":
                            Send(new Line("JOIN", "#irctokens"));
                            break;
                        case "PRIVMSG":
                            Send(new Line("PRIVMSG", line.Params[0], "hello there"));
                            break;
                    }
                }
            }
        }

        private void Send(Line line)
        {
            Console.WriteLine($"> {line.Format()}");
            _encoder.Push(line);
            while (_encoder.PendingBytes.Length > 0)
                _encoder.Pop(_socket.Send(_encoder.PendingBytes, SocketFlags.None));
        }
    }
}