中國移動簡訊網關CMPP3.0 C#原始碼:事件定義及相關工具函數

來源:互聯網
上載者:User
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections;
using System.Diagnostics;
namespace Tiray.SMS
{
 //事件參數定義
 public class SMSEventArgs:EventArgs
 {
  SMS_STATE m_State;
  Object m_Data;
  DateTime m_dtTime;

  public SMS_STATE State
  {
   get{return m_State;}
   set{m_State=value;}
  }

  public object Data
  {
   get{return m_Data;}
   set{m_Data=value;}
  }
  public DateTime Time
  {
   get{return m_dtTime;}
   set{m_dtTime=value;}
  }

 }
 //事件處理函數
 public delegate void SMSEventHandler(object sender,SMSEventArgs e);
 
 //非同步事件回呼函數
 public delegate void SMSAsyncEvent(SMSEventArgs e);

 public class Utility
 {
  public static String Decode(Byte[] buf,int StartIndex,int Length,CODING Coding)
  {
   String str=String.Empty;
   if(Coding==CODING.ASCII)
    str=System.Text.Encoding.ASCII.GetString(buf,StartIndex,Length);
   
   else if(Coding==CODING.UCS2)
    str=System.Text.Encoding.BigEndianUnicode.GetString(buf,0,Length);
   else if(Coding==CODING.GBK)
    str=System.Text.UnicodeEncoding.GetEncoding("gb2312").GetString(buf,StartIndex,Length);
   return str;
  }
  public static Byte[] Encode(String str,CODING coding)
  {
   Byte[] buf=null;
   if(str==null)
    return buf;
   if(coding==CODING.ASCII)
    buf=System.Text.Encoding.ASCII.GetBytes(str);
   else if(coding==CODING.UCS2)
    buf=System.Text.Encoding.BigEndianUnicode.GetBytes(str);
   else if(coding==CODING.GBK)
    buf=System.Text.UnicodeEncoding.GetEncoding("gb2312").GetBytes(str);

   return buf;

  }
  public static UInt32 CountLength(String str,CODING coding)
  {
   Byte [] buf=Encode(str,coding);
   if(buf!=null)
    return (UInt32)buf.Length;
   else
    return 0;
  }

  public static Byte[] IntToNetBytes(object obj)
  {
   Byte[] bytes=null;
   if(obj.GetType()==System.Type.GetType("System.UInt32"))
   {
    UInt32 val=(UInt32) obj;
    bytes=BitConverter.GetBytes(val);
   }
   if(obj.GetType()==System.Type.GetType("System.UInt64"))
   {
    UInt64 val=(UInt64) obj;
    bytes=BitConverter.GetBytes(val);
   }

   if(bytes!=null)
    System.Array.Reverse(bytes);

   return bytes;

  }
  public static object NetBytesToInt(Byte[] bytes,int index,int length)
  {
   Array.Reverse(bytes,index,length);
   if(length==4)
    return BitConverter.ToUInt32(bytes,index);
   else if(length==8)
    return BitConverter.ToUInt64(bytes,index);
   else
    return 0;
  }
 }

}

【待續】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.