コード例 #1
0
        public void ConnectTest()
        {
            AutoResetEvent evt          = new AutoResetEvent(false);
            var            isConnected  = false;
            var            isConnecting = false;
            var            tcp          = new TcpSocket();

            tcp.OnConnecting += () =>
            {
                isConnecting = true;
            };
            tcp.OnConnected += () =>
            {
                isConnected = true;
                evt.Set();
            };
            tcp.OnDisconnected += () =>
            {
                isConnected = false;
                evt.Set();
            };
            tcp.Connect("127.0.0.1", 999);
            evt.WaitOne();
            Assert.IsTrue(isConnecting);
            Assert.IsTrue(isConnected);

            tcp.Disconnect();
            evt.WaitOne();
            Assert.IsFalse(isConnected);
        }
コード例 #2
0
        public void TestEvent()
        {
            bool isOnConnecting   = false;
            bool isOnConnected    = false;
            bool isOnDisconnected = false;
            bool isOnSend         = false;
            bool isOnReceive      = false;

            TcpSocket tcp = new TcpSocket();

            tcp.OnConnecting   += () => { isOnConnecting = true; };
            tcp.OnConnected    += () => { isOnConnected = true; };
            tcp.OnDisconnected += () => { isOnDisconnected = true; };
            tcp.OnSendBytes    += (x) => { isOnSend = true; };
            tcp.OnReceiveBytes += (x) => { isOnReceive = true; };

            tcp.Connect(Common.GetIpEndPoint());
            Assert.IsTrue(isOnConnecting);

            Common.WaitTrue(ref isOnConnected);
            Assert.IsTrue(isOnConnected);

            tcp.Send(new byte[10]);
            Common.WaitTrue(ref isOnSend);
            Assert.IsTrue(isOnSend);

            Common.WaitTrue(ref isOnReceive);
            Assert.IsTrue(isOnReceive);

            tcp.Disconnect();
            Assert.IsTrue(isOnDisconnected);
        }
コード例 #3
0
        /// <summary>
        /// Handle the disconnection of a client.
        /// </summary>
        /// <param name="aSocket">The socket fo the client now disconnected.</param>
        private static void DisconnectionHandler(TcpSocket aSocket)
        {
            try
            {
                if (aSocket != null && aSocket.Wrapper != null)
                {
                    Client client = aSocket.Wrapper as Client;

                    if (client.Account != null)
                    {
                        sLogger.Info("Disconnection of {0}, with {1}.", aSocket.IPAddress, client.Account);
                    }

                    client.Disconnect();
                    client.Dispose();

                    aSocket.Wrapper = null;
                }
                else if (aSocket != null)
                {
                    aSocket.Disconnect();
                }
            }
            catch (Exception exc) { sLogger.Error(exc); }
        }
コード例 #4
0
        public void TestConnect()
        {
            TcpSocket tcp = new TcpSocket();

            tcp.Connect(Common.GetIpEndPoint());

            //Test connected
            Common.WaitConnected(tcp);
            Assert.IsTrue(tcp.IsConnected);

            //Test disconnect
            tcp.Disconnect();
            Assert.IsFalse(tcp.IsConnected);
        }
コード例 #5
0
        public void TestSendReceive()
        {
            var tcp = new TcpSocket();

            tcp.Connect(Common.GetIpEndPoint());
            Common.WaitConnected(tcp);
            int length = 0;

            tcp.OnReceiveBytes += (x) =>
            {
                length = x.Length;
            };
            tcp.Send(new byte[10]);
            Common.WaitValue(ref length, 10);
            tcp.Disconnect();
            Assert.AreEqual(length, 10);
        }
コード例 #6
0
        public void TestLargeMessage()
        {
            var tcp = new TcpSocket();

            tcp.Connect(Common.GetIpEndPoint());
            Common.WaitConnected(tcp);
            int length = 0;

            tcp.OnReceiveBytes += (x) =>
            {
                length += x.Length;
            };
            tcp.Send(new byte[1 << 10]);
            Common.WaitValue(ref length, 1 << 10, 10000);
            tcp.Disconnect();
            Console.WriteLine(length);
            Assert.AreEqual(length, 1 << 10);
        }
