📄 smppclient.cs.svn-base
字号:
/*
* EasySMPP - SMPP protocol library for fast and easy
* SMSC(Short Message Service Centre) client development
* even for non-telecom guys.
*
* Easy to use classes covers all needed functionality
* for SMS applications developers and Content Providers.
*
* 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.opensource.org/licenses/lgpl-license.php
*
* For further information visit:
* http://easysmpp.sf.net/
*
*
* "Support Open Source software. What about a donation today?"
*
*
* File Name: SMPPClient.cs
*
* File Authors:
* Balan Name, http://balan.name
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using EasySMPP;
namespace EasySMPP
{
/// <summary>
/// Summary description for SMPPClient.
/// </summary>
public class SMPPClient
{
#region Private variables
private Socket clientSocket;
private int connectionState;
private DateTime enquireLinkSendTime;
private DateTime enquireLinkResponseTime;
private DateTime lastSeenConnected;
private DateTime lastPacketSentTime;
private Timer enquireLinkTimer;
private int undeliveredMessages = 0;
private SMSCArray smscArray = new SMSCArray();
private byte[] mbResponse = new byte[KernelParameters.MaxBufferSize];
private int mPos = 0;
private int mLen = 0;
private bool mustBeConnected;
private int logLevel = 0;
private byte askDeliveryReceipt = KernelParameters.AskDeliveryReceipt;
private bool splitLongText = KernelParameters.SplitLongText;
private int nationalNumberLength = KernelParameters.NationalNumberLength;
private bool useEnquireLink = KernelParameters.UseEnquireLink;
private int enquireLinkTimeout = KernelParameters.EnquireLinkTimeout;
private int reconnectTimeout = KernelParameters.ReconnectTimeout;
private SortedList sarMessages = SortedList.Synchronized(new SortedList());
#endregion Private variables
#region Public Functions
public SMPPClient()
{
connectionState= ConnectionStates.SMPP_SOCKET_DISCONNECTED;
enquireLinkSendTime = DateTime.Now;
enquireLinkResponseTime = enquireLinkSendTime.AddSeconds(1);
lastSeenConnected = DateTime.Now;
lastPacketSentTime = DateTime.MaxValue;
mustBeConnected = false;
TimerCallback timerDelegate = new TimerCallback(checkSystemIntegrity);
enquireLinkTimer = new Timer(timerDelegate, null, enquireLinkTimeout, enquireLinkTimeout);
}//SMPPClient
public void Connect()
{
try
{
mustBeConnected = true;
connectToSMSC();
}
catch (Exception ex)
{
logMessage(LogLevels.LogExceptions, "connectToSMSC | " + ex.ToString());
}
}//connectToSMSC
public void Disconnect()
{
try
{
mustBeConnected = false;
unBind();
disconnectSocket();
}
catch (Exception ex)
{
logMessage(LogLevels.LogExceptions, "DisconnectFromSMSC | " + ex.ToString());
}
}//DisconnectFromSMSC
public void ClearSMSC()
{
try
{
smscArray.Clear();
}
catch (Exception ex)
{
logMessage(LogLevels.LogExceptions, "AddSMSC | " + ex.ToString());
}
}//ClearSMSC
public void AddSMSC(SMSC mSMSC)
{
try
{
smscArray.AddSMSC(mSMSC);
}
catch (Exception ex)
{
logMessage(LogLevels.LogExceptions, "AddSMSC | " + ex.ToString());
}
}//AddSMSC
#region Send Functions
public void Send(byte[] data, int n)
{
try
{
lastPacketSentTime = DateTime.Now;
logMessage(LogLevels.LogPdu, "Sending PDU : " + Tools.ConvertArrayToHexString(data, n));
clientSocket.BeginSend(data, 0, n, 0, new AsyncCallback(sendCallback), clientSocket);
}
catch (Exception ex)
{
logMessage(LogLevels.LogExceptions, "Send | " + ex.ToString());
}
}//Send
public int SubmitSM(byte sourceAddressTon, byte sourceAddressNpi, string sourceAddress,
byte destinationAddressTon, byte destinationAddressNpi, string destinationAddress,
byte esmClass, byte protocolId, byte priorityFlag,
DateTime sheduleDeliveryTime, DateTime validityPeriod, byte registeredDelivery,
byte replaceIfPresentFlag, byte dataCoding, byte smDefaultMsgId,
byte[] message)
{
try
{
byte[] _destination_addr;
byte[] _source_addr;
byte[] _SUBMIT_SM_PDU;
byte[] _shedule_delivery_time;
byte[] _validity_period;
int _sequence_number;
int pos;
byte _sm_length;
_SUBMIT_SM_PDU = new byte[KernelParameters.MaxPduSize];
////////////////////////////////////////////////////////////////////////////////////////////////
/// Start filling PDU
Tools.CopyIntToArray(0x00000004, _SUBMIT_SM_PDU, 4);
_sequence_number = smscArray.currentSMSC.SequenceNumber;
Tools.CopyIntToArray(_sequence_number, _SUBMIT_SM_PDU, 12);
pos = 16;
_SUBMIT_SM_PDU[pos] = 0x00; //service_type
pos += 1;
_SUBMIT_SM_PDU[pos] = sourceAddressTon;
pos += 1;
_SUBMIT_SM_PDU[pos] = sourceAddressNpi;
pos += 1;
_source_addr = Tools.ConvertStringToByteArray(Tools.GetString(sourceAddress, 20, ""));
Array.Copy(_source_addr, 0, _SUBMIT_SM_PDU, pos, _source_addr.Length);
pos += _source_addr.Length;
_SUBMIT_SM_PDU[pos] = 0x00;
pos += 1;
_SUBMIT_SM_PDU[pos] = destinationAddressTon;
pos += 1;
_SUBMIT_SM_PDU[pos] = destinationAddressNpi;
pos += 1;
_destination_addr = Tools.ConvertStringToByteArray(Tools.GetString(destinationAddress, 20, ""));
Array.Copy(_destination_addr, 0, _SUBMIT_SM_PDU, pos, _destination_addr.Length);
pos += _destination_addr.Length;
_SUBMIT_SM_PDU[pos] = 0x00;
pos += 1;
_SUBMIT_SM_PDU[pos] = esmClass;
pos += 1;
_SUBMIT_SM_PDU[pos] = protocolId;
pos += 1;
_SUBMIT_SM_PDU[pos] = priorityFlag;
pos += 1;
_shedule_delivery_time = Tools.ConvertStringToByteArray(Tools.GetDateString(sheduleDeliveryTime));
Array.Copy(_shedule_delivery_time, 0, _SUBMIT_SM_PDU, pos, _shedule_delivery_time.Length);
pos += _shedule_delivery_time.Length;
_SUBMIT_SM_PDU[pos] = 0x00;
pos += 1;
_validity_period = Tools.ConvertStringToByteArray(Tools.GetDateString(validityPeriod));
Array.Copy(_validity_period, 0, _SUBMIT_SM_PDU, pos, _validity_period.Length);
pos += _validity_period.Length;
_SUBMIT_SM_PDU[pos] = 0x00;
pos += 1;
_SUBMIT_SM_PDU[pos] = registeredDelivery;
pos += 1;
_SUBMIT_SM_PDU[pos] = replaceIfPresentFlag;
pos += 1;
_SUBMIT_SM_PDU[pos] = dataCoding;
pos += 1;
_SUBMIT_SM_PDU[pos] = smDefaultMsgId;
pos += 1;
_sm_length = message.Length > 254 ? (byte)254 : (byte)message.Length;
_SUBMIT_SM_PDU[pos] = _sm_length;
pos += 1;
Array.Copy(message, 0, _SUBMIT_SM_PDU, pos, _sm_length);
pos += _sm_length;
Tools.CopyIntToArray(pos, _SUBMIT_SM_PDU, 0);
Send(_SUBMIT_SM_PDU, pos);
undeliveredMessages++;
return _sequence_number;
}
catch (Exception ex)
{
logMessage(LogLevels.LogExceptions, "SubmitSM | " + ex.ToString());
}
return -1;
}//SubmitSM
public int DataSM(byte sourceAddressTon, byte sourceAddressNpi, string sourceAddress,
byte destinationAddressTon, byte destinationAddressNpi, string destinationAddress,
byte esmClass,
byte registeredDelivery,
byte dataCoding,
byte[] data)
{
try
{
byte[] _destination_addr;
byte[] _source_addr;
byte[] _DATA_SM_PDU;
int _sequence_number;
int pos;
Int16 _sm_length;
_DATA_SM_PDU = new byte[KernelParameters.MaxPduSize];
////////////////////////////////////////////////////////////////////////////////////////////////
/// Start filling PDU
Tools.CopyIntToArray(0x00000103, _DATA_SM_PDU, 4);
_sequence_number = smscArray.currentSMSC.SequenceNumber;
Tools.CopyIntToArray(_sequence_number, _DATA_SM_PDU, 12);
pos = 16;
_DATA_SM_PDU[pos] = 0x00; //service_type
pos += 1;
_DATA_SM_PDU[pos] = sourceAddressTon;
pos += 1;
_DATA_SM_PDU[pos] = sourceAddressNpi;
pos += 1;
_source_addr = Tools.ConvertStringToByteArray(Tools.GetString(sourceAddress, 20, ""));
Array.Copy(_source_addr, 0, _DATA_SM_PDU, pos, _source_addr.Length);
pos += _source_addr.Length;
_DATA_SM_PDU[pos] = 0x00;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -