?? gatewayservice.cs
字號(hào):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Text;
using System.Windows;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Net;
namespace GateWayFroCMPP3
{
public class CMPP3GateWayService : System.ServiceProcess.ServiceBase
{
private CMPP3.Client.CMPP3Client cmppClient;//=new CMPP.client.CMPPClient();
/// <summary>
/// 網(wǎng)關(guān)所在IP
/// </summary>
private string CM_GateWayIP;//真實(shí)網(wǎng)關(guān)IP
/// <summary>
/// 網(wǎng)關(guān)端口
/// </summary>
private int CM_GateWayPort;//真實(shí)網(wǎng)關(guān)端口
/// <summary>
/// 網(wǎng)關(guān)分配企業(yè)代碼
/// </summary>
private string SPID;
/// <summary>
/// 網(wǎng)關(guān)分配服務(wù)號(hào)碼
/// </summary>
private string ServiceCode;//特服號(hào)
/// <summary>
/// 登陸用戶名
/// </summary>
private string SendUserName;
/// <summary>
/// 登陸密碼
/// </summary>
private string SendPassWord;
/**************************主服務(wù)程序配置*********************************/
private string GateWayID;
private string GateWayPWD;
private string IsmgServerName;
private int IsmgServerPort;
private System.Threading.AutoResetEvent ARE_ISMGConnect=new AutoResetEvent(false);
/***************************數(shù)據(jù)庫(kù)連接配置*****************************/
private string DataBaseConnectString;
private System.Data.SqlClient.SqlConnection SqlConn;
private System.Data.SqlClient.SqlCommand SqlComm;
/***************************平臺(tái)標(biāo)志位及其他配置***********************/
private bool isStart=false;
private bool HandStop=false;
private byte[] LastData=new byte[0];
private ReaderWriterLock RWL_MSMQ_Temp=new ReaderWriterLock();
private System.Threading.AutoResetEvent ARE_Start=new AutoResetEvent(false);
private System.Threading.Thread ThreadStart;
/// <summary>
/// 運(yùn)營(yíng)商編號(hào)
/// </summary>
private int TelOperatorID;
/// <summary>
/// 整型的網(wǎng)關(guān)號(hào)
/// </summary>
private int iGatewayID;
private string path;
private ReaderWriterLock RWL_log=new ReaderWriterLock();
private System.Messaging.MessageQueue RecvSMBPQueue;
private System.Messaging.MessageQueue SendSMBPQueue;
private System.Threading.ReaderWriterLock RWL_SendSmbppQueue=new ReaderWriterLock();
//private ReaderWriterLock RWL_SendQueue=new ReaderWriterLock();
/// <summary>
/// 受系統(tǒng)是否啟動(dòng)所控制
/// </summary>
private bool IfCanSendMsg=false;
private ReaderWriterLock RWL_IfCanSendMsg=new ReaderWriterLock();
private System.Messaging.MessageQueue MSMQ_TempQueue;
private int ResendTimes=3;
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public CMPP3GateWayService()
{
// 該調(diào)用是 Windows.Forms 組件設(shè)計(jì)器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 調(diào)用后添加任何初始化
}
// 進(jìn)程的主入口點(diǎn)
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// 同一進(jìn)程中可以運(yùn)行多個(gè)用戶服務(wù)。若要將
//另一個(gè)服務(wù)添加到此進(jìn)程,請(qǐng)更改下行
// 以創(chuàng)建另一個(gè)服務(wù)對(duì)象。例如,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CMPP3GateWayService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
//
// CMPP3GateWayService
//
this.ServiceName = "CMPP3GateWayService";
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 設(shè)置具體的操作,以便服務(wù)可以執(zhí)行它的工作。
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: 在此處添加代碼以啟動(dòng)服務(wù)。
//Thread.Sleep(20000);
string queuepath=@".\Private$\SmbppSrvCmppGateWayTempQueue";
if(!System.Messaging.MessageQueue.Exists(queuepath))
{
//System.Messaging.MessageQueue.Delete(".\\Private$\\GateWayLogQueue");
System.Messaging.MessageQueue.Create(queuepath);
}
if(this.MSMQ_TempQueue!=null)
{
this.MSMQ_TempQueue.Close();
this.MSMQ_TempQueue.Dispose();
}
else
{
this.MSMQ_TempQueue=new System.Messaging.MessageQueue(queuepath);
this.MSMQ_TempQueue.Formatter=new System.Messaging.BinaryMessageFormatter(System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full, System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways);
this.MSMQ_TempQueue.ReceiveCompleted+=new System.Messaging.ReceiveCompletedEventHandler(MSMQ_TempQueue_ReceiveCompleted);
}
this.path=System.Windows.Forms.Application.StartupPath;
this.MSMQ_TempQueue.BeginReceive();
//this.RWL_log.ReleaseLock();
this.TobeStart();
}
//準(zhǔn)備啟動(dòng)
private void TobeStart()
{
this.HandStop=false;
this.ThreadStart=new Thread(new ThreadStart(this.ServiceStart));
this.ThreadStart.Start();
}
/// <summary>
/// 啟動(dòng)線程避免管理器無(wú)法控制
/// </summary>
private void ServiceStart()
{
while(!this.HandStop)
{
this.ARE_Start.Reset();
this.OneStart();
this.ARE_Start.WaitOne();
//啟動(dòng)失敗,系統(tǒng)冷卻60S
Thread.Sleep(60000);
}
}
/// <summary>
/// 進(jìn)行一次啟動(dòng)
/// </summary>
private void OneStart()
{
this.isStart=true;
string log;
bool initcon=this.InitConfig();
if(!initcon)
{
//配置信息讀取失敗
log="配置信息讀取失敗";
this.WriteLogFile(log);
this.ARE_Start.Set();
return;
}
log="配置信息讀取成功";
this.WriteLogFile(log);
#region <建立收發(fā)隊(duì)列>
string RecvPath=@".\Private$\smbppSrvService_SMBPPToGateWay_"+this.GateWayID+"_queue";
if(!System.Messaging.MessageQueue.Exists(RecvPath))
{
System.Messaging.MessageQueue.Create(RecvPath);
}
this.RecvSMBPQueue=new System.Messaging.MessageQueue(RecvPath,false);
RecvSMBPQueue.Formatter=new System.Messaging.BinaryMessageFormatter(System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full, System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways);
RecvSMBPQueue.ReceiveCompleted+=new System.Messaging.ReceiveCompletedEventHandler(RecvSMBPQueue_ReceiveCompleted);
string SendPath=@".\Private$\smbppSrvService_GateWayToSMBP_"+this.GateWayID+"_queue";
if(!System.Messaging.MessageQueue.Exists(SendPath))
{
System.Messaging.MessageQueue.Create(SendPath);
}
this.SendSMBPQueue=new System.Messaging.MessageQueue(SendPath,false);
SendSMBPQueue.Formatter=new System.Messaging.BinaryMessageFormatter(System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full, System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways);
#endregion
IPAddress GateWayIPAddress=IPAddress.Parse(this.CM_GateWayIP);
this.cmppClient=new CMPP3.Client.CMPP3Client(this.SPID,this.SendUserName,this.SendPassWord,GateWayIPAddress,this.CM_GateWayPort,this.ServiceCode);
this.cmppClient.onRequestRestartHandler+=new CMPP3.Client.RequestRestartEventHandler(cmppClient_onRequestRestartHandler);
this.cmppClient.onSentTestHandler+=new CMPP3.Client.SentTestEventHandler(cmppClient_onSentTestHandler);
this.cmppClient.onReceiveTestRespHandler+=new CMPP3.Client.RecvTestRespEventHandler(cmppClient_onReceiveTestRespHandler);
this.cmppClient.onErrorHandler+=new CMPP3.Client.ErrorEventHandler(cmppClient_onErrorHandler);
this.cmppClient.onFailedItemDeletedHandler+=new CMPP3.Client.FailedItemDeletedEventHandler(cmppClient_onFailedItemDeletedHandler);
this.cmppClient.onLoginSuccessHandler+=new CMPP3.Client.LoginSuccEventHandler(cmppClient_onLoginSuccessHandler);
this.cmppClient.onReceiveCancelRespHandler+=new CMPP3.Client.RecvCancelRespEventHandler(cmppClient_onReceiveCancelRespHandler);
this.cmppClient.onReceiveConnectRespHandler+=new CMPP3.Client.RecvConnectRespEventHandler(cmppClient_onReceiveConnectRespHandler);
this.cmppClient.onReceiveNewShortMessageHandler+=new CMPP3.Client.RecvSMSEnventHandler(cmppClient_onReceiveNewShortMessageHandler);
this.cmppClient.onReceiveQueryRespHandler+=new CMPP3.Client.RecvQueryRespEventHandler(cmppClient_onReceiveQueryRespHandler);
this.cmppClient.onReceiveReportHandler+=new CMPP3.Client.RecvReportEventHandler(cmppClient_onReceiveReportHandler);
this.cmppClient.onReceiveSubmitRespHandler+=new CMPP3.Client.RecvSubmitRespEventHandler(cmppClient_onReceiveSubmitRespHandler);
this.cmppClient.onReceiveTerminateRespHandler+=new CMPP3.Client.RecvTerminateRespEventHandler(cmppClient_onReceiveTerminateRespHandler);
this.cmppClient.onSentTerminateHander+=new CMPP3.Client.SentTerminateEventHandler(cmppClient_onSentTerminateHander);
int state=this.cmppClient.Start();
if(state!=0)
{
string _log="連接或登陸網(wǎng)關(guān)失敗,錯(cuò)誤代碼"+state.ToString();
this.WriteLogFile(_log);
this.cmppClient.Dispose();
this.RecvSMBPQueue.Close();
this.RecvSMBPQueue.Dispose();
this.SendSMBPQueue.Close();
this.SendSMBPQueue.Dispose();
this.ARE_Start.Set();
return;
}
else
{
string _log="登陸網(wǎng)關(guān)成功!";
this.WriteLogFile(_log);
}
this.SetIfCanSendMsg(true);
this.RecvSMBPQueue.BeginReceive();
}
private void SetIfCanSendMsg(bool b)
{
this.RWL_IfCanSendMsg.AcquireWriterLock(-1);
this.IfCanSendMsg=b;
this.RWL_IfCanSendMsg.ReleaseWriterLock();
Thread.Sleep(1);
}
private bool InitConfig()
{
//bool b=true;
string log="開始讀取配置信息";
lock(this)
{
this.WriteLogFile(log);
//this.richTextBox1.Text+=DateTime.Now.ToLongTimeString()+" "+log+"\r\n";
}
ReadConfig rc=new ReadConfig(this.path+"\\config.xml");
this.GateWayID=rc.GetValueByName("GateWay","GateWayID");
this.GateWayPWD=rc.GetValueByName("GateWay","GateWayPWD");
this.IsmgServerName=rc.GetValueByName("GateWay","IsmgServerName");
string _IsmgServerPort=rc.GetValueByName("GateWay","IsmgServerPort");
if(this.GateWayID==null||this.GateWayPWD==null||this.IsmgServerName==null)
{
log="讀取配置失敗";
lock(this)
{
this.WriteLogFile(log);
//this.richTextBox1.Text+=DateTime.Now.ToLongTimeString()+" "+log+"\r\n";
}
return false;
}
try
{
this.IsmgServerPort=int.Parse(_IsmgServerPort);
}
catch
{
log="讀取的IsmgServerPort不合法";
lock(this)
{
this.WriteLogFile(log);
//this.richTextBox1.Text+=DateTime.Now.ToLongTimeString()+" "+log+"\r\n";
}
return false;
}
try
{
this.iGatewayID=int.Parse(GateWayID);
}
catch
{
log="讀取的GateWayID不合法";
lock(this)
{
this.WriteLogFile(log);
//this.richTextBox1.Text+=DateTime.Now.ToLongTimeString()+" "+log+"\r\n";
}
return false;
}
string dbip=rc.GetValueByName("DataBase","server");
string userid=rc.GetValueByName("DataBase","UserID");
string psw=rc.GetValueByName("DataBase","PassWord");
this.DataBaseConnectString="server="+dbip+";database=Smbp_JDG;user id="+userid+";password="+psw+";";
this.SqlConn=new SqlConnection(this.DataBaseConnectString);
this.SqlComm=new SqlCommand();
this.SqlComm.Connection=this.SqlConn;
bool b=true;
try
{
this.SqlConn.Open();
//
}
catch
{
}
finally
{
if(this.SqlConn.State==ConnectionState.Open)
{
this.SqlConn.Close();
string sqltxt="select * from Smbp_Gateway where GateWayID=@gw";
SqlDataAdapter sda=new SqlDataAdapter(sqltxt,this.SqlConn);
sda.SelectCommand.Parameters.Add("@gw",this.iGatewayID);
DataSet ds=new DataSet();
sda.Fill(ds);
if(ds.Tables[0].Rows.Count==0)
{
log="沒有找到此網(wǎng)關(guān)的配置";
lock(this)
{
this.WriteLogFile(log);
//this.richTextBox1.Text+=DateTime.Now.ToLongTimeString()+" "+log+"\r\n";
}
b=false;
}
else
{
try
{
this.SPID=(string)ds.Tables[0].Rows[0]["SPID"];
this.ServiceCode=(string)ds.Tables[0].Rows[0]["ServiceCode"];
this.SendUserName=(string)ds.Tables[0].Rows[0]["SendUserName"];
this.SendPassWord=(string)ds.Tables[0].Rows[0]["SendPassWord"];
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -