コード例 #1
0
        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]);
        }
コード例 #2
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));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: NetworkServer.cs プロジェクト: jamiees2/Pie
 private void PacketReceived(TcpNetworkConnection connection, byte[] packet)
 {
     lock (this.ConnectionLock)
     {
         this.DeliverPacket(this.Settings.Serializer.Deserialize(packet));
     }
 }
コード例 #5
0
ファイル: NetworkServer.cs プロジェクト: jamiees2/Pie
        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);
                    }
                }
            }
        }
コード例 #6
0
ファイル: NetworkServer.cs プロジェクト: jamiees2/Pie
 private void ConnectionDisconnected(TcpNetworkConnection connection)
 {
     lock (this.ConnectionLock)
     {
         this.Connections.Remove(connection.ID);
         if (this.OnConnectionDisconnected != null) this.OnConnectionDisconnected(this, connection);
     }
 }
コード例 #7
0
ファイル: NetworkClient.cs プロジェクト: jamiees2/Pie
 private void ConnectionDisconnected(TcpNetworkConnection conn)
 {
     lock (this.ConnectionLock)
     {
         if (this._IsConnected)
         {
             this.Disconnect();
         }
     }
 }