/// <summary> /// Creates a reliable packet channel /// </summary> /// <returns></returns> public ReliableChannel <TPacket> CreateReliableChannel() { var channel = new ReliableChannel <TPacket>(); ConfigurePacketChannel(channel); return(channel); }
internal void Configure(ReliableChannel reliable, UnreliableChannel unreliable) { this.reliableChannel = reliable; this.unreliableChannel = unreliable; this.reliableChannel.listener = this; }
internal void CreateChannels(List <ChannelType> types) { Channels = new Channel[types.Count]; for (byte i = 0; i < types.Count; i++) { switch (types[i]) { case ChannelType.Reliable: { Channels[i] = new ReliableChannel(i); } break; case ChannelType.ReliableSequenced: { Channels[i] = new ReliableSequencedChannel(i); } break; case ChannelType.Unreliable: { Channels[i] = new UnreliableChannel(i); } break; case ChannelType.UnreliableOrdered: { Channels[i] = new UnreliableSequencedChannel(i); } break; } } }
public void TestReliableChannelDoesNotDiscardsOldMessages() { // Create 3 packets, add them in the order 1, 3, 2. // 2 should be discarded var channel = new ReliableChannel(); channel.QueueIncomingPacket(EndPoint, new Packet(PacketType.Connect, Channel.Reliable, 1, new byte[0])); channel.QueueIncomingPacket(EndPoint, new Packet(PacketType.Connect, Channel.Reliable, 3, new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })); channel.QueueIncomingPacket(EndPoint, new Packet(PacketType.Connect, Channel.Reliable, 2, new byte[0])); UnreliableChannel.PendingPacket packet; Assert.IsTrue(channel.TryGetNextIncomingPacket(out packet)); Assert.IsNotNull(packet); Assert.AreEqual(1, packet.Sequence); Assert.IsTrue(channel.TryGetNextIncomingPacket(out packet)); Assert.IsNotNull(packet); Assert.AreEqual(3, packet.Sequence); Assert.AreEqual(new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }, packet.Data); Assert.IsFalse(channel.TryGetNextIncomingPacket(out packet)); Assert.IsNull(packet); }
public void Disconnect() { this.reliableChannel.CloseChannel(); this.unreliableChannel.CloseChannel(this.remoteEndPoint); ReliableChannel.StopIO(); this.unreliableChannel.StopIO(); }
void ITcpClientListener.SocketDidDisconnect() { ReliableChannel.Remove(this.reliableChannel); this.listener?.NetworkClientDidDisconnect(); this.tcpSocket.Close(); }
public void Stop() { ThreadChecker.AssertMainThread(); ReliableChannel.StopIO(); this.tcpSocket.Stop(); this.unreliableChannel.StopIO(); }
void ITcpClientListener.SocketDidConnect() { ReliableChannel.Add(this.reliableChannel); this.udpSocket.Bind(this.tcpSocket.localEndPoint); this.udpSocket.Connect(this.tcpSocket.remoteEndPoint); this.listener?.NetworkClientDidConnect(); }
void ITcpServerListener <TcpSocket> .SocketDidDisconnect(TcpSocket socket) { ThreadChecker.AssertReliableChannel(); var channel = this.socketCollection.Remove(socket); if (channel == null) { return; } ReliableChannel.Remove(channel); this.listener?.NetworkServerPlayerDidDisconnect(channel); }
public void Start(NetEndPoint endPoint) { ThreadChecker.AssertMainThread(); this.tcpSocket.Bind(endPoint); this.tcpSocket.Start(); this.udpSocket.Bind(endPoint); this.listeningOnEndPoint = this.tcpSocket.localEndPoint; ReliableChannel.StartIO(); this.unreliableChannel.StartIO(); }
void ITcpServerListener <TcpSocket> .SocketDidAccept(TcpSocket socket) { ThreadChecker.AssertAcceptThread(); if (socket == null) { return; } socket.serverListener = this; var reliable = new ReliableChannel(socket); ReliableChannel.Add(reliable); this.socketCollection.Add(socket, reliable); this.listener?.NetworkServerDidAcceptPlayer(reliable, this.unreliableChannel); }
public void TestReliableChannelResends() { var channel = new ReliableChannel(); channel.QueueOutgoingPacket(EndPoint, new Packet(PacketType.Connect, Channel.Reliable, 1, new byte[0])); UnreliableChannel.PendingPacket[] packets = channel.GetPendingOutgoingPackets().ToArray(); Assert.IsNotEmpty(packets); Assert.AreEqual(1, packets[0].Sequence); channel.QueueIncomingPacket(EndPoint, new Packet(PacketType.Ack, Channel.Reliable, (ushort)packets[0].Sequence, new byte[0])); packets = channel.GetPendingIncomingPackets().ToArray(); Assert.IsEmpty(packets); channel.QueueOutgoingPacket(EndPoint, new Packet(PacketType.Connect, Channel.Reliable, 2, new byte[0])); packets = channel.GetPendingOutgoingPackets().ToArray(); Assert.IsNotEmpty(packets); Assert.AreEqual(2, packets[0].Sequence); Thread.Sleep(600); packets = channel.GetPendingOutgoingPackets().ToArray(); Assert.IsNotEmpty(packets); Assert.AreEqual(2, packets[0].Sequence); Thread.Sleep(600); packets = channel.GetPendingOutgoingPackets().ToArray(); Assert.IsNotEmpty(packets); Assert.AreEqual(2, packets[0].Sequence); channel.QueueIncomingPacket(EndPoint, new Packet(PacketType.Ack, Channel.Reliable, (ushort)packets[0].Sequence, new byte[0])); packets = channel.GetPendingIncomingPackets().ToArray(); Assert.IsEmpty(packets); packets = channel.GetPendingOutgoingPackets().ToArray(); Assert.IsEmpty(packets); }
virtual protected void Start() { _players = new Dictionary <int, Player>(); _autoAttacks = new Dictionary <int, AutoAttack>(); _channel = CreateChannel(); }
void IReliableChannelListener.ChannelDidReceiveMessage(ReliableChannel channel, MessageContainer container) { this.listener?.PlayerDidReceiveMessage(container, this); }
void INetworkServerListener.NetworkServerPlayerDidDisconnect(ReliableChannel channel) { ThreadChecker.AssertReliableChannel(); this.clientAcceptor.NetworkServerPlayerDidDisconnect(channel); }
void INetworkServerListener.NetworkServerDidAcceptPlayer(ReliableChannel reliable, UnreliableChannel unreliable) { ThreadChecker.AssertReliableChannel(); this.clientAcceptor.NetworkServerDidAcceptPlayer(reliable, unreliable); }
public void Connect(string host, int port) { this.tcpSocket.Connect(new NetEndPoint(IPAddress.Parse(host), port)); ReliableChannel.StartIO(); this.unreliableChannel.StartIO(); }
void IReliableChannelListener.ChannelDidReceiveMessage(ReliableChannel channel, MessageContainer container) { this.listener?.NetworkClientDidReceiveMessage(container); }