China Mobile SMS gateway cmpp3.0 C # source code: cmpp30 class (2)

Source: Internet
Author: User
Protected cmpp_head readhead ()
{
Cmpp_head head = new cmpp_head ();
Head. commandid = 0;
Byte [] buffer = new byte [12];
Try
{
If (m_networkstream.dataavailable)
{
M_networkstream.read (buffer, 0, buffer. Length );
Head. totallength = (uint32) Utility. netbytestoint (buffer, 0, 4 );
Head. commandid = (uint32) Utility. netbytestoint (buffer, 4, 4 );
Head. sequenceid = (uint32) Utility. netbytestoint (buffer, 8, 4 );
}

}
Catch
{
Head. commandid = cmd_error;
}
Return head;
}

Protected cmpp_connect_resp readconnectresp (cmpp_head head)
{
Cmpp_connect_resp resp = new cmpp_connect_resp ();
Resp. Head = head;
Try
{
If (m_networkstream.dataavailable)
{
Byte [] buffer = new byte [resp. Head. TotalLength-Marshal.SizeOf (resp. Head)];
M_networkstream.read (buffer, 0, buffer. Length );
Resp. Status = (uint32) Utility. netbytestoint (buffer, 0, 4 );
Resp. authenticatorismg = new byte [16];
Array. Copy (buffer, 4, resp. authenticatorismg, 0, 16 );
Resp. Version = buffer [buffer. Length-1];
}
}
Catch
{
Resp. Head. commandid = pai_error;
}

Return resp;


}
Protected cmpp_submit_resp readsubmitresp (cmpp_head head)
{
Cmpp_submit_resp resp = new cmpp_submit_resp ();
Resp. Head = head;
String strerror = string. empty;
Bool Bok = true;
Try
{
If (m_networkstream.dataavailable)
{
Byte [] buffer = new byte [resp. Head. TotalLength-Marshal.SizeOf (resp. Head)];
M_networkstream.read (buffer, 0, buffer. Length );
// Resp. msgid = (uint64) Utility. netbytestoint (buffer, 0, 8 );
Resp. msg_id = (uint64) bitconverter. touint64 (buffer, 0 );
Resp. Result = (uint32) Utility. netbytestoint (buffer, 8, 4 );
Monitor. Enter (slidingwindow );
For (INT I = 0; I <m_islidingwindowsize; I ++)
{
If (slidingwindow [I]. Status = 1) & // sent, waiting for response
(Sshortingwindow [I]. sequenceid = resp. Head. sequenceid)
(Slidingwindow [I]. Command = cmd_submit) // It is submit.
{
Slidingwindow [I]. Status = 0; // clear the window
Break;
}
}
Monitor. Exit (slidingwindow );
}

}
Catch (exception ex)
{
Resp. Head. commandid = pai_error;
Strerror = ex. message;
Bok = false;
}
If (Bok)
Raiseevent (sms_state.submit_response, resp );
Else
Raiseevent (sms_state.submit_response_error, strerror );
Return resp;

}

Protected cmpp_deliver readdeliver (cmpp_head head)
{
Cmpp_deliver deliver = new cmpp_deliver ();
Deliver. Head = head;
String strerror = string. empty;
Try
{
If (m_networkstream.dataavailable)
{
Byte [] buffer = new byte [deliver. Head. TotalLength-Marshal.SizeOf (deliver. Head)];
M_networkstream.read (buffer, 0, buffer. Length );
Deliver. INIT (buffer );
Deliverresponse (deliver. Head. sequenceid, deliver. msg_id, 0 );
}
}
Catch (exception ex)
{
Deliver. Head. commandid = pai_error;
Strerror = ex. message;
}
If (deliver. Head. commandid = pai_deliver) & (deliver. registered_delivery = 0) // is a short message
{
Raiseevent (sms_state.deliver, deliver );
}
Else if (deliver. Head. commandid = pai_deliver) & (deliver. registered_delivery = 1) /// status report
{
Raiseevent (sms_state.report, deliver );
}
Else // Error
{
Raiseevent (sms_state.deliver_error, strerror );
}
Return deliver;

}

Protected bool deliverresponse (uint32 sequenceid, uint64 msg_id, uint32 result)
{
Bool Bok = true;
String strerror = string. empty;
Cmpp_deliver_resp resp = new cmpp_deliver_resp ();
Resp. Head = new cmpp_head ();
Resp. Head. commandid = cmpp30.cmd _ deliver_resp;
Resp. Head. sequenceid = sequenceid;
Resp. msg_id = msg_id;
Resp. Result = result;

Data_package dp = new data_package ();
DP. sequenceid = resp. Head. sequenceid;
DP. Command = resp. Head. commandid;
DP. sendcount = 0;
DP. Data = resp;
DP. Status = 1;
 
Monitor. Enter (m_messagequeue );
M_messagequeue.enqueue (DP );
Monitor. Exit (m_messagequeue );

Return Bok;
}

