public ConnectionData(int connectionId) { this.connectionId = connectionId; currentStage = ConnectionStage.AwaitingEncryptionSupport; nickname = null; aesCommunicationKey = null; }
private static Mock <ISocket> CreateSocket() { var socket = new Mock <ISocket>(); socket.Setup(x => x.Poll(It.IsAny <int>(), It.IsAny <SelectMode>())) .Returns(true); socket.Setup(x => x.Connected).Returns(true); socket.Setup(x => x.RemoteEndPoint).Returns(new IPEndPoint(IPAddress.Loopback, 1234)); socket.Setup(x => x.Disconnect(It.IsAny <bool>())) .Callback(() => socket.Setup(x => x.Connected).Returns(false)); socket.Setup(x => x.Send(It.IsAny <byte[]>())) .Returns((byte[] buffer) => buffer.Length); socket.Setup(x => x.Send(It.IsAny <byte[]>(), It.IsAny <SocketFlags>())) .Returns((byte[] buffer, SocketFlags flags) => buffer.Length); socket.Setup(x => x.Send(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <SocketFlags>())) .Returns((byte[] buffer, int size, SocketFlags flags) => size); socket.Setup(x => x.Send(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <SocketFlags>())) .Returns((byte[] buffer, int offset, int size, SocketFlags flags) => size); SocketError errorCode; socket.Setup(x => x.Send(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <SocketFlags>(), out errorCode)) .Returns((byte[] buffer, int offset, int size, SocketFlags flags, SocketError unused) => size); bool isDisposed = false; socket.Setup(x => x.Dispose()).Callback(() => isDisposed = true); var message = CreateMessage(AbstractBinaryStreamEndPoint <ISocket> .NoAuthenticationRequiredMessage, string.Empty); ConnectionStage stage = ConnectionStage.HandshakeLength; socket.Setup(x => x.Available).Returns(4); socket.Setup(x => x.Receive(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <SocketFlags>(), out errorCode)) .Returns((byte[] buffer, int offset, int size, SocketFlags flags, SocketError unused) => { switch (stage) { case ConnectionStage.HandshakeLength: using (var stream = new MemoryStream(buffer, true)) using (var writer = new BinaryWriter(stream)) { writer.Write(message.Length); writer.Flush(); } socket.Setup(x => x.Available).Returns(message.Length); stage = ConnectionStage.Handshake; return(4); case ConnectionStage.Handshake: message.CopyTo(buffer, 0); stage = ConnectionStage.Other; return(message.Length); default: while (!isDisposed) { Thread.Sleep(100); } return(0); } }); return(socket); }
public void SetStage(ConnectionStage stage) { this.currentStage = stage; }