Exemple #1
1
        private static async Task RunSelectedConfigurationAsync(Transport transport,
            Protocol protocol, CancellationToken cancellationToken)
        {
            var fabric = new LoggerFactory();
            var handler = new CalculatorAsyncHandler();
            var processor = new Calculator.AsyncProcessor(handler);

            TServerTransport serverTransport = null;

            switch (transport)
            {
                case Transport.Tcp:
                    serverTransport = new TServerSocketTransport(9090);
                    break;
                case Transport.TcpBuffered:
                    serverTransport = new TServerSocketTransport(port: 9090, clientTimeout: 10000,
                        useBufferedSockets: true);
                    break;
                case Transport.NamedPipe:
                    serverTransport = new TNamedPipeServerTransport(".test");
                    break;
                case Transport.TcpTls:
                    serverTransport = new TTlsServerSocketTransport(9090, false, GetCertificate(),
                        ClientCertValidator, LocalCertificateSelectionCallback);
                    break;
            }

            ITProtocolFactory inputProtocolFactory;
            ITProtocolFactory outputProtocolFactory;

            switch (protocol)
            {
                case Protocol.Binary:
                {
                    inputProtocolFactory = new TBinaryProtocol.Factory();
                    outputProtocolFactory = new TBinaryProtocol.Factory();
                }
                    break;
                case Protocol.Compact:
                {
                    inputProtocolFactory = new TCompactProtocol.Factory();
                    outputProtocolFactory = new TCompactProtocol.Factory();
                }
                    break;
                case Protocol.Json:
                {
                    inputProtocolFactory = new TJsonProtocol.Factory();
                    outputProtocolFactory = new TJsonProtocol.Factory();
                }
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(protocol), protocol, null);
            }

            try
            {
                Logger.LogInformation(
                    $"Selected TAsyncServer with {serverTransport} transport and {inputProtocolFactory} protocol factories");

                var server = new AsyncBaseServer(processor, serverTransport, inputProtocolFactory,
                    outputProtocolFactory, fabric);

                Logger.LogInformation("Starting the server...");
                await server.ServeAsync(cancellationToken);
            }
            catch (Exception x)
            {
                Logger.LogInformation(x.ToString());
            }

            Logger.LogInformation("Server stopped.");
        }
Exemple #2
0
        static void Main(string[] args)
        {

            IProtocol protocol = new Protocol();
            MultipleThread thread = new MultipleThread(protocol);
            Server server = new Server(thread);
            server.Start();
        }
    private void UpdateDictionary(StreamWriter streamWriter, StreamReader streamReader, String nextLine, Server.Protocol protocol, string ipAddress)
    {
        //These variables are used to hold output stings
        string output         = string.Empty;
        string protocolStatus = "OK";

        switch (protocol)
        {
            #region WhoIs

        //This deals with whois server requests
        case Server.Protocol.whoIs:

            //This splits the input into a array containing the username and location
            string[] nextLineSectionsWhoIs = nextLine.Split(new char[] { ' ' }, 2);

            #region 1

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsWhoIs.Length == 1)
            {
                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(nextLine))
                {
                    //This adds the username to a string
                    string username = Server.dictionary[nextLine];

                    //This adds the username to a string
                    output = username + "\r\n";
                }
                else
                {
                    //This outputs an error if the dictionary does not contain the username
                    output = "ERROR: no entries found" + "\r\n";
                }
            }

            #endregion

            #region 2

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsWhoIs.Length == 2)
            {
                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(nextLineSectionsWhoIs[0]))
                {
                    //This adds the location to the dictionary
                    Server.dictionary[nextLineSectionsWhoIs[0]] = nextLineSectionsWhoIs[1];

                    //This an output to a string
                    output = "OK" + "\r\n";
                }
                else
                {
                    //This adds the username and location to the dictionary
                    Server.dictionary.Add(nextLineSectionsWhoIs[0], nextLineSectionsWhoIs[1]);

                    //This an output to a string
                    output = "OK" + "\r\n";
                }
            }

            #endregion

            //This breaks out of the case
            break;

            #endregion

            #region H09

        //This deals with whois server requests
        case Server.Protocol.h09:

            //This splits the input into a array containing the username and location
            string[] nextLineSectionsH09 = nextLine.Split(new char[] { ' ' }, 2);

            #region GET

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsH09[0] == "GET")
            {
                //This splits the input into a array containing the username and GET
                string nextLineLocation = nextLine.Split('/')[1];

                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(nextLineLocation))
                {
                    //This adds the username to a string
                    string username = Server.dictionary[nextLineLocation];

                    //This adds the username to a string
                    output = "HTTP/0.9 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n" + username + "\r\n";
                }
                else
                {
                    //This outputs an error if the dictionary does not contain the username
                    output = "HTTP/0.9 404 Not Found" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n";
                }
            }

            #endregion

            #region PUT

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsH09[0] == "PUT")
            {
                //This splits the input into a array containing the username and GET
                string username = nextLine.Split('/')[1];

                streamReader.ReadLine();

                //This adds the location to a string
                string locationOfUsername = streamReader.ReadLine();;

                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(username))
                {
                    //This adds the location to the dictionary
                    Server.dictionary[username] = locationOfUsername;

                    //This an output to a string
                    output = "HTTP/0.9 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n";
                }
                else
                {
                    //This adds the username and location to the dictionary
                    Server.dictionary.Add(username, locationOfUsername);

                    //This an output to a string
                    output = "HTTP/0.9 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n";
                }
            }

            #endregion

            //This breaks out of the case
            break;

            #endregion

            #region H10

        case Server.Protocol.h10:

            //This splits the input into a array containing the username and location
            string[] nextLineSectionsH10 = nextLine.Split(new char[] { ' ' }, 3);

            #region GET

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsH10[0] == "GET")
            {
                //This splits the input into a array containing the username and GET
                string nextLineLocation = nextLineSectionsH10[1].Split('/')[1];

                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(nextLineLocation))
                {
                    //This adds the username to a string
                    string username = Server.dictionary[nextLineLocation];

                    //This adds the username to a string
                    output = "HTTP/1.0 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n" + username + "\r\n";
                }
                else
                {
                    //This outputs an error if the dictionary does not contain the username
                    output = "HTTP/1.0 404 Not Found" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n";
                }
            }

            #endregion

            #region POST

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsH10[0] == "POST")
            {
                //This splits the input into a array containing the username and POST
                string username = nextLineSectionsH10[1].Split('/')[1];

                streamReader.ReadLine();
                streamReader.ReadLine();

                //This adds the location to a string
                string locationOfUsername = streamReader.ReadLine();

                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(username))
                {
                    //This adds the username and location to the dictionary
                    Server.dictionary[username] = locationOfUsername;

                    //This an output to a string
                    output = "HTTP/1.0 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n";
                }
                else
                {
                    //This adds the username and location to the dictionary
                    Server.dictionary.Add(username, locationOfUsername);

                    //This an output to a string
                    output = "HTTP/1.0 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n";
                }
            }

            #endregion

            //This breaks out of the case
            break;

            #endregion

            #region H11

        case Server.Protocol.h11:

            //This splits the input into a array containing the username and location
            string[] nextLineSectionsH11 = nextLine.Split(new char[] { ' ' }, 3);

            #region GET

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsH11[0] == "GET")
            {
                //This splits the input into a array containing the username and GET
                string nextLineLocation = nextLineSectionsH11[1].Split('/')[1];

                streamReader.ReadLine();

                //This adds the optional header lines to a string
                string optionalHeaderLines = streamReader.ReadLine();

                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(nextLineLocation))
                {
                    //This adds the username to a string
                    string username = Server.dictionary[nextLineLocation];

                    //This adds the username to a string
                    output = "HTTP/1.1 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n" + optionalHeaderLines + "\r\n" + username + "\r\n";
                }
                else
                {
                    //This outputs an error if the dictionary does not contain the username
                    output = "HTTP/1.1 404 Not Found" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n" + optionalHeaderLines + "\r\n";
                }
            }

            #endregion

            #region POST

            //This checks to see if the array contains both a username and location
            if (nextLineSectionsH11[0] == "POST")
            {
                //This splits the input into a array containing the username and POST
                string username = nextLineSectionsH11[1].Split('/')[1];

                streamReader.ReadLine();
                streamReader.ReadLine();

                //This adds the optional header lines to a string
                string OptionalHeaderLines = streamReader.ReadLine();

                //This adds the location to a string
                string locationOfUsername = streamReader.ReadLine();;

                //This checks to see if the dictionary already containes an instance of the given username
                if (Server.dictionary.ContainsKey(username))
                {
                    //This adds the username and location to the dictionary
                    Server.dictionary[username] = locationOfUsername;

                    //This an output to a string
                    output = "HTTP/1.1 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n" + OptionalHeaderLines + "\r\n";
                }
                else
                {
                    //This adds the username and location to the dictionary
                    Server.dictionary.Add(username, locationOfUsername);

                    //This an output to a string
                    output = "HTTP/1.1 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n\r\n" + OptionalHeaderLines + "\r\n";
                }
            }
            #endregion

            //This breaks out of the case
            break;

            #endregion

            #region Default

        default:

            protocolStatus = "UNKNOWN";

            break;

            #endregion
        }

        streamWriter.WriteLine(output);
        streamWriter.Close();

        //This updates the dictionary backup
        UpdateDictionaryBackup();

        //This runs the logging scripts
        logging.ToLog(output, ipAddress, protocolStatus);
    }
