Esempio n. 1
0
        void CreateServer()
        {
            // Create the server
            var server = new SimplSocketServer();

            // Start listening for client connections on loopback end poiny
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));

            // Wait until a new client has connected
            var connectedClient = server.WaitForNewClient();

            // Create a byte array to send
            var arrayToSend1 = new byte[1000];

            // Send it
            Console.WriteLine($"Server is going to send message of {arrayToSend1.Length} bytes");
            server.Send(arrayToSend1, connectedClient);


            // Get a byte array from the memory pool
            var arrayToSend2 = PooledMessage.Rent(1000);

            Console.WriteLine($"Server is going to send a pooled message of {arrayToSend1.Length} bytes");
            server.Send(arrayToSend2, connectedClient);

            // Return message to pool
            arrayToSend2.ReturnAfterSend();
        }
Esempio n. 2
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="socketFunc">The function that creates a new socket. Use this to specify your socket constructor and initialize settings.</param>
 /// <param name="keepAlive">Whether or not to send keep-alive packets and detect if alive</param>
 /// <param name="messageBufferSize">The message buffer size to use for send/receive.</param>
 /// <param name="communicationTimeout">The communication timeout, in milliseconds.</param>
 /// <param name="maxMessageSize">The maximum message size.</param>
 /// <param name="useNagleAlgorithm">Whether or not to use the Nagle algorithm.</param>
 public SimplMessageServer(Func <Socket> socketFunc, bool keepAlive = true, int messageBufferSize = 65536,
                           int communicationTimeout = 10000, int maxMessageSize = 10 * 1024 * 1024, bool useNagleAlgorithm = false)
 {
     _simplSocketServer = new SimplSocketServer(socketFunc, messageBufferSize, communicationTimeout,
                                                maxMessageSize, useNagleAlgorithm);
     Initialize();
 }
Esempio n. 3
0
        static async Task RunViaSockets()
        {
            using (var server = new SimplSocketServer(
                       () => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                NoDelay = true
            }))
            {
                server.MessageReceived += (s, e) =>
                {
                    var blob = e.ReceivedMessage.Message;
                    ReverseServer.Reverse(blob);
                    server.Reply(blob, e.ReceivedMessage);
                };

                server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
                await Console.Out.WriteLineAsync(
                    "Server running; type 'q' to exit, anything else to broadcast");

                string line;
                while ((line = await Console.In.ReadLineAsync()) != null)
                {
                    if (line == "q")
                    {
                        break;
                    }

                    var blob = Encoding.UTF8.GetBytes(line);
                    server.Broadcast(blob);

                    await Console.Out.WriteLineAsync(
                        $"Broadcast {blob.Length} bytes to {server.CurrentlyConnectedClientCount} clients");
                }
            }
        }
Esempio n. 4
0
        public void Setup()
        {
            var socketServer = new SimplSocketServer(CreateSocket);

            socketServer.Listen(s1);
            _socketServer = socketServer;
            var pipeServer = SimplPipelineSocketServer.For <ReverseServer>();

            pipeServer.Listen(s2);
            _pipeServer = pipeServer;

            _data = new byte[1024];
        }
Esempio n. 5
0
        void CreateServer()
        {
            // Create the server
            var server = new SimplSocketServer();

            // Create a callback for received array
            server.MessageReceived += (s, e) =>
            {
                // Get the message
                var receivedMessage = e.ReceivedMessage;
                Console.WriteLine($"Server received message of {receivedMessage.Length} bytes");

                // Return the message to the pool
                receivedMessage.Return();
            };
            // Start listening for client connections on loopback endpoint
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
        }
Esempio n. 6
0
        void CreateServer()
        {
            // Create the server
            var server = new SimplSocketServer();

            // Create a callback for received array
            server.MessageReceived += (s, e) =>
            {
                // Get the message
                var receivedMessage = e.ReceivedMessage;
                Console.WriteLine($"Server received message of {receivedMessage.Length} bytes");

                // Reply to the message with the same message (echo)
                server.Reply(receivedMessage, receivedMessage);
                Console.WriteLine($"Server replied to message");


                // We cannot not dispose the received message directly, since it may still need to be send
                // Instead we use the ReturnAfterSend, which will return the message to the pool after sending
                receivedMessage.ReturnAfterSend();
            };
            // Start listening for client connections on loopback endpoint
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
        }