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

📄 smsdispatcher.cs

📁 Easy to use SMS/MMS Messaging Gateway to develop Content Delivery Platforms by GSM Operators,Content
💻 CS
字号:
/*
 * Easy Messaging Gateway (SMS/MMS/Mail) - Easy to use SMS/MMS Messaging Gateway
 * to develop Content Delivery Platforms by GSM Operators,Content Providers
 * and even non-telecom guys. 
 * 
 * Uses file system for data flow. Protocol level is based on Easy SMPP and Easy MM7 projects.
 * 
 * Written for .NET 2.0 in C# 
 * Copyright (C) 2006 Balan Andrei, http://balan.name
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.gnu.org/licenses/lgpl.html
 * 
 * For further information visit:
 * 		http://easymessaging.sf.net/
 * 
 * 
 * "Support Open Source software. What about a donation today?"
 *
 * 
 * File Name: InboxWatcher.cs
 * 
 * File Authors:
 *      Egoricev Serghei, egoricev@users.sourceforge.net
 * 		Balan Name, http://balan.name
 *      
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;

using EasySMPP;


namespace EasyMessaging.SmsGateway
{
    class SmsDispatcher
    {
        private class AliasNotFoundException : Exception
        {
            public AliasNotFoundException(string message)
                : base(message)
            {
            }
        }

        private Thread thread;
        private bool working;
        private AutoResetEvent waiter;
        private string inboxPath;
        private string badPath;
        private string sentPath;
        private string outboxPath;

        private SmsClient smsClient;

        public SmsDispatcher()
        {
            inboxPath = Path.Combine(Path.GetFullPath(Settings.Default.toSmsc), "inbox");
            badPath = Path.Combine(Path.GetFullPath(Settings.Default.toSmsc), "bad");
            sentPath = Path.Combine(Path.GetFullPath(Settings.Default.toSmsc), "sent");
            outboxPath = Path.Combine(Path.GetFullPath(Settings.Default.fromSmsc), "inbox");

            smsClient = new SmsClient();
            smsClient.OnNewSms += new NewSmsEventHandler(onNewSms);
        }

        public void Wake()
        {
            waiter.Set();
        }


        public void Start()
        {
            smsClient.Connect();
            waiter = new AutoResetEvent(false);
            working=true;
            thread = new Thread(new ThreadStart(main));
            thread.Start();
        }

        private void main()
        {
            while(working)
            {
                try
                {
                    checkInbox();
                    waiter.WaitOne(10000, false);
                } // try
                catch(Exception ex)
                {
#if DEBUG
                    Console.WriteLine(ex.Message);
#endif
                } // catch
           } // while
        }

        private void onNewSms(NewSmsEventArgs args)
        {
            XmlDocument xml = new XmlDocument();
            XmlElement sms = xml.CreateElement("sms");
            xml.AppendChild(sms);
            XmlElement element;
            string id = Guid.NewGuid().ToString();

            element = xml.CreateElement("id");
            element.InnerText = id;
            sms.AppendChild(element);

            element = xml.CreateElement("language");
            element.InnerText = getLanguage(args.From);
            sms.AppendChild(element);

            element = xml.CreateElement("source");
            element.InnerText = args.From;
            sms.AppendChild(element);

            element = xml.CreateElement("destination");
            element.InnerText = args.To;
            sms.AppendChild(element);

            element = xml.CreateElement("text");
            element.InnerText = args.Text;
            sms.AppendChild(element);

            xml.Save(Path.Combine(outboxPath,id));
        }
        
        private void checkInbox()
        {
            string[] files = Directory.GetFiles(inboxPath);
            foreach (string file in files)
                if (working)
                    processFile(file);
                else
                    break;
        }

        private void processFile(string file)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                XmlNode node;
                xml.Load(file);
                string source;
                string destination;
                string text;
                node = xml.DocumentElement.SelectSingleNode("source");
                if (node == null)
                {
                    moveToBad(file);
                    return;
                }
                else
                {
                    source = node.InnerText;
                }
                node = xml.DocumentElement.SelectSingleNode("destination");
                if (node == null)
                {
                    moveToBad(file);
                    return;
                }
                else
                {
                    destination = node.InnerText;
                }
                node = xml.DocumentElement.SelectSingleNode("text");
                if (node == null)
                {
                    moveToBad(file);
                    return;
                }
                else
                {
                    text = node.InnerText;
                }
                if (smsClient.SendSms(source, destination, text))
                    moveToSent(file);
            }
            catch
            {
                moveToBad(file);
            }
        }

        private void moveToBad(string file)
        {
            if (File.Exists(Path.Combine(badPath, Path.GetFileName(file))))
                File.Move(file, Path.Combine(badPath, Path.GetFileName(file) + "-" + Guid.NewGuid().ToString()));
            else
                File.Move(file, Path.Combine(badPath, Path.GetFileName(file)));
        }

        private void moveToSent(string file)
        {
            
            string datePath = Path.Combine(sentPath, DateTime.Now.ToString("yyyyMMdd"));
            if (!Directory.Exists(datePath))
                Directory.CreateDirectory(datePath);
            string hourPath = Path.Combine(datePath, DateTime.Now.ToString("HH"));
            if (!Directory.Exists(hourPath))
                Directory.CreateDirectory(hourPath);
            if (File.Exists(Path.Combine(hourPath, Path.GetFileName(file))))
                File.Move(file, Path.Combine(hourPath, Path.GetFileName(file)+"-"+Guid.NewGuid().ToString()));
            else
                File.Move(file, Path.Combine(hourPath, Path.GetFileName(file)));
        }

        private string getLanguage(string msisdn)
        {
            string language = "en";
            if (Settings.Default.useLanguageUrl)
            {
                try
                {
                    HttpWebRequest req = HttpWebRequest.Create(string.Format(Settings.Default.languageUrl, msisdn)) as HttpWebRequest;
                    using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
                    using (StreamReader sr = new StreamReader(res.GetResponseStream()))
                        language = sr.ReadToEnd();
                    language = Regex.Replace(language, @"\s+|<.+?>", "", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    if (string.IsNullOrEmpty(language) || language == "xx")
                        language = "en";
                } // try
                catch
                {
                } // catch
            }
            return language;
        }

        public void Stop()
        {
            working = false;
            Wake();
            thread.Join();
            waiter.Close();
            smsClient.Disconnect();
        }

    }
}

⌨️ 快捷键说明

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