⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 global.cs.svn-base

📁 sms短信发送方面的东东。仅供参考使用。有问题可以和我联系
💻 SVN-BASE
字号:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
using etmc.sms.smpapi;

namespace PlaneSale
{
    class Global
    {
        public static Form1 form = null;
        public static UdpClient client = null;
        public static XmlDocument doc = null;
        public static Queue RecQueue = new Queue();
        public static Queue SendQueue = new Queue();
        public static SqlConnection conn = new SqlConnection();
        public static string BeginTime = null;
        public static string EndTime = null;
        public static string WarnTime = null;
        public static string FeeCode = null;

        public static int DOProcessTag = 1;

        private int LocalPort = 0;
        private int FreePort = 0;
        private int FeePort = 0;
        private string FreeAddr = null;
        private string FeeAddr = null;
         
        private string Addr = null;
        private string User = null;
        private string Password = null;
        private string DB = null;

        private Thread ListenSocketThread = null;
        private IPEndPoint recPoint = null;
        private FileStream fs = null;
        
        private Functions func = new Functions();
        private SmpApi smp = new SmpApi();
        private DBOperator db = new DBOperator();

        public Global()
        {
            recPoint = new IPEndPoint(IPAddress.Any, LocalPort);   
        }
       
        /// <summary>
        /// 完成系统初始化
        /// </summary>
        public void Init()
        {
            InitConfig();
            initDBConn();
            InitSocket();
            InitThread();
            func.StartAllTimer();
        }
        
        
        /// <summary>
        /// 释放系统资源
        /// </summary>
        public void StopAll()
        {
            try
            {
                if ( ListenSocketThread.IsAlive)
                    ListenSocketThread.Abort();
                if (client != null)
                    client.Close();
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }
        /// <summary>
        /// 初始化配置文件
        /// </summary>
        public void InitConfig()
        {
            try
            {
                string configFile = form.GetAppPath() + "\\config.xml";
                doc = new XmlDocument();
                fs = new FileStream(configFile, FileMode.Open);
                doc.Load(fs);
                form.AddMessage("加载配置文件成功!");
                XmlNodeList list = null;
                list = doc.GetElementsByTagName("LocalPort");
                LocalPort = Convert.ToInt32(list[0].ChildNodes[0].Value);
                list = doc.GetElementsByTagName("FreePort");
                FreePort = Convert.ToInt32(list[0].ChildNodes[0].Value);
                list = doc.GetElementsByTagName("FeePort");
                FeePort = Convert.ToInt32(list[0].ChildNodes[0].Value);
                list = doc.GetElementsByTagName("FreeAddr");
                FreeAddr = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("FeeAddr");
                FeeAddr = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("Addr");
                Addr = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("User");
                User = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("Password");
                Password = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("DB");
                DB = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("BeginTime");
                BeginTime = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("EndTime");
                EndTime = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("WarnTime");
                WarnTime = list[0].ChildNodes[0].Value;
                list = doc.GetElementsByTagName("FeeCode");
                FeeCode = list[0].ChildNodes[0].Value;
            }
            catch (System.Exception e)
            {
            	form.AddMessage(e.ToString());
            }
            finally
            {
                fs.Close();
            }
        }
        
        /// <summary>
        /// 初始化全局SOCKET
        /// </summary>
        public void InitSocket()
        {
            try
            {
                client = new UdpClient(LocalPort);
                form.AddMessage(string.Format("绑定端口:{0}成功!", LocalPort.ToString()));
            }catch(Exception ex)
            {
                form.AddMessage(ex.ToString());
            }
        }
        
        /// <summary>
        /// 线程初始化
        /// </summary>
        public void InitThread()
        {
            try
            {
                ListenSocketThread = new Thread(new ThreadStart(ListenSocket));
                ListenSocketThread.Start(); 
                form.AddMessage("启动线程:ListenSocketThread成功");
            }catch(Exception ex)
            {
                form.AddMessage("启动线程:ListenSocketThread失败");
            }
            
        }
        
        /// <summary>
        /// 接受数据的方法 
        /// </summary>
        private void ListenSocket()
        {
            while(true)
            {
                byte[] buf = new byte[1024];
                buf = client.Receive(ref recPoint);
                string msg = Encoding.Default.GetString(buf);
                RecQueue.Enqueue(msg);
                form.AddMessage(string.Format("收到消息:{0}",msg));
                ProcessRecQueue();
            }
        }

        /// <summary>
        /// 初始化数据库连接
        /// </summary>
        public void initDBConn()
        {
            try
            {
                string connString = "server=" + Addr + "; User ID=" + User + ";password=" + Password + ";database=" + DB;
                conn.ConnectionString = connString;
                conn.Open();
                form.AddMessage(string.Format("连接数据库:{0}成功",connString));
            }
            catch (System.Exception e)
            {
                form.AddMessage(string.Format("连接数据库失败:{0}", e));
            }
        }
        
        /// <summary>
        /// 处理队列消息
        /// </summary>
        private void ProcessRecQueue()
        {
            if (RecQueue.Count > 0)
            {
                string msg = (string) RecQueue.Dequeue();
                if (DOProcessTag == 1)
                {
                    DoProcess(msg);
                }
                else 
                    DoBeforeAndAfter(smp.getSmp023(msg));
            }
        }
        
        /// <summary>
        /// 活动还未开始时收到用户消息的处理方式
        /// </summary>
        /// <param name="mobile"></param>
        private void DoBeforeAndAfter(string mobile)
        {
            string str = null;
            if(DOProcessTag == 0)
                str= func.GetMessage("BeforMessage");
            else
                str = func.GetMessage("AfterMessage");
           SendFreeMessage(mobile,str);
        }
        
        /// <summary>
        /// 处理活动的方法
        /// </summary>
        /// <param name="msg"></param>
        private void DoProcess(string msg)
        {
            string input = smp.getSmp032(msg).ToUpper().Trim();
            string mobile = smp.getSmp023(msg).Trim();
            string price = db.GetHighPriceByCode(input);
            string respMessage = null;
            if (price == null)
            {
                price = func.GetMessageByCode("BeginPrice", input);
                if (price == null)
                {
                    SendFreeMessage(mobile, func.GetMessage("CodeError"));
                }
                else
                {
                    respMessage = PackResponseMessage("HighPriceResponse", input, out price);
                    //SendFeeMessage(mobile, respMessage);
                }
            }
            else
            {
                respMessage = PackResponseMessage("HighPriceResponse", input, out price);
            }
            string highestPrice = func.GetMessageByCode("HighestPrice", input);
            //达到最高价时
            if (Convert.ToInt32(price) == Convert.ToInt32(highestPrice) || (Convert.ToInt32(price) + Convert.ToInt32(func.GetMessageByCode("OncePrice", input))) >= Convert.ToInt32(highestPrice))
            {
                SendFeeMessage(mobile, respMessage);
                AllLoopPeopleSend(input,price);
                db.AddmessageToDB(mobile, input, price);
                db.UpdateLoopState(input);
            }
            //没有到最高价时
            else
            {
                SendFeeMessage(mobile, respMessage);
                MulitSend(input, price);
                db.AddmessageToDB(mobile, input, price);
            }
            
        }
        
        /// <summary>
        /// 发送免费消息给用户
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="msg"></param>
        private void SendFreeMessage(string mobile,string msg)
        {
            smp.setSmp021(mobile);
            smp.setSmp032(msg);
            byte[] buf = Encoding.Default.GetBytes(smp.packSmp());
            client.Send(buf, buf.Length, func.GetMessage("FreeAddr"), Convert.ToInt32(func.GetMessage("FreePort")));
            form.AddMessage(string.Format("发送消息{0}到:{1},{2}", smp.packSmp(), FreeAddr, FreePort));
        }
        
        /// <summary>
        /// 发送收费信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="msg"></param>
        private void SendFeeMessage(string mobile,string msg)
        {
            smp.setSmp021(mobile);
            smp.setSmp023("82575060");
            smp.setSmp032(msg);
            smp.setSmp083(func.GetMessage("FeeCode"));
            byte[] buf = Encoding.Default.GetBytes(smp.packSmp());
           // client.Send(buf, buf.Length, FeeAddr, FeePort);
            client.Send(buf, buf.Length, func.GetMessage("FeeAddr"), Convert.ToInt32(func.GetMessage("FeePort")));
            form.AddMessage(string.Format("发送消息{0}到:{1},{2}", smp.packSmp(), FeeAddr, FeePort));
        }
        
        /// <summary>
        /// 完成032字段的内容
        /// </summary>
        /// <param name="key"></param>
        /// <param name="code"></param>
        /// <param name="CurPrice"></param>
        /// <returns></returns>
        private string PackResponseMessage(string key,string code,out string CurPrice)
        {
            string msg = func.GetMessageByCode(key, code);
            string price = null;
            if (msg.IndexOf("$highestmoney") >= 0)
            {
                if (db.GetHighPriceByCode(code) != null)
                {
                    int i_price = Convert.ToInt32(db.GetHighPriceByCode(code)) +
                                  Convert.ToInt32(func.GetMessageByCode("OncePrice", code));
                    price = i_price.ToString();
                }
                else if (func.GetMessageByCode("BeginPrice", code) != null)
                    price = func.GetMessageByCode("BeginPrice", code);
                msg = msg.Replace("$highestmoney", price);
            }
            CurPrice = price; 
            return msg;
        }
        
        /// <summary>
        /// 群发通知给指定数目的用户
        /// </summary>
        /// <param name="code"></param>
        private void MulitSend(string code,string price)
        {
            string msg = func.GetMessageByCode("MultiMessage", code);
            msg = msg.Replace("$highestmoney", price);
            ArrayList list = db.GetNearhighPricePeople(code);
            for(int j=0;j<list.Count;j++)
            {
                SendFreeMessage((string)list[j], msg); 
            }
        }


        /// <summary>
        /// 当前竞拍结束时,给所有参与竞拍的用户发送通知
        /// </summary>
        /// <param name="code"></param>
        /// <param name="price"></param>
        private void AllLoopPeopleSend(string code, string price)
        {
            string msg = func.GetMessageByCode("SendMessageAll", code);
            msg = msg.Replace("$highestmoney", price);
            ArrayList list = db.GetCurLoopPeople(code);
            for (int j = 0; j < list.Count; j++)
            {
                SendFreeMessage((string)list[j], msg);
            }
        }
        
        /// <summary>
        /// 预警时处理
        /// </summary>
        public void WarnDo()
        {
            //Global g = new Global();
            Regex regex = new Regex(@"\,");
            string[] codeList = regex.Split(func.GetMessage("CodeList"));
            string[] WarnReceiver = regex.Split(func.GetMessage("WarnPeople"));
            int WarnPrice = 0;
            int CurPrice = 0;
            for (int i = 0; i < codeList.Length; i++)
            {
                WarnPrice = Convert.ToInt32(func.GetMessageByCode("WarnPrice", codeList[i]));
                CurPrice = Convert.ToInt32(db.GetHighPriceByCode(codeList[i]));
                if (CurPrice <= WarnPrice)
                {
                    string warnMessage = func.GetMessage("WarnMessage");
                    warnMessage = warnMessage.Replace("$code", codeList[i]);
                    warnMessage = warnMessage.Replace("$highestmoney", CurPrice.ToString());
                    for(int j=0;j<WarnReceiver.Length;j++)
                    {
                        SendFreeMessage(WarnReceiver[j],warnMessage);
                    }
                }

            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public void EndDo()
        {
            Regex regex = new Regex(@"\,");
            string[] codeList = regex.Split(func.GetMessage("CodeList"));
            for (int i=0;i < codeList.Length;i++)
            {
                AllLoopPeopleSend(codeList[i],db.GetHighPriceByCode(codeList[i]));
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -