123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace HPSocketCS
- {
- public class TcpPullAgentEvent
- {
- public delegate HandleResult OnReceiveEventHandler(IntPtr connId, int length);
- }
- public class TcpPullAgent<T> : TcpPullAgent
- {
- public new T GetExtra(IntPtr connId)
- {
- return base.GetExtra<T>(connId);
- }
- public bool SetExtra(IntPtr connId, T obj)
- {
- return base.SetExtra(connId, obj);
- }
- }
- public class TcpPullAgent : TcpAgent
- {
- public new TcpPullAgentEvent.OnReceiveEventHandler OnReceive;
- /// <summary>
- /// 创建socket监听&服务组件
- /// </summary>
- /// <returns></returns>
- protected override bool CreateListener()
- {
- if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
- {
- return false;
- }
- pListener = Sdk.Create_HP_TcpPullAgentListener();
- if (pListener == IntPtr.Zero)
- {
- return false;
- }
- pAgent = Sdk.Create_HP_TcpPullAgent(pListener);
- if (pAgent == IntPtr.Zero)
- {
- return false;
- }
- IsCreate = true;
- return true;
- }
- /// <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_TcpPullAgent_Fetch(pAgent, 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_TcpPullAgent_Peek(pAgent, connId, pBuffer, size);
- }
- new Sdk.OnPullReceive _OnReceive = null;
- /// <summary>
- /// 设置回调函数
- /// </summary>
- /// <param name="prepareConnect"></param>
- /// <param name="connect"></param>
- /// <param name="send"></param>
- /// <param name="recv"></param>
- /// <param name="close"></param>
- /// <param name="error"></param>
- /// <param name="agentShutdown"></param>
- protected override void SetCallback()
- {
- _OnReceive = new Sdk.OnPullReceive(SDK_OnReceive);
- // 设置 Socket 监听器回调函数
- Sdk.HP_Set_FN_Agent_OnPullReceive(pListener, _OnReceive);
- base.SetCallback();
- }
- protected virtual HandleResult SDK_OnReceive(IntPtr pSender, IntPtr connId, int length)
- {
- if (OnReceive != null)
- {
- return OnReceive(connId, length);
- }
- return HandleResult.Ignore;
- }
- /// <summary>
- /// 终止服务并释放资源
- /// </summary>
- public override void Destroy()
- {
- Stop();
- if (pAgent != IntPtr.Zero)
- {
- Sdk.Destroy_HP_TcpPullAgent(pAgent);
- pAgent = IntPtr.Zero;
- }
- if (pListener != IntPtr.Zero)
- {
- Sdk.Destroy_HP_TcpPullAgentListener(pListener);
- pListener = IntPtr.Zero;
- }
- IsCreate = false;
- }
- }
- }
|