Exemplo n.º 1
0
 public static void tcp_client_connection_dropped_event(AsyncTcpClient client, Action <AsyncTcpClient> connection_dropped_func)
 {
     if (client != null)
     {
         client.ConnectionDropped += connection_dropped_func;
     }
 }
Exemplo n.º 2
0
        public static void tcp_client_send(AsyncTcpClient client, Cmd cmd)
        {
            if (cmd == null || client == null || !client.Connected)
            {
                return;
            }

            if (cmd is HeartBeatCmd)
            {
                if (Monitor.TryEnter(client))
                {
                    try { client.SendMessage(cmd); }
                    catch (System.Exception ex) { EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine)); }
                    finally { Monitor.Exit(client); }
                }
            }
            else
            {
                lock (client)
                {
                    try { client.SendMessage(cmd); }
                    catch (System.Exception ex) { EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine)); }
                }
            }
        }
Exemplo n.º 3
0
 public static void stop_tcp_client(ref AsyncTcpClient client, string name)
 {
     try
     {
         if (client != null)
         {
             client.Dispose();
             client = null;
             EzLogger.GlobalLogger.info("stop " + name + " success.");
         }
     }
     catch (System.Exception ex)
     {
         EzLogger.GlobalLogger.warning(string.Format("stop {0} failed: {1}{3}{2}{3}",
                                                     name, ex.Message, ex.StackTrace, Environment.NewLine));
     }
 }
Exemplo n.º 4
0
 public static void connect_tcp_client(AsyncTcpClient client, string name, string server_ip, int server_port)
 {
     try
     {
         if (client != null &&
             !client.Connected &&
             !client.Connect(server_ip, server_port))
         {
             throw new Exception();
         }
         EzLogger.GlobalLogger.info(string.Format("connect {0} to {1}:{2} success.",
                                                  name, server_ip, server_port));
     }
     catch (System.Exception ex)
     {
         EzLogger.GlobalLogger.warning(string.Format("connect {0} to {1}:{2} failed: {3}{5}{4}{5}",
                                                     name, server_ip, server_port, ex.Message, ex.StackTrace, Environment.NewLine));
     }
 }
Exemplo n.º 5
0
        public static bool start_tcp_client(ref AsyncTcpClient client, string name,
                                            bool auto_hb, string sender, Action <AsyncTcpClient, Cmd> recv_func)
        {
            try
            {
                if (client == null)
                {
                    client = new AsyncTcpClient(auto_hb);
                    client.HeartBeatSender  = sender;
                    client.MessageReceived += recv_func;
                    EzLogger.GlobalLogger.info("start " + name + " success.");
                }
                return(true);
            }
            catch (System.Exception ex)
            {
                stop_tcp_client(ref client, name);

                EzLogger.GlobalLogger.warning(string.Format("start {0} failed: {1}{3}{2}{3}",
                                                            name, ex.Message, ex.StackTrace, Environment.NewLine));

                return(false);
            }
        }
Exemplo n.º 6
0
        public static void tcp_client_send(AsyncTcpClient client, Cmd cmd)
        {
            if (cmd == null || client == null || !client.Connected)
                return;

            if (cmd is HeartBeatCmd)
            {
                if (Monitor.TryEnter(client))
                {
                    try { client.SendMessage(cmd); }
                    catch (System.Exception ex) { EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine)); }
                    finally { Monitor.Exit(client); }
                }
            }
            else
            {
                lock (client)
                {
                    try { client.SendMessage(cmd); }
                    catch (System.Exception ex) { EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine)); }
                }
            }
        }
Exemplo n.º 7
0
 public static void tcp_client_connection_dropped_event(AsyncTcpClient client, Action<AsyncTcpClient> connection_dropped_func)
 {
     if (client != null)
     {
         client.ConnectionDropped += connection_dropped_func;
     }
 }
Exemplo n.º 8
0
 public static void stop_tcp_client(ref AsyncTcpClient client, string name)
 {
     try
     {
         if (client != null)
         {
             client.Dispose();
             client = null;
             EzLogger.GlobalLogger.info("stop " + name + " success.");
         }
     }
     catch (System.Exception ex)
     {
         EzLogger.GlobalLogger.warning(string.Format("stop {0} failed: {1}{3}{2}{3}",
             name, ex.Message, ex.StackTrace, Environment.NewLine));
     }
 }
