コード例 #1
0
        public void Send_message_SendsMessage()
        {
            // Establish the local endpoint for the socket.
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = ipHostInfo.AddressList[0];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, 20055);

            // Create a server
            Socket server = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            server.Bind(localEndPoint);
            server.Listen(2);

            // Create a SocketConnectionController
            SocketConnection socket = new SocketConnection(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(localEndPoint);

            SocketConnectionController <MessageType> socketConnectionController = new SocketConnectionController <MessageType>(socket, 1024);

            socketConnectionController.Start();

            Socket server_receiver = server.Accept();

            Thread.Sleep(1000);
            byte[] message      = Encoding.ASCII.GetBytes("ThisIsAMessage");
            byte[] expectedData = PacketProtocol <MessageType> .WrapMessage(MessageType.Standard, message);

            socketConnectionController.Send(MessageType.Standard, message);
            byte[] receiveBuffer = new byte[expectedData.Length];
            server_receiver.Receive(receiveBuffer);
            Assert.AreEqual(expectedData, receiveBuffer);
        }
コード例 #2
0
        public void Send_Message_CallsBeginSend()
        {
            MessageType messageType = MessageType.Init;

            byte[] message = Encoding.ASCII.GetBytes("test message");
            var    mock    = new Mock <ISocketConnection>();

            mock.Setup(x => x.Connected).Returns(true);

            byte[] expectedMessage = PacketProtocol <MessageType> .WrapMessage(messageType, message);

            byte[] receivedMessage = null;
            mock.Setup(x => x.BeginSend(
                           It.IsAny <byte[]>(),
                           It.IsAny <int>(),
                           It.IsAny <int>(),
                           It.IsAny <SocketFlags>(),
                           It.IsAny <AsyncCallback>(),
                           It.IsAny <object>()))
            .Callback <byte[], int, int, SocketFlags, AsyncCallback, object>((data, offset, size, socketFlags, asyncCallback, state) =>
            {
                receivedMessage = data;
            });

            SocketConnectionController <MessageType> controller = new SocketConnectionController <MessageType>(mock.Object, 1024);

            controller.Start();
            controller.Send(messageType, message);
            Assert.AreEqual(expectedMessage, receivedMessage);
        }
コード例 #3
0
        public void ReceiveCallback_Message_InvokesMessageArrived()
        {
            // The receive callback is private. We have to chain the receive via the start function.
            MessageType messageType = MessageType.Init;

            byte[] message = Encoding.ASCII.GetBytes("test message");
            var    mock    = new Mock <ISocketConnection>();

            mock.Setup(x => x.Connected).Returns(true);

            byte[] dataToSend = PacketProtocol <MessageType> .WrapMessage(messageType, message);


            var asyncStateMock = new Mock <IAsyncResult>();

            asyncStateMock.Setup(x => x.AsyncState).Returns(dataToSend);

            bool beginReceiveHasBeenInvoked = false;

            mock.Setup(x => x.BeginReceive(
                           It.IsAny <byte[]>(),
                           It.IsAny <int>(),
                           It.IsAny <int>(),
                           It.IsAny <SocketFlags>(),
                           It.IsAny <AsyncCallback>(),
                           It.IsAny <object>()))
            .Callback <byte[], int, int, SocketFlags, AsyncCallback, object>((data, offset, size, socketFlags, asyncCallback, state) =>
            {
                if (!beginReceiveHasBeenInvoked)
                {
                    beginReceiveHasBeenInvoked = true;           // Prevent endless loop
                    asyncCallback.Invoke(asyncStateMock.Object); // calls ReceiveCallBack
                }
            });

            bool endSendCalled = false;

            mock.Setup(x => x.EndReceive(It.IsAny <IAsyncResult>())).Returns(dataToSend.Length).Callback(() => endSendCalled = true);

            SocketConnectionController <MessageType> controller = new SocketConnectionController <MessageType>(mock.Object, 1024);
            MessageReceivedEventArgs <MessageType>   messageReceivedEventArgs = null;

            controller.MessageReceived += (sender, args) => { messageReceivedEventArgs = args; };
            controller.Start();
            Assert.IsTrue(endSendCalled);
            Assert.AreEqual(messageType, messageReceivedEventArgs.MessageType);
            Assert.AreEqual(message, messageReceivedEventArgs.Message);
        }
コード例 #4
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                // Complete the connection.
                ISocketConnection socket = (ISocketConnection)ar.AsyncState;
                socket.EndConnect(ar);

                Console.WriteLine("Connected with StellaServer");
                _socketConnectionController = new SocketConnectionController <MessageType>(socket, TCP_BUFFER_SIZE);
                _socketConnectionController.MessageReceived += OnMessageReceived;
                _socketConnectionController.Disconnect      += OnDisconnect;
                _socketConnectionController.Start();

                // Send init
                SendInit();
            }
            catch (SocketException e)
            {
                Console.WriteLine($"Failed to open connection with StellaServer.{e.SocketErrorCode}.");
                Reconnect();
            }
        }
コード例 #5
0
        public void SendCallback_SocketException_InvokesOnDisconnect()
        {
            MessageType messageType = MessageType.Init;

            byte[] message         = Encoding.ASCII.GetBytes("test message");
            byte[] expectedMessage = PacketProtocol <MessageType> .WrapMessage(messageType, message);

            var mock = new Mock <ISocketConnection>();

            mock.Setup(x => x.Connected).Returns(true);

            // Chain the BeginSend to the EndSend SendCallBack function
            mock.Setup(x => x.BeginSend(
                           It.IsAny <byte[]>(),
                           It.IsAny <int>(),
                           It.IsAny <int>(),
                           It.IsAny <SocketFlags>(),
                           It.IsAny <AsyncCallback>(),
                           It.IsAny <object>()))
            .Callback <byte[], int, int, SocketFlags, AsyncCallback, object>((data, offset, size, socketFlags, asyncCallback, state) =>
            {
                asyncCallback.Invoke(null);
            });

            mock.Setup(x => x.EndSend(It.IsAny <IAsyncResult>())).Throws <SocketException>();
            // Listen to the Disconnect event
            bool disconnectInvoked = false;
            SocketConnectionController <MessageType> controller = new SocketConnectionController <MessageType>(mock.Object, 1024);

            controller.Disconnect += (sender, args) => disconnectInvoked = true;

            // Run
            controller.Start();
            controller.Send(messageType, message);
            Assert.IsTrue(disconnectInvoked);
        }