📄 basecmpp2.cs
字号:
using System;
using System.Net.Sockets;
using System.Threading;
using Glenet.CMPP.Messages;
namespace Glenet.CMPP
{
public abstract class BaseCMPP2
{
private string _userName;
/// <summary>
/// 账号
/// </summary>
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _passwd;
/// <summary>
/// 密码
/// </summary>
public string Passwd
{
get { return _passwd; }
set { _passwd = value; }
}
private string _spCode;
/// <summary>
/// 企业代码
/// </summary>
public string SpCode
{
get { return _spCode; }
set { _spCode = value; }
}
private string _getwayIp;
/// <summary>
/// 网关IP
/// </summary>
public string GetwayIp
{
get { return _getwayIp; }
set { _getwayIp = value; }
}
private int _port = 7890;
/// <summary>
/// 端口
/// </summary>
public int Port
{
get { return _port; }
set { _port = value; }
}
private uint _version = 0x20;
/// <summary>
/// 版本
/// </summary>
public uint Version
{
get { return _version; }
set { _version = value; }
}
private string _ServiceNo;
/// <summary>
/// 接入号
/// </summary>
public string ServiceNo
{
get { return _ServiceNo; }
set { _ServiceNo = value; }
}
/// <summary>
/// 网络流
/// </summary>
private NetworkStream ns;
public NetworkStream Ns
{
get { return ns; }
set { ns = value; }
}
public BaseCMPP2()
{
}
public BaseCMPP2(string userName, string passwd, string spCode, string getwayip, int port,string serviceNo)
{
_userName = userName;
_passwd = passwd;
_spCode = spCode;
_getwayIp = getwayip;
_port = port;
_ServiceNo = serviceNo;
}
public bool Connection()
{
return CMPP_CONNECT_RESP(out ns);
}
private bool CMPP_CONNECT_RESP(out NetworkStream Stream)
{
bool b = false;
TcpClient tc = new TcpClient();
tc.Connect(_getwayIp, _port);
CMPP_CONNECT connect = new CMPP_CONNECT(_userName, _passwd, DateTime.Now, _version);
byte[] bytes = connect.ToBytes();
Stream = tc.GetStream();
if (Stream.CanWrite)
{
Stream.Write(bytes, 0, bytes.Length);
}
if (Stream.CanRead)
{
int s = 10; //buffer size
bytes = ResponseAsBytes(Stream, MessageHeader.Length + Glenet.CMPP.Messages.CMPP_CONNECT_RESP._FixedBodyLength /*CMPP_CONNECT_RESP.BodyLength*/, s);
CMPP_CONNECT_RESP connect_resp = new CMPP_CONNECT_RESP(bytes);
//PrintHeader(connect_resp.Header);
if (connect_resp.Header.Command_Id == CMPP_Command_Id.CMPP_CONNECT_RESP)
{
b = connect_resp.Status == 0;
}
}
return b;
}
/// <summary>
/// 关闭连接
/// </summary>
public virtual void Close()
{
int buffSize = 10;
if (ns != null)
{
MessageHeader terminate = new MessageHeader(MessageHeader.Length, CMPP_Command_Id.CMPP_TERMINATE, 1);
if (ns.CanWrite)
{
ns.Write(terminate.ToBytes(), 0, MessageHeader.Length);
}
if (ns.CanRead)
{
byte[] bytes = ResponseAsBytes(ns, MessageHeader.Length, buffSize);
MessageHeader terminate_resp = new MessageHeader(bytes);
}
ns.Close();
ns = null;
}
}
public byte[] ResponseAsBytes(NetworkStream Stream, int Length, int BufferSize)
{
int l;
byte[] bytes = new byte[Length];
l = 0;
do
{
byte[] buffer = new byte[BufferSize];
int r = Stream.Read(buffer, 0, buffer.Length);
if (r > 0)
{
Buffer.BlockCopy(buffer, 0, bytes, l, r);
l += r;
}
} while (Stream.DataAvailable);
byte[] Bytes = new byte[l];
Buffer.BlockCopy(bytes, 0, Bytes, 0, Bytes.Length);
return Bytes;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -