Exemplo n.º 1
0
        public void SendAsync(byte[] bytes)
        {
            if (IsClient)
            {
                var block     = BufferPool.Pop();
                var writeArgs = SocketPool.Pop();

                // TODO: loop in case byte[] is greater than block capacity!!!
                var writeSize = (bytes.Length < block.Capacity) ? bytes.Length : block.Capacity;
                System.Buffer.BlockCopy(bytes, 0, block.Bytes, block.Offset, writeSize);
                ((MiniNet.Buffers.Buffer)block).Size = writeSize;

                writeArgs.UserToken    = block;
                writeArgs.AcceptSocket = BaseSocket;
                writeArgs.SetBuffer(block.Bytes, block.Offset, block.Size);
                writeArgs.Completed += OnSocketCompleted;

                var willRaiseEvent = BaseSocket.SendAsync(writeArgs);
                if (!willRaiseEvent)
                {
                    SendHandle(writeArgs);
                }
            }
            else
            {
                RaiseFault("Only accepted client sockets may read and write.");
            }
        }
Exemplo n.º 2
0
        private static async Task Handle()
        {
            using var client = new Socket(SocketType.Stream, ProtocolType.Tcp);

            while (true)
            {
                try {
                    await BaseSocket.ConnectAsync(client, Listener.IpAddress, 11000);

                    var sentBytes = await BaseSocket.SendAsync(client, "Message", Encoding.UTF8);

                    System.Console.WriteLine($"SentBytes:{sentBytes}");

                    var receiveMessage = await BaseSocket.ReceiveAsync(client, Encoding.UTF8);

                    System.Console.WriteLine($"receiveMessage:{receiveMessage}");
                } finally {
                    client.Shutdown(SocketShutdown.Both);
                    client.Close();
                }
                System.Console.WriteLine();
            }
        }
Exemplo n.º 3
0
 protected override void MainHandler(Socket handler, string data)
 {
     BaseSocket.SendAsync(handler, data, Encoding.UTF8).Wait();
 }