123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace HPSocketCS
- {
- public class SSLPullServerEvent
- {
- public delegate HandleResult OnReceiveEventHandler(IntPtr connId, int length);
- }
- public class SSLPullServer : SSLServer
- {
- /// <summary>
- /// 数据到达事件
- /// </summary>
- public new event SSLPullServerEvent.OnReceiveEventHandler OnReceive;
- public SSLPullServer()
- : base()
- {
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="verifyModel">验证模式</param>
- /// <param name="pemCertFile">证书文件(客户端可选)</param>
- /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
- /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
- /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
- /// <param name="sniServerNameCallback">SNI 回调函数指针(可选)</param>
- public SSLPullServer(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath, SSLSdk.SNIServerNameCallback sniServerNameCallback)
- : base(verifyModel, pemCertFile, pemKeyFile, keyPasswod, caPemCertFileOrPath, sniServerNameCallback)
- {
- }
- /// <summary>
- /// 创建socket监听&服务组件
- /// </summary>
- /// <returns></returns>
- protected override bool CreateListener()
- {
- if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
- {
- return false;
- }
- pListener = Sdk.Create_HP_TcpPullServerListener();
- if (pListener == IntPtr.Zero)
- {
- return false;
- }
- pServer = SSLSdk.Create_HP_SSLPullServer(pListener);
- if (pServer == IntPtr.Zero)
- {
- return false;
- }
-
- IsCreate = true;
- return true;
- }
- new Sdk.OnPullReceive _OnReceive = null;
- protected override void SetCallback()
- {
- _OnReceive = new Sdk.OnPullReceive(SDK_OnReceive);
- Sdk.HP_Set_FN_Server_OnPullReceive(pListener, _OnReceive);
- base.SetCallback();
- }
- protected HandleResult SDK_OnReceive(IntPtr pSender, IntPtr connId, int length)
- {
- if (OnReceive != null)
- {
- return OnReceive(connId, length);
- }
- return HandleResult.Ignore;
- }
- /// <summary>
- /// 抓取数据
- /// 用户通过该方法从 Socket 组件中抓取数据
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="pBuffer"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- public FetchResult Fetch(IntPtr connId, IntPtr pBuffer, int size)
- {
- return Sdk.HP_TcpPullServer_Fetch(pServer, connId, pBuffer, size);
- }
- /// <summary>
- /// 抓取数据
- /// 用户通过该方法从 Socket 组件中抓取数据
- /// </summary>
- /// <param name="connId"></param>
- /// <param name="pBuffer"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- public FetchResult Peek(IntPtr connId, IntPtr pBuffer, int size)
- {
- return Sdk.HP_TcpPullServer_Peek(pServer, connId, pBuffer, size);
- }
- /// <summary>
- /// 终止服务并释放资源
- /// </summary>
- public override void Destroy()
- {
- Stop();
- if (pServer != IntPtr.Zero)
- {
- SSLSdk.Destroy_HP_SSLPullServer(pServer);
- pServer = IntPtr.Zero;
- }
- if (pListener != IntPtr.Zero)
- {
- Sdk.Destroy_HP_TcpPullServerListener(pListener);
- pListener = IntPtr.Zero;
- }
- IsCreate = false;
- }
- }
- }
|