コード例 #1
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);
        }
コード例 #2
0
 public void Send(MessageType type, byte[] message)
 {
     try
     {
         if (_socketConnectionController.IsConnected)
         {
             _socketConnectionController.Send(type, message);
         }
         else
         {
             Console.Out.WriteLine($"Failed to send message of type {type}. Not connected with server."); // TODO keep buffer of message to send on reconnect
         }
     }
     catch (SocketException exception)
     {
         Console.Out.WriteLine($"Failed to send message of type {type}. {exception}"); // TODO keep buffer of message to send on reconnect
     }
 }
コード例 #3
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);
        }