You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.4 KiB
105 lines
3.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
using HPSocketCS.SDK;
|
|
namespace HPSocketCS
|
|
{
|
|
public class TcpPullServerEvent
|
|
{
|
|
public delegate HandleResult OnReceiveEventHandler(IntPtr connId, int length);
|
|
}
|
|
public class TcpPullServer : TcpServer
|
|
{
|
|
/// <summary>
|
|
/// 数据到达事件
|
|
/// </summary>
|
|
public new event TcpPullServerEvent.OnReceiveEventHandler OnReceive;
|
|
public TcpPullServer()
|
|
{
|
|
CreateListener();
|
|
}
|
|
/// <summary>
|
|
/// 创建socket监听&服务组件
|
|
/// </summary>
|
|
/// <param name="isUseDefaultCallback">是否使用tcppullserver类默认回调函数</param>
|
|
/// <returns></returns>
|
|
protected override bool CreateListener()
|
|
{
|
|
if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
|
|
{
|
|
return false;
|
|
}
|
|
pListener = HPSocketSdk.Create_HP_TcpPullServerListener();
|
|
if (pListener == IntPtr.Zero)
|
|
{
|
|
return false;
|
|
}
|
|
pServer = HPSocketSdk.Create_HP_TcpPullServer(pListener);
|
|
if (pServer == IntPtr.Zero)
|
|
{
|
|
return false;
|
|
}
|
|
IsCreate = true;
|
|
return true;
|
|
}
|
|
HPSocketCS.SDK.HPSocketSdk.OnPullReceive _OnReceive = null;
|
|
protected override void SetCallback()
|
|
{
|
|
_OnReceive = new HPSocketSdk.OnPullReceive(SDK_OnReceive);
|
|
HPSocketSdk.HP_Set_FN_Server_OnPullReceive(pListener, _OnReceive);
|
|
base.SetCallback();
|
|
}
|
|
protected HandleResult SDK_OnReceive(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 HPSocketSdk.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 HPSocketSdk.HP_TcpPullServer_Peek(pServer, connId, pBuffer, size);
|
|
}
|
|
/// <summary>
|
|
/// 释放TcpPullServer和TcpPullServerListener
|
|
/// </summary>
|
|
public override void Destroy()
|
|
{
|
|
Stop();
|
|
if (pServer != IntPtr.Zero)
|
|
{
|
|
HPSocketSdk.Destroy_HP_TcpPullServer(pServer);
|
|
pServer = IntPtr.Zero;
|
|
}
|
|
if (pListener != IntPtr.Zero)
|
|
{
|
|
HPSocketSdk.Destroy_HP_TcpPullServerListener(pListener);
|
|
pListener = IntPtr.Zero;
|
|
}
|
|
IsCreate = false;
|
|
}
|
|
}
|
|
}
|
|
|