Exemple #4
0
 public RecieveResult Recieve(NetworkStream stream, out byte[] data)
 {
     return(Protocol.Recieve(stream, out data));
 }
Exemple #5
0
        private static async Task RunSelectedConfigurationAsync(Transport transport, Buffering buffering, Protocol protocol, bool multiplex, CancellationToken cancellationToken)
        {
            TServerTransport serverTransport = transport switch
            {
                Transport.Tcp => new TServerSocketTransport(9090, Configuration),
                Transport.NamedPipe => new TNamedPipeServerTransport(".test", Configuration, NamedPipeClientFlags.None),
                Transport.TcpTls => new TTlsServerSocketTransport(9090, Configuration, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback),
                _ => throw new ArgumentException("unsupported value $transport", nameof(transport)),
            };

            TTransportFactory transportFactory = buffering switch
            {
                Buffering.Buffered => new TBufferedTransport.Factory(),
                Buffering.Framed => new TFramedTransport.Factory(),
                // layered transport(s) are optional
                Buffering.None => null,
                _ => throw new ArgumentException("unsupported value $buffering", nameof(buffering)),
            };

            TProtocolFactory protocolFactory = protocol switch
            {
                Protocol.Binary => new TBinaryProtocol.Factory(),
                Protocol.Compact => new TCompactProtocol.Factory(),
                Protocol.Json => new TJsonProtocol.Factory(),
                _ => throw new ArgumentException("unsupported value $protocol", nameof(protocol)),
            };

            var handler = new CalculatorAsyncHandler();
            ITAsyncProcessor processor = new Calculator.AsyncProcessor(handler);

            if (multiplex)
            {
                var multiplexedProcessor = new TMultiplexedProcessor();
                multiplexedProcessor.RegisterProcessor(nameof(Calculator), processor);

                processor = multiplexedProcessor;
            }


            try
            {
                Logger.LogInformation(
                    "TSimpleAsyncServer with \n{transport} transport\n{buffering} buffering\nmultiplex = {multiplex}\n{protocol} protocol",
                    transport,
                    buffering,
                    multiplex ? "yes" : "no",
                    protocol
                    );

                var server = new TSimpleAsyncServer(
                    itProcessorFactory: new TSingletonProcessorFactory(processor),
                    serverTransport: serverTransport,
                    inputTransportFactory: transportFactory,
                    outputTransportFactory: transportFactory,
                    inputProtocolFactory: protocolFactory,
                    outputProtocolFactory: protocolFactory,
                    logger: LoggingHelper.CreateLogger <TSimpleAsyncServer>());

                Logger.LogInformation("Starting the server...");

                await server.ServeAsync(cancellationToken);
            }
            catch (Exception x)
            {
                Logger.LogInformation("{x}", x);
            }
        }