コード例 #7
0
        /// <summary>
        /// Handle the reception of data from a client.
        /// </summary>
        /// <param name="aSocket">The socket of the client that sent the data.</param>
        /// <param name="aData">The data.</param>
        private static void ReceiveHandler(TcpSocket aSocket, ref Byte[] aData)
        {
            try
            {
                if (aSocket != null && aSocket.Wrapper != null)
                {
                    Client client = aSocket.Wrapper as Client;

                    if (client == null)
                    {
                        return;
                    }

                    client.Receive(ref aData);
                }
            }
            catch (Exception exc) { sLogger.Error(exc); aSocket.Disconnect(); }
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: CaptJiggly/SocksLib
        static void client()
        {
            Console.Title = "Client";
            TcpSocket socket = new TcpSocket();
            socket.AsyncConnectionResult += (s, e) =>
            {
                if (e.Connected)
                {
                    Console.WriteLine("Client Connected!");
                    beginChat(false, socket);

                    while (true)
                    {
                        string msg = Console.ReadLine();

                        if (socket.Connected)
                        {
                            if (msg.ToLower() != "disconnect")
                            {
                                byte[] msgPayload = Encoding.ASCII.GetBytes(msg);

                                socket.SendAsync(msgPayload);
                            }
                            else
                            {
                                socket.Disconnect();
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Connection could not be made... Retrying in 2 seconds...");
                    client();
                }
            };
            socket.ConnectAsync(IPAddress.Loopback, 1000);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: CaptJiggly/SocksLib
        static void server()
        {
            Console.Title = "Server";
            Console.WriteLine("Waiting for client...");
            TcpSocketListener l = new TcpSocketListener(1000);
            TcpSocket client = null;
            l.Accepted += (s, e) =>
            {
                Console.WriteLine("Client accepted!");
                client = e.AcceptedSocket;
                beginChat(true, client);
                l.Stop();
            };
            l.Start();

            while (true)
            {
                string msg = Console.ReadLine();

                if (client.Connected)
                {
                    if (msg.ToLower() != "disconnect")
                    {
                        byte[] msgPayload = Encoding.ASCII.GetBytes(msg);

                        client.SendAsync(msgPayload);
                    }
                    else
                    {
                        client.Disconnect();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            server();
        }
コード例 #10
0
        /// <summary>
        /// Handle the reception of data from a client.
        /// </summary>
        /// <param name="aSocket">The socket of the client that sent the data.</param>
        /// <param name="aData">The data.</param>
        private static void ReceiveHandler(TcpSocket aSocket, ref Byte[] aData)
        {
            try
            {
                if (aSocket != null && aSocket.Wrapper != null)
                {
                    Client client = aSocket.Wrapper as Client;

                    if (client == null)
                    {
                        return;
                    }

                    client.Receive(ref aData);
                }
            }
            catch (Exception exc)
            {
                sLogger.Warn("Something wrong happened while receiving data.\n{0}", exc);
                aSocket.Disconnect();
            }
        }
コード例 #11
0
        /// <summary>
        /// Disconnect the client.
        /// </summary>
        public void Disconnect()
        {
            try
            {
                if (Player != null)
                {
                    if (Player.TransformEndTime != 0)
                    {
                        if (Player.CurHP >= Player.MaxHP)
                        {
                            Player.CurHP = Player.MaxHP;
                        }
                        Double Multiplier = (Double)Player.CurHP / (Double)Player.MaxHP;
                        Player.CalcMaxHP();
                        Player.CurHP = (Int32)(Player.MaxHP * Multiplier);

                        MyMath.GetEquipStats(Player);
                    }
                    //User.mThread.Stop();

                    Database.Save(Player, false);

                    //Mine
                    if (Player.Mining)
                    {
                        Player.Mining = false;
                    }

                    //Booth
                    if (Player.Booth != null)
                    {
                        Player.Booth.Destroy();
                    }

                    //Team
                    if (Player.Team != null)
                    {
                        if (Player.Team.Leader.UniqId == Player.UniqId)
                        {
                            Player.Team.Dismiss(Player);
                        }
                        else
                        {
                            Player.Team.DelMember(Player, true);
                        }
                        Player.Team = null;
                    }

                    // Pet
                    if (Player.Pet != null)
                    {
                        Player.Pet.Brain.Sleep();
                        Player.Pet.Disappear();
                        Player.Pet = null;
                    }

                    //Deal
                    if (Player.Deal != null)
                    {
                        Player.Deal.Release();
                    }

                    //Friends
                    foreach (Int32 FriendUID in Player.Friends.Keys)
                    {
                        Player Friend = null;
                        if (!World.AllPlayers.TryGetValue(FriendUID, out Friend))
                        {
                            continue;
                        }

                        Friend.Send(new MsgFriend(Player.UniqId, Player.Name, MsgFriend.Status.Offline, MsgFriend.Action.FriendOffline));
                    }

                    //Enemies
                    foreach (Player Enemy in World.AllPlayers.Values)
                    {
                        if (!Enemy.Enemies.ContainsKey(Player.UniqId))
                        {
                            continue;
                        }

                        Enemy.Send(new MsgFriend(Player.UniqId, Player.Name, MsgFriend.Status.Offline, MsgFriend.Action.EnemyOffline));
                    }

                    //Screen
                    if (Player.Screen != null)
                    {
                        Player.Screen.Clear(true);
                    }

                    Player.Map.DelEntity(Player);

                    if (World.AllPlayers.ContainsKey(Player.UniqId))
                    {
                        World.AllPlayers.Remove(Player.UniqId);
                    }

                    if (World.AllPlayerNames.ContainsKey(Player.Name))
                    {
                        World.AllPlayerNames.Remove(Player.Name);
                    }

                    Player.Dispose();
                    Player = null;
                }

                Server.NetworkIO.DelClient(this, ref mNetworkWorker);

                if (mSocket != null && mSocket.IsAlive)
                {
                    mSocket.Disconnect();
                }
            }
            catch (Exception exc) { Console.WriteLine(exc); }
        }
コード例 #12
0
 /// <summary>
 /// Disconnect the client.
 /// </summary>
 public void Disconnect()
 {
     mSocket.Disconnect();
 }