public async Task TestSimpleSendRecieve() { // Connect B to A: TcpNetworkConnection connectionOnA = null; factoryA.OnClientConnected += (sender, connectoin) => { connectionOnA = connectoin; }; TcpNetworkConnection connectionOnB = await factoryB.ConnectToAsync(IPAddress.Loopback); await Task.Delay(100); // Send message from B to A: INetworkMessage recievedOnA = null; connectionOnA.OnRecieve += (sender, message) => { recievedOnA = message; }; INetworkMessage sendFromB = new DirectNetworkMessage( DhtUtils.GeneratePlayerId(), DhtUtils.GeneratePlayerId(), DhtUtils.GeneratePlayerId(), new byte[] { 42 }); connectionOnB.Send(sendFromB); await Task.Delay(100); // Assert: Assert.IsNotNull(recievedOnA); Assert.AreEqual(42, ((DirectNetworkMessage)recievedOnA).Data[0]); }
public async Task TestConnect() { // Connect B to A: TcpNetworkConnection connectionOnA = null; factoryA.OnClientConnected += (sender, connectoin) => { connectionOnA = connectoin; }; await factoryB.ConnectToAsync(IPAddress.Loopback); await Task.Delay(100); // Assert: Assert.IsNotNull(connectionOnA); Assert.IsTrue(IPAddress.IsLoopback(connectionOnA.Address)); }
public async Task TestConnectionDropped() { // Connect B to A: TcpNetworkConnection connectionOnA = null; factoryA.OnClientConnected += (sender, connectoin) => { connectionOnA = connectoin; }; TcpNetworkConnection connectionOnB = await factoryB.ConnectToAsync(IPAddress.Loopback); await Task.Delay(100); bool called = false; connectionOnA.OnConnectionDropped = (sender, args) => { called = true; }; // Kill B: connectionOnB.Dispose(); await Task.Delay(100); // Assert: Assert.IsTrue(called); }
private void PacketReceived(TcpNetworkConnection connection, byte[] packet) { lock (this.ConnectionLock) { this.DeliverPacket(this.Settings.Serializer.Deserialize(packet)); } }
private void EndAcceptConnection(IAsyncResult result) { lock (this.ConnectionLock) { if (this._IsOpen) { try { Socket socket = this.Socket.EndAccept(result); if (this.Settings.MaxConnectionCount == 0 || this._ConnectionCount < this.Settings.MaxConnectionCount) { int id = this.ConnectionIDCounter++; if (id == 0) id = this.ConnectionIDCounter++; SocketError e; try { socket.Send(BitConverter.GetBytes(id), 0, 4, SocketFlags.None, out e); } catch { e = SocketError.SocketError; } if (e == SocketError.Success) { TcpNetworkConnection connection = new TcpNetworkConnection(id, socket, ref this.ConnectionLock); connection.OnDisconnect += new Action<TcpNetworkConnection>(ConnectionDisconnected); connection.OnPacketReceived += new Action<TcpNetworkConnection, byte[]>(PacketReceived); connection.Connect(); this.Connections.Add(id, connection); this._ConnectionCount++; // connection.Send(new NetworkPacket { Service = -1, Targets = new int[1] { id }, Data = id }); if (this.OnConnectionConnected != null) this.OnConnectionConnected(this, connection); } } else { try { socket.BeginSend(new byte[4], 0, 4, SocketFlags.None, null, null); } catch { } } this.BeginAcceptConnection(); } catch (SocketException e) { this.Error(e.SocketErrorCode); } } } }
private void ConnectionDisconnected(TcpNetworkConnection connection) { lock (this.ConnectionLock) { this.Connections.Remove(connection.ID); if (this.OnConnectionDisconnected != null) this.OnConnectionDisconnected(this, connection); } }
private void ConnectionDisconnected(TcpNetworkConnection conn) { lock (this.ConnectionLock) { if (this._IsConnected) { this.Disconnect(); } } }