呼叫中心(CallCenter)开发应用系列(3)

初始化的类,用以关联Socket以及语音卡控制线程,一遍后续的业务控制。

该类初始化了相关的属性,串联了多个对象,在主进程中实例化!

 

using System; using System.Collections.Generic; using System.Text; using CC.QJ.Tools; using CC.DJ081A.Lib; using System.Threading; using System.Windows.Forms; using System.Collections; using System.Net; using System.Net.Sockets; using CC.QJSocket.Server; using System.Configuration; namespace CallCenterApp { public class MyService { SocketListener listener; pubvar.ui_writelog _writelog; MyLogSerice logservice; int TotalLine = 0; //核心线程 CallThread callthread; Thread SocketServerth; System.Timers.Timer ListenerTimer; Trunk[] DJChannel; public CardHelper.refreshlv _RefreshLv; private static readonly string socketConfig = ConfigurationManager.AppSettings["socketConfig"]; private static readonly string xmlfile = ConfigurationManager.AppSettings["cogfigpath"]; public MyService() { logservice = new MyLogSerice(); } public pubvar.ui_writelog Writelog { set { _writelog = value; } } private void WriteLog(LogFile lf, string log) { //写文件 logservice.fileLog(lf, log); //写界面 _writelog(log); } public void InitCard() { int i = pubvar.LoadPlugConfig(xmlfile); if (i == 0) { WriteLog(LogFile.Trace, "加载配置文件完成!"); } else { WriteLog(LogFile.Trace, "加载配置文件失败!"); } int iReturn = Tc08a32.LoadDRV(); if (iReturn != 0) { WriteLog(LogFile.Trace, "加载D081A模拟语音卡驱动程序失败!"); return; } Thread.Sleep(1000); TotalLine = Tc08a32.CheckValidCh(); iReturn = Tc08a32.EnableCard(TotalLine, 1024 * 8); if (iReturn != 0) { Tc08a32.FreeDRV(); WriteLog(LogFile.Trace, "激活D081A语音卡失败 !"); return; } Thread.Sleep(1000); // 需从配置文件中读取参数 Tc08a32.SetBusyPara(350); // 设置忙音参数 Tc08a32.SetDialPara(1000, 4000, 350, 7); // 设置回铃音参数 Tc08a32.SetPackRate(pubparam.PACK_64KBPS); // 设置压缩率 Tc08a32.Sig_Init(0); // 使用新的信号音检测函数 Tc08a32.ReadGenerateSigBuf("system_voc//teding.voc"); //初始化通道 InitChn(); InitThread(); WriteLog(LogFile.Trace,"-----------系统初始化成功!-------------"); } private void StartSocketThread() { listener = new SocketListener(); //绑定回调事件 listener.CbMessage += new pubvar.CallbackClientMessage(callthread.listener_CbMessage); //将socket监听对象赋值给语音卡线程,因为,语音卡线程要根据流程发起对客户端的消息发送。 callthread._socketListener = listener; listener.StartListen(socketConfig, WriteLog); } private void InitThread() { //初始化语音卡线程 callthread = new CallThread(DJChannel,TotalLine); callthread.LogService = WriteLog; callthread._RefreshLv = _RefreshLv; //初始化socket监听线程 SocketServerth = new Thread(new ThreadStart(StartSocketThread)); } /// <summary> /// 这一段初始化是核心。 /// </summary> public void RunKernelFunciton() { //启动语音卡线程 callthread.start(); WriteLog(LogFile.Trace, "开启语音卡主线程。"); //启动socket监听线程 SocketServerth.Start(); WriteLog(LogFile.Trace, "开启socket的监听线程。"); OpenTimer(); WriteLog(LogFile.Trace, "开启客户端连接监听时钟。"); } /// <summary> /// 初始化通道状态 /// </summary> private void InitChn() { DJChannel = new Trunk[TotalLine]; for (int i = 0; i < this.TotalLine; i++) { DJChannel[i].chn = i; DJChannel[i].callee = string.Empty; DJChannel[i].caller = string.Empty; DJChannel[i].chnstatus = CHN_STATUS.LINE_INIT; DJChannel[i].dispstatus = " "; DJChannel[i].dtmf = string.Empty; DJChannel[i].dtmflength = 0; DJChannel[i].chntype = Tc08a32.CheckChType(i); DJChannel[i].busyflag = false; DJChannel[i].connchn = -1; DJChannel[i].calltime = string.Empty; DJChannel[i].workflag = false; DJChannel[i].filename = null; } WriteLog(LogFile.Trace, "初始化语音卡通道属性完成!"); } public void InitDisp(ListView lv) { if (TotalLine <= 1) { return; } for (int i = 0; i < TotalLine; i++) { ListViewItem lvi = new ListViewItem(DJChannel[i].chn.ToString()); lvi.SubItems.Add(CardHelper.GetChnTypeById((int)DJChannel[i].chntype)); lvi.SubItems.Add(DJChannel[i].dispstatus); lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); lv.Items.Add(lvi); } } /// <summary> /// 用以监听socket客户端的链接. /// </summary> private void OpenTimer() { ListenerTimer = new System.Timers.Timer(2000); //实例化Timer类,设置间隔时间为5000毫秒; ListenerTimer.Elapsed += new System.Timers.ElapsedEventHandler(CheckListen); //到达时间的时候执行事件; ListenerTimer.AutoReset = true; ListenerTimer.Start(); } private void CheckListen(object sender, System.Timers.ElapsedEventArgs e) { if (listener != null && listener.Connection != null) { ArrayList deletedKeys = new ArrayList(); try { foreach (DictionaryEntry obj in listener.Connection) { if (((Connection)obj.Value)._connection == null) { deletedKeys.Add(obj); } } foreach (var key in deletedKeys) { WriteLog(LogFile.Trace, "释放客户端连接,连接数:" + listener.Connection.Count.ToString()); listener.Connection.Remove(((DictionaryEntry)key).Key); listener.Threadpools.Remove(((DictionaryEntry)key).Key); string ip = ((DictionaryEntry)key).Key.ToString().Split(':')[0]; //取消注册 CardHelper.ChangerRegStatusOfInner(ip, -1); WriteLog(LogFile.Trace, "注销坐席连接!"); } deletedKeys.Clear(); deletedKeys = null; } catch (Exception ex) { WriteLog(LogFile.Error, "remove a socket @! " + ex.Message.ToString()); deletedKeys.Clear(); deletedKeys = null; } } } public void ExitSystem() { //关闭客户端连接监听时钟 ListenerTimer.Stop(); ListenerTimer.Close(); ListenerTimer = null; //释放监听对象本身 listener.FreeSocketListener(); Thread.Sleep(1000); //停止并释放监听线程 SocketServerth.Abort(); SocketServerth = null; //停止并释放语音卡线程 callthread.Stop(); callthread = null; //释放板卡资源. Tc08a32.FreeDRV(); } } }

你可能感兴趣的:(thread,exception,socket,String,ListView,null)