Beispiel #1
0
        /// <summary>
        /// 服务器写入通讯数据
        /// </summary>
        /// <param name="client">客户端连接</param>
        /// <param name="server">服务器</param>
        private void writeData(ClientInfo clientinfo, ServerBase server)
        {
            if (null == clientinfo || null == server)
            {
                return;
            }
            Socket        client       = clientinfo.Client;
            List <byte[]> responseList = clientinfo.BufferResponse;

            while (true)
            {
                //心跳超时关闭退出
                if (DateTime.Now - clientinfo.OPdt > server.TimeDisconn)
                {
                    server.Close(clientinfo);
                    //string msgex = "心跳超时退出,剩余连接数:" + Convert.ToString(server.ClientList.Count);
                    //ExceptionManager.Publish(new Exception(msgex));
                    DisconnEventArgs args = new DisconnEventArgs(client, server);
                    this.raiseEvent(args);
                    return;
                }
                for (int i = 0; i < 80; i++)
                {
                    if (responseList.Count > 0)
                    {
                        break;
                    }
                    Thread.Sleep(5);
                }
                if (responseList.Count < 1)
                {
                    continue;
                }
                //同步事件触发发送响应数据
                clientinfo.OPdt = DateTime.Now;
                Monitor.Enter(clientinfo);
                byte[][] responseArray = responseList.ToArray();
                responseList.Clear();
                Monitor.PulseAll(clientinfo);
                Monitor.Exit(clientinfo);
                try
                {
                    for (int i = 0; i < responseArray.Length; i++)
                    {
                        client.Send(responseArray[i]);
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " S发送:" + CommandBase.Parse(responseArray[i]));
                        myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " S发送:" + CommandBase.Parse(responseArray[i]));
                        ResponseSrvEventArgs argresp = new ResponseSrvEventArgs(client, server, responseArray[i]);
                        this.raiseEvent(argresp);
                    }
                }
                catch (Exception ex)
                {
                    server.Close(clientinfo);
                    //NameValueCollection attr = new NameValueCollection();
                    //attr["剩余连接数"] = Convert.ToString(server.ClientList.Count);
                    //ExceptionManager.Publish(ex, attr);
                    DisconnEventArgs args = new DisconnEventArgs(client, server, ex);
                    this.raiseEvent(args);
                    return;
                }
                clientinfo.OPdt = DateTime.Now;
            }
        }
 /// <summary>
 /// 构造函数,默认成功
 /// </summary>
 /// <param name="client">通讯的Socket/UdpClient/SerailPort</param>
 /// <param name="cmds">当前进行通讯的指令列表</param>
 /// <param name="response">响应结果</param>
 /// <param name="cmd">当前指令</param>
 /// <param name="success">响应是否成功</param>
 public ResponseEventArgs(object client, CommiTarget param, CommandBase[] cmds, byte[] response, CommandBase cmd, bool success)
 {
     this.Client         = client;
     this.Commands       = cmds;
     this.Response       = response;
     this.ContentLen     = null == response ? 0 : response.Length;
     this.CurrentCommand = cmd;
     this.Success        = success;
     this.Target         = param;
 }