Exemple #6
0
        private static async Task RunSelectedConfigurationAsync(Transport transport, Buffering buffering, Protocol protocol, CancellationToken cancellationToken)
        {
            var handler = new CalculatorAsyncHandler();

            TServerTransport serverTransport = null;

            switch (transport)
            {
            case Transport.Tcp:
                serverTransport = new TServerSocketTransport(9090, Configuration);
                break;

            case Transport.NamedPipe:
                serverTransport = new TNamedPipeServerTransport(".test", Configuration);
                break;

            case Transport.TcpTls:
                serverTransport = new TTlsServerSocketTransport(9090, Configuration,
                                                                GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback);
                break;
            }

            TTransportFactory inputTransportFactory  = null;
            TTransportFactory outputTransportFactory = null;

            switch (buffering)
            {
            case Buffering.Buffered:
                inputTransportFactory  = new TBufferedTransport.Factory();
                outputTransportFactory = new TBufferedTransport.Factory();
                break;

            case Buffering.Framed:
                inputTransportFactory  = new TFramedTransport.Factory();
                outputTransportFactory = new TFramedTransport.Factory();
                break;

            default:     // layered transport(s) are optional
                Debug.Assert(buffering == Buffering.None, "unhandled case");
                break;
            }

            TProtocolFactory inputProtocolFactory  = null;
            TProtocolFactory outputProtocolFactory = null;
            ITAsyncProcessor processor             = null;

            switch (protocol)
            {
            case Protocol.Binary:
                inputProtocolFactory  = new TBinaryProtocol.Factory();
                outputProtocolFactory = new TBinaryProtocol.Factory();
                processor             = new Calculator.AsyncProcessor(handler);
                break;

            case Protocol.Compact:
                inputProtocolFactory  = new TCompactProtocol.Factory();
                outputProtocolFactory = new TCompactProtocol.Factory();
                processor             = new Calculator.AsyncProcessor(handler);
                break;

            case Protocol.Json:
                inputProtocolFactory  = new TJsonProtocol.Factory();
                outputProtocolFactory = new TJsonProtocol.Factory();
                processor             = new Calculator.AsyncProcessor(handler);
                break;

            case Protocol.Multiplexed:
                inputProtocolFactory  = new TBinaryProtocol.Factory();
                outputProtocolFactory = new TBinaryProtocol.Factory();

                var calcHandler   = new CalculatorAsyncHandler();
                var calcProcessor = new Calculator.AsyncProcessor(calcHandler);

                var sharedServiceHandler   = new SharedServiceAsyncHandler();
                var sharedServiceProcessor = new SharedService.AsyncProcessor(sharedServiceHandler);

                var multiplexedProcessor = new TMultiplexedProcessor();
                multiplexedProcessor.RegisterProcessor(nameof(Calculator), calcProcessor);
                multiplexedProcessor.RegisterProcessor(nameof(SharedService), sharedServiceProcessor);

                processor = multiplexedProcessor;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(protocol), protocol, null);
            }


            try
            {
                Logger.LogInformation(
                    $"Selected TAsyncServer with {serverTransport} transport, {processor} processor and {inputProtocolFactory} protocol factories");

                var loggerFactory = ServiceCollection.BuildServiceProvider().GetService <ILoggerFactory>();

                var server = new TSimpleAsyncServer(
                    itProcessorFactory: new TSingletonProcessorFactory(processor),
                    serverTransport: serverTransport,
                    inputTransportFactory: inputTransportFactory,
                    outputTransportFactory: outputTransportFactory,
                    inputProtocolFactory: inputProtocolFactory,
                    outputProtocolFactory: outputProtocolFactory,
                    logger: loggerFactory.CreateLogger <TSimpleAsyncServer>());

                Logger.LogInformation("Starting the server...");

                await server.ServeAsync(cancellationToken);
            }
            catch (Exception x)
            {
                Logger.LogInformation(x.ToString());
            }
        }