Exemplo n.º 1
0
        static void Main(string[] args)
        {
            encoding = Encoding.ASCII;
            SocketServer server;
            server = new SocketServer(new IPEndPoint(IPAddress.Loopback, 0));

            server.Connected += c =>
                {
                    serverClient = new FramedClient(c);

                    // When received is called, bs will contain no more and no less
                    // data than whole packet as sent from client.
                    serverClient.Received += bs =>
                        {
                            var msg = encoding.GetString(bs.Array, bs.Offset, bs.Count);
                            msg = "Hello, " + msg + "!";
                            serverClient.SendPacket(encoding.GetBytes(msg));
                        };
                };

            server.Start();
            HandleClient(server.BindEndPoint.Port);
            
            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();

            server.Stop();
            client.Close();
            serverClient.Close();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            encoding = Encoding.ASCII;
            SocketServer server;

            server = new SocketServer(new IPEndPoint(IPAddress.Loopback, 0));

            server.Connected.Subscribe(c =>
            {
                serverClient = new FramedClient(c);

                // When received is called, bs will contain no more and no less
                // data than whole packet as sent from client.
                serverClient.Received.Subscribe(bs =>
                {
                    var msg = encoding.GetString(bs.Array, bs.Offset, bs.Count);
                    msg     = "Hello, " + msg + "!";
                    serverClient.SendPacket(encoding.GetBytes(msg));
                });
            });

            server.Start();
            HandleClient(server.BindEndPoint.Port);

            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();

            server.Stop();
            client.Close();
            serverClient.Close();
        }
Exemplo n.º 3
0
        static async void HandleClient(int serverPort)
        {
            client = new FramedClient(
                            new SocketClient());

            client.Received += bs =>
                {
                    Console.WriteLine("Received: " +
                        encoding.GetString(bs.Array, bs.Offset, bs.Count));
                };
            
            await client.Connect(new IPEndPoint(IPAddress.Loopback, serverPort));
            client.SendPacket(encoding.GetBytes("Steve"));
        }
Exemplo n.º 4
0
        static async void HandleClient(int serverPort)
        {
            client = new FramedClient(
                new SocketClient());

            client.Received.Subscribe(bs =>
            {
                Console.WriteLine("Received: " +
                                  encoding.GetString(bs.Array, bs.Offset, bs.Count));
            });

            await client.Connect(new IPEndPoint(IPAddress.Loopback, serverPort));

            client.SendPacket(encoding.GetBytes("Steve"));
        }
Exemplo n.º 5
0
        private static void Measure(int bufSize, int packets)
        {
            var  buffer           = DataHelpers.CreateRandomBuffer(bufSize);
            long l                = packets;
            long totalRecv        = 0;
            long totalPacketsRecv = 0;
            var  received         = new ManualResetEventSlim();

            GC.Collect();
            Console.WriteLine("Gen 0: " + GC.CollectionCount(0) +
                              ", Gen 1: " + GC.CollectionCount(1) + ", Gen 2: " +
                              GC.CollectionCount(2));
            var sw = Stopwatch.StartNew();

            Action <ArraySegment <byte> > recv = bs =>
            {
                totalRecv += bs.Count;
                ++totalPacketsRecv;
                if (totalRecv == l * bufSize)
                {
                    received.Set();
                }
            };
            Action <int> sent = (t) => Console.WriteLine("Sent ");

            fc1.Sent.Subscribe(sent);
            var recvSub = fc2.Received.Subscribe(recv);

            for (int i = 0; i < l; ++i)
            {
                fc1.SendPacket(buffer);
            }

            received.Wait();

            recvSub.Dispose();

            var elapsed = sw.Elapsed.TotalSeconds;

            GC.Collect();
            Console.WriteLine("Gen 0: " + GC.CollectionCount(0) +
                              ", Gen 1: " + GC.CollectionCount(1) + ", Gen 2: " +
                              GC.CollectionCount(2));

            Console.WriteLine("Elapsed s: " + elapsed);
            Console.WriteLine("Rate: " + (double)totalRecv * 8 / elapsed / 1024 / 1024 + " Mb/sec");
            Console.WriteLine("Sent {0} packets. Received: {1}", packets, totalPacketsRecv);
        }
Exemplo n.º 6
0
        private void AuxSendProtocolPacket <T>(int packetId, T packet)
        {
            using (var ms = new MemoryStream())
            {
                ms.SetLength(8);
                ms.Position = 8;

                packetSerializer.Serialize(packet, ms);

                ms.Position = 0;

                var buffer = ms.GetBuffer();
                unsafe
                {
                    fixed(byte *b = buffer)
                    {
                        *(Proto.ActorProtocolFlags *)b = Proto.ActorProtocolFlags.StacksProtocol;
                        *(int *)(b + 4) = packetId;
                    }
                }

                framedClient.SendPacket(new ArraySegment <byte>(buffer, 0, (int)ms.Length));
            }
        }