Beispiel #1
0
 public override List<byte> pack(Cmd cmd)
 {
     List<byte> buffer = cmd.to_byte();
     long size = buffer.Count;
     buffer.InsertRange(0, Cmd.get_bytes(size));
     return buffer;
 }
Beispiel #2
0
        private void p2_cmd_handler(IPEndPoint remote, Cmd cmd_base)
        {
            if (cmd_base is ConfirmIpCmd)
            {
                ConfirmIpCmd cmd = cmd_base as ConfirmIpCmd;

                BroadcastAddressCmd ack = new BroadcastAddressCmd();
                ack.sender_name = this_name;
                ack.ip = cmd.ip;
                ack.port = Helper.p5_tcp_listen_port;
                _p5_udp_peer.SendMessage(
                    new IPEndPoint(IPAddress.Parse(ack.ip), remote.Port),
                    ack);
            }
        }
Beispiel #3
0
 private void p4_tcp_handler(IScsServerClient sc, Cmd cmd)
 {
     switch (cmd.id)
     {
         case CmdType.Get_All_Ready_Users_Cmd: OnGetAllReadyUsers(sc, cmd as GetAllReadyUsersCmd); break;
         case CmdType.Get_Changed_Ready_Users_Cmd: OnGetChangedReadyUsers(sc, cmd as GetChangedReadyUsersCmd); break;
     }
 }
Beispiel #4
0
 private void p2_tcp_handler(IScsServerClient sc, Cmd cmd)
 {
     switch (cmd.id)
     {
         case CmdType.Query_User_Cmd: OnQueryUser(sc, cmd as QueryUserCmd); break;
         case CmdType.Server_Print_User_Cmd: OnServerPrintUser(sc, cmd as ServerPrintUserCmd); break;
         case CmdType.Login_Cmd: OnLogin(sc, cmd as LoginCmd); break;
         case CmdType.Client_Print_User_Cmd: OnClientPrintUser(sc, cmd as ClientPrintUserCmd); break;
     }
 }
Beispiel #5
0
        private void p1_tcp_server_MessageReceived(IScsServerClient sc, Cmd cmd)
        {
            try
            {
                //心跳消息底层处理过
                if (cmd is HeartBeatCmd)
                    return;

                string sender = cmd.sender_name.ToLower();
                if (sender.Contains("p4"))
                    Task.Factory.StartNew(() => p4_tcp_handler(sc, cmd));
                else if (sender.Contains("p2"))
                    Task.Factory.StartNew(() => p2_tcp_handler(sc, cmd));
            }
            catch (System.Exception ex)
            {
                EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine));
            }
        }
Beispiel #6
0
 public virtual List<byte> pack(Cmd cmd)
 {
     throw new Exception("no implementation.");
 }
Beispiel #7
0
        public static void tcp_server_send(IScsServerClient server, Cmd cmd)
        {
            if (server == null || cmd == null || server.CommunicationState != CommunicationStates.Connected)
                return;

            lock (server)
            {
                try { server.SendMessage(cmd); }
                catch (System.Exception ex) { EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine)); }
            }
        }
Beispiel #8
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);
            }
        }
Beispiel #9
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)); }
                }
            }
        }
Beispiel #10
0
        void p5_tcp_server_MessageReceived(IScsServerClient sc, Cmd cmd)
        {
            EzLogger.GlobalLogger.debug("message sender = " + cmd.sender_name ?? "");

            //根据cmd.sender_name设置各个远程设备状态
            //todo

            //p5必须响应的其它命令
            //todo
        }
Beispiel #11
0
 public virtual List <byte> pack(Cmd cmd)
 {
     throw new Exception("no implementation.");
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="timeout">ms为单位. 1000 stands for 1s.</param>
        /// <returns></returns>
        public virtual Cmd SendMessageAndWaitForResponse(Cmd cmd, int timeout = 15000)
        {
            if (Messenger == null)
                return null;

            lock (Messenger)
            {
                return Messenger.SendMessageAndWaitForResponse(cmd, timeout) as Cmd;
            }
        }
        protected virtual void OnMessageReceived(Cmd cmd)
        {
            EzLogger.GlobalLogger.debug(string.Format("tcp client receive {2} from {0}:{1}",
                    this.ServerIp, this.ServerPort, cmd.GetType().Name));
            EzLogger.GlobalLogger.debug(cmd.ToString());

            if (MessageReceived != null)
                MessageReceived(this, cmd);
        }
Beispiel #14
0
        private void p5_udp_handler(IPEndPoint remote, Cmd cmd_base)
        {
            if (cmd_base is BroadcastIpListCmd)
            {
                BroadcastIpListCmd cmd = cmd_base as BroadcastIpListCmd;
                int size = cmd.ips == null ? 0 : cmd.ips.Length;
                for (int i = 0; i < size; i++)
                {
                    ConfirmIpCmd ack = new ConfirmIpCmd();
                    ack.ip = cmd.ips[i];
                    ack.sender_name = this_name;
                    _p1_udp_peer.SendMessage(
                        new IPEndPoint(IPAddress.Parse(ack.ip), remote.Port),
                        ack);
                }
            }
            else if (cmd_base is BroadcastAddressCmd)
            {
                if (_p5_tcp_client != null && _p5_tcp_client.Connected)
                    return;

                BroadcastAddressCmd cmd = cmd_base as BroadcastAddressCmd;

                _p5_ip = cmd.ip;
                _p5_port = cmd.port;

                Task.Factory.StartNew(() =>
                {
                    start_p5_tcp_client();
                    connect_p5_tcp_client(cmd.ip, cmd.port);
                });
            }
        }
Beispiel #15
0
        public static Cmd tcp_client_send_and_response(RequestReplyTcpClient client, Cmd cmd, int timeout=15000)
        {
            try
            {
                if (cmd == null || client == null || !client.Connected)
                    return null;

                return client.SendMessageAndWaitForResponse(cmd, timeout);
            }
            catch (System.Exception ex)
            {
                EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine));

                return null;
            }
        }
Beispiel #16
0
 private void _p5_tcp_client_MessageReceived(AsyncTcpClient client, Cmd cmd)
 {
 }
Beispiel #17
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);
            }
        }