Exemplo n.º 9
0
        public static bool start_tcp_client(ref AsyncTcpClient client, string name,
            bool auto_hb, string sender, Action<AsyncTcpClient, Cmd> recv_func)
        {
            try
            {
                if (client == null)
                {
                    client = new AsyncTcpClient(auto_hb);
                    client.HeartBeatSender = sender;
                    client.MessageReceived += recv_func;
                    EzLogger.GlobalLogger.info("start " + name + " success.");
                }
                return true;
            }
            catch (System.Exception ex)
            {
                stop_tcp_client(ref client, name);

                EzLogger.GlobalLogger.warning(string.Format("start {0} failed: {1}{3}{2}{3}",
                    name, ex.Message, ex.StackTrace, Environment.NewLine));

                return false;
            }
        }
Exemplo n.º 10
0
 public static bool is_tcp_client_normal(AsyncTcpClient client)
 {
     return (client != null) && client.Available;
 }
Exemplo n.º 11
0
        void p1_tcp_client_MessageReceived(AsyncTcpClient client, Cmd cmd)
        {
            if (cmd == null || (cmd is HeartBeatCmd))
                return;

            if (!cmd.sender_name.ToLower().Contains("p1"))
                return;

            try
            {
                switch (cmd.id)
                {
                    case CmdType.Get_All_Ready_Users_Cmd_Ack: OnGetAllReadyUsersAck(_p1_tcp_client, cmd as GetAllReadyUsersCmdAck); break;
                    case CmdType.Get_Changed_Ready_Users_Cmd_Ack: OnGetChangedUsersAck(_p1_tcp_client, cmd as GetChangedReadyUsersCmdAck); break;
                }
            }
            catch (System.Exception ex)
            {
                EzLogger.GlobalLogger.debug(ex.Message);
            }
        }
Exemplo n.º 12
0
 public static bool is_tcp_client_normal(AsyncTcpClient client)
 {
     return((client != null) && client.Available);
 }
Exemplo n.º 13
0
 private void OnGetAllReadyUsersAck(AsyncTcpClient client, GetAllReadyUsersCmdAck ack)
 {
     lock (_users_lock)
     {
         this._users = ack.users;
         ReplaceAllUsersToDisplay(this._users);
     }
     scroll.Reset();
 }
Exemplo n.º 14
0
        void p5_tcp_client_MessageReceived(AsyncTcpClient client, Cmd cmd)
        {
            if (cmd == null || cmd is HeartBeatCmd)
                return;

            if (!cmd.sender_name.ToLower().Contains("p5"))
                return;

            try
            {
                //只向p5发心跳、报告消息,其返回不关心
            }
            catch (System.Exception ex)
            {
                EzLogger.GlobalLogger.debug(ex.Message);
            }
        }
Exemplo n.º 15
0
 public static void connect_tcp_client(AsyncTcpClient client, string name, string server_ip, int server_port)
 {
     try
     {
         if (client != null &&
             !client.Connected &&
             !client.Connect(server_ip, server_port))
             throw new Exception();
         EzLogger.GlobalLogger.info(string.Format("connect {0} to {1}:{2} success.",
             name, server_ip, server_port));
     }
     catch (System.Exception ex)
     {
         EzLogger.GlobalLogger.warning(string.Format("connect {0} to {1}:{2} failed: {3}{5}{4}{5}",
             name, server_ip, server_port, ex.Message, ex.StackTrace, Environment.NewLine));
     }
 }
Exemplo n.º 16
0
 private void _p5_tcp_client_MessageReceived(AsyncTcpClient client, Cmd cmd)
 {
 }
Exemplo n.º 17
0
 private void OnGetChangedUsersAck(AsyncTcpClient client, GetChangedReadyUsersCmdAck ack)
 {
     lock (_users_lock)
     {
         //改变显示
         UpdateChangedUsersToDisplay(ack);
     }
 }