Protected bool activetest ()
{
Bool Bok = true;
Timespan Ts = datetime. Now-m_dtLastTransferTime;
If (TS. totalseconds> m_iactivetestspan)
{
Cmpp_head head = new cmpp_head ();
Head. commandid = cmpp30.cmd _ active_test;
Head. sequenceid = createid ();
Head. totallength = 12;

Data_package dp = new data_package ();
DP. sequenceid = head. sequenceid;
DP. Command = head. commandid;
DP. sendcount = 0;
DP. Data = head;
DP. Status = 1;
 
Monitor. Enter (m_messagequeue );
M_messagequeue.enqueue (DP );
Monitor. Exit (m_messagequeue );

}
Return Bok;

}
Protected cmpp_active_test_resp readactivetestresponse (cmpp_head head)
{
Cmpp_active_test_resp resp = new cmpp_active_test_resp ();
Resp. Head = head;
String strerror = string. empty;
Bool Bok = true;
Try
{
If (m_networkstream.dataavailable)
{
Byte [] buffer = new byte [resp. Head. TotalLength-Marshal.SizeOf (resp. Head)];
M_networkstream.read (buffer, 0, buffer. Length );
Resp. Reserved = buffer [0];
Monitor. Enter (slidingwindow );
For (INT I = 0; I <m_islidingwindowsize; I ++)
{
If (slidingwindow [I]. Status = 1) & // sent, waiting for response
(Sshortingwindow [I]. sequenceid = resp. Head. sequenceid)
(Slidingwindow [I]. Command = pai_active_test) // It is active_test.
{
Slidingwindow [I]. Status = 0; // clear the window
Break;
}
}
Monitor. Exit (slidingwindow );
}

}
Catch (exception ex)
{
Resp. Head. commandid = pai_error;
Strerror = ex. message;
Bok = false;
}

If (Bok)
Raiseevent (sms_state.active_test_response, resp );
Else
Raiseevent (sms_state.active_test_response_error, strerror );

Return resp;
}
Protected bool activetestresponse (uint32 sequenceid)
{
Bool Bok = true;
Cmpp_active_test_resp resp = new cmpp_active_test_resp ();
Resp. Head = new cmpp_head ();
Resp. Head. commandid = cmpp30.cmd _ active_test_resp;
Resp. Head. sequenceid = sequenceid;
Resp. Reserved = 0;

Data_package dp = new data_package ();
DP. sequenceid = resp. Head. sequenceid;
DP. Command = resp. Head. commandid;
DP. sendcount = 0;
DP. Data = resp;
DP. Status = 1;
 
Monitor. Enter (m_messagequeue );
M_messagequeue.enqueue (DP );
Monitor. Exit (m_messagequeue );

Return Bok;
}

# Endregion

# Region Public Methods
// Start thread
Public void startthread ()
{
If (m_sendthread = NULL)
{
M_dtlasttransfertime = datetime. now;
M_sendthread = new thread (New threadstart (this. sendthreadproc ));
// M_sendthread.isbackground = false;
M_sendthread.name = m_strspid + "_ Send ";
M_sendthread.start ();
}
If (m_receivethread = NULL)
{
M_deleethread = new thread (New threadstart (this. deleethreadproc ));
// M_deleethread.isbackground = false;
M_deleethread.name = m_strspid + "_ receive ";
M_deleethread.start ();
}
M_eventconnect.set ();

}
// End the thread
Public void endthread ()
{
M_eventsend.reset ();
M_eventreceive.reset ();
M_eventreceiveexit.set ();
M_eventsendexit.set ();
}
// Sending Function
Public bool submit (string message,
String [] destination,
String source,
String serviceid,
Coding coding,
Bool needreport,
Byte feeusertype,
Byte feetype,
Int infographic,
String feeuser,
String linkid)
{
Bool Bok = true;
String strerror = string. empty;
Cmpp_submit submit = new cmpp_submit ();
Submit. Head = new cmpp_head ();
Submit. Head. commandid = cmpp30.cmd _ submit;
Submit. Head. sequenceid = createid ();
Submit. msg_id = 0;
Submit. pk_total = 1;
Submit. pk_number = 1;
Submit. registered_delivery = convert. tobyte (needreport );
Submit. msg_level = 0;
Submit. service_id = serviceid;
Submit. fee_usertype = feeusertype;
Submit. fee_terminal_id = feeuser;
Submit. fee_terminal_type = 0; // real number
Submit. tp_pid = 0;
Submit. tp_udhi = 0;
Submit. msg_fmt = (byte) coding;
Submit. msg_src = m_strspid;
Submit. feetype = string. Format ("{0: D2}", feetype );
Submit. feecode = infographic. tostring ();
Submit. valid_time = string. empty;
Submit. at_time = string. empty;
Submit. src_id = source;
Submit. destusr_tl = (byte) destination. length;
Submit. dest_terminal_id = destination;
Submit. dest_terminal_type = 0; // real number
Submit. msg_length = (byte) Utility. countlength (message, coding );
Submit. msg_content = message;
Submit. linkid = linkid;


Data_package dp = new data_package ();
DP. sequenceid = submit. Head. sequenceid;
DP. Command = submit. Head. commandid;
DP. sendcount = 0;
DP. Data = submit;
DP. Status = 1;
 
Monitor. Enter (m_messagequeue );
M_messagequeue.enqueue (DP );
Monitor. Exit (m_messagequeue );

Return Bok;
}
# Endregion
}
// Callback function for asynchronous sending
Public Delegate bool cmppasyncsubmit (string message,
String [] destination,
String source,
String serviceid,
Coding coding,
Bool needreport,
Byte feeusertype,
Byte feetype,
Int infographic,
String feeuser,
String linkid );

}

[To be continued]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.