UdpServer.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace HPSocketCS
  6. {
  7. public class UdpServer<T> : UdpServer
  8. {
  9. public new T GetExtra(IntPtr connId)
  10. {
  11. return base.GetExtra<T>(connId);
  12. }
  13. public bool SetExtra(IntPtr connId, T obj)
  14. {
  15. return base.SetExtra(connId, obj);
  16. }
  17. }
  18. public class UdpServer : ConnectionExtra, IServer
  19. {
  20. /// <summary>
  21. /// 连接到达事件
  22. /// </summary>
  23. public event ServerEvent.OnAcceptEventHandler OnAccept;
  24. /// <summary>
  25. /// 数据包发送事件
  26. /// </summary>
  27. public event ServerEvent.OnSendEventHandler OnSend;
  28. /// <summary>
  29. /// 准备监听了事件
  30. /// </summary>
  31. public event ServerEvent.OnPrepareListenEventHandler OnPrepareListen;
  32. /// <summary>
  33. /// 数据到达事件
  34. /// </summary>
  35. public event ServerEvent.OnReceiveEventHandler OnReceive;
  36. /// <summary>
  37. /// 数据到达事件(指针数据)
  38. /// </summary>
  39. public event ServerEvent.OnPointerDataReceiveEventHandler OnPointerDataReceive;
  40. /// <summary>
  41. /// 连接关闭事件
  42. /// </summary>
  43. public event ServerEvent.OnCloseEventHandler OnClose;
  44. /// <summary>
  45. /// 服务器关闭事件
  46. /// </summary>
  47. public event ServerEvent.OnShutdownEventHandler OnShutdown;
  48. /// <summary>
  49. /// 握手成功事件
  50. /// </summary>
  51. public event ServerEvent.OnHandShakeEventHandler OnHandShake;
  52. protected IntPtr pServer = IntPtr.Zero;
  53. protected IntPtr pListener = IntPtr.Zero;
  54. /// <summary>
  55. /// 服务器ip
  56. /// </summary>
  57. public string IpAddress { get; set; }
  58. /// <summary>
  59. /// 服务器端口
  60. /// </summary>
  61. public ushort Port { get; set; }
  62. protected bool IsCreate = false;
  63. public UdpServer()
  64. {
  65. CreateListener();
  66. }
  67. ~UdpServer()
  68. {
  69. Destroy();
  70. }
  71. /// <summary>
  72. /// 创建socket监听&服务组件
  73. /// </summary>
  74. /// <returns></returns>
  75. protected virtual bool CreateListener()
  76. {
  77. if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
  78. {
  79. return false;
  80. }
  81. pListener = Sdk.Create_HP_UdpServerListener();
  82. if (pListener == IntPtr.Zero)
  83. {
  84. return false;
  85. }
  86. pServer = Sdk.Create_HP_UdpServer(pListener);
  87. if (pServer == IntPtr.Zero)
  88. {
  89. return false;
  90. }
  91. IsCreate = true;
  92. return true;
  93. }
  94. /// <summary>
  95. /// 终止服务并释放资源
  96. /// </summary>
  97. public virtual void Destroy()
  98. {
  99. Stop();
  100. if (pServer != IntPtr.Zero)
  101. {
  102. Sdk.Destroy_HP_UdpServer(pServer);
  103. pServer = IntPtr.Zero;
  104. }
  105. if (pListener != IntPtr.Zero)
  106. {
  107. Sdk.Destroy_HP_UdpServerListener(pListener);
  108. pListener = IntPtr.Zero;
  109. }
  110. IsCreate = false;
  111. }
  112. /// <summary>
  113. /// 启动服务
  114. /// </summary>
  115. /// <returns></returns>
  116. public bool Start()
  117. {
  118. if (IsCreate == false)
  119. {
  120. return false;
  121. }
  122. if (pServer == IntPtr.Zero)
  123. {
  124. return false;
  125. }
  126. SetCallback();
  127. return Sdk.HP_Server_Start(pServer, IpAddress, Port);
  128. }
  129. /// <summary>
  130. /// 停止服务
  131. /// </summary>
  132. /// <returns></returns>
  133. public bool Stop()
  134. {
  135. if (pServer == IntPtr.Zero)
  136. {
  137. return false;
  138. }
  139. return Sdk.HP_Server_Stop(pServer);
  140. }
  141. /// <summary>
  142. /// 发送数据
  143. /// </summary>
  144. /// <param name="connId"></param>
  145. /// <param name="bytes"></param>
  146. /// <param name="size"></param>
  147. /// <returns></returns>
  148. public bool Send(IntPtr connId, byte[] bytes, int size)
  149. {
  150. return Sdk.HP_Server_Send(pServer, connId, bytes, size);
  151. }
  152. /// <summary>
  153. /// 发送数据
  154. /// </summary>
  155. /// <param name="connId"></param>
  156. /// <param name="bufferPtr"></param>
  157. /// <param name="size"></param>
  158. /// <returns></returns>
  159. public bool Send(IntPtr connId, IntPtr bufferPtr, int size)
  160. {
  161. return Sdk.HP_Server_Send(pServer, connId, bufferPtr, size);
  162. }
  163. /// <summary>
  164. /// 发送数据
  165. /// </summary>
  166. /// <param name="connId"></param>
  167. /// <param name="bytes"></param>
  168. /// <param name="offset">针对bytes的偏移</param>
  169. /// <param name="size">发多大</param>
  170. /// <returns></returns>
  171. public bool Send(IntPtr connId, byte[] bytes, int offset, int size)
  172. {
  173. return Sdk.HP_Server_SendPart(pServer, connId, bytes, size, offset);
  174. }
  175. /// <summary>
  176. /// 发送数据
  177. /// </summary>
  178. /// <param name="connId"></param>
  179. /// <param name="bufferPtr"></param>
  180. /// <param name="offset">针对bufferPtr的偏移</param>
  181. /// <param name="size">发多大</param>
  182. /// <returns></returns>
  183. public bool Send(IntPtr connId, IntPtr bufferPtr, int offset, int size)
  184. {
  185. return Sdk.HP_Server_SendPart(pServer, connId, bufferPtr, size, offset);
  186. }
  187. /// <summary>
  188. /// 发送多组数据
  189. /// 向指定连接发送多组数据
  190. /// UDP - 把所有数据包组合成一个数据包发送(数据包的总长度不能大于设置的 UDP 包最大长度)
  191. /// </summary>
  192. /// <param name="connId">连接 ID</param>
  193. /// <param name="pBuffers">发送缓冲区数组</param>
  194. /// <param name="count">发送缓冲区数目</param>
  195. /// <returns>TRUE.成功,FALSE.失败,可通过 SYSGetLastError() 获取 Windows 错误代码</returns>
  196. public bool SendPackets(IntPtr connId, WSABUF[] pBuffers, int count)
  197. {
  198. return Sdk.HP_Server_SendPackets(pServer, connId, pBuffers, count);
  199. }
  200. /// <summary>
  201. /// 发送多组数据
  202. /// 向指定连接发送多组数据
  203. /// UDP - 把所有数据包组合成一个数据包发送(数据包的总长度不能大于设置的 UDP 包最大长度)
  204. /// </summary>
  205. /// <param name="connId">连接 ID</param>
  206. /// <param name="objects">发送缓冲区数组</param>
  207. /// <returns>TRUE.成功,FALSE.失败,可通过 SYSGetLastError() 获取 Windows 错误代码</returns>
  208. public bool SendPackets<T>(IntPtr connId, T[] objects)
  209. {
  210. bool ret = false;
  211. WSABUF[] buffer = new WSABUF[objects.Length];
  212. IntPtr[] ptrs = new IntPtr[buffer.Length];
  213. try
  214. {
  215. for (int i = 0; i < objects.Length; i++)
  216. {
  217. buffer[i].Length = Marshal.SizeOf(typeof(T));
  218. ptrs[i] = Marshal.AllocHGlobal(buffer[i].Length);
  219. Marshal.StructureToPtr(objects[i], ptrs[i], true);
  220. buffer[i].Buffer = ptrs[i];
  221. }
  222. ret = SendPackets(connId, buffer, buffer.Length);
  223. }
  224. catch (Exception ex)
  225. {
  226. throw ex;
  227. }
  228. finally
  229. {
  230. foreach (var ptr in ptrs)
  231. {
  232. if (ptr != IntPtr.Zero)
  233. {
  234. Marshal.FreeHGlobal(ptr);
  235. }
  236. }
  237. }
  238. return ret;
  239. }
  240. /// <summary>
  241. /// 断开与某个客户的连接
  242. /// </summary>
  243. /// <param name="connId"></param>
  244. /// <param name="force">是否强制断开</param>
  245. /// <returns></returns>
  246. public bool Disconnect(IntPtr connId, bool force = true)
  247. {
  248. return Sdk.HP_Server_Disconnect(pServer, connId, force);
  249. }
  250. /// <summary>
  251. /// 断开超过指定时间的连接
  252. /// </summary>
  253. /// <param name="period">毫秒</param>
  254. /// <param name="force">强制</param>
  255. /// <returns></returns>
  256. public bool DisconnectLongConnections(uint period, bool force = true)
  257. {
  258. return Sdk.HP_Server_DisconnectLongConnections(pServer, period, force);
  259. }
  260. /// <summary>
  261. /// 暂停接收
  262. /// </summary>
  263. /// <param name="connId"></param>
  264. /// <returns></returns>
  265. public bool PauseReceive(IntPtr connId)
  266. {
  267. return Sdk.HP_Server_PauseReceive(pServer, connId, true);
  268. }
  269. /// <summary>
  270. /// 唤醒接收
  271. /// </summary>
  272. /// <param name="connId"></param>
  273. /// <returns></returns>
  274. public bool ResumeReceive(IntPtr connId)
  275. {
  276. return Sdk.HP_Server_PauseReceive(pServer, connId, false);
  277. }
  278. /// <summary>
  279. /// 获取连接的接收状态
  280. /// </summary>
  281. /// <param name="connId"></param>
  282. /// <returns></returns>
  283. public ReceiveState GetReceiveState(IntPtr connId)
  284. {
  285. int state = -1;
  286. if (Sdk.HP_Server_IsPauseReceive(pServer, connId, ref state))
  287. {
  288. return (ReceiveState)state;
  289. }
  290. return ReceiveState.Unknown;
  291. }
  292. /// <summary>
  293. /// 断开超过指定时长的静默连接
  294. /// </summary>
  295. /// <param name="period">毫秒</param>
  296. /// <param name="force">强制</param>
  297. /// <returns></returns>
  298. public bool DisconnectSilenceConnections(uint period, bool force = true)
  299. {
  300. return Sdk.HP_Server_DisconnectSilenceConnections(pServer, period, force);
  301. }
  302. /// <summary>
  303. /// 获取某个连接的本地地址信息
  304. /// </summary>
  305. /// <param name="connId"></param>
  306. /// <param name="ip"></param>
  307. /// <param name="port"></param>
  308. /// <returns></returns>
  309. public bool GetLocalAddress(IntPtr connId, ref string ip, ref ushort port)
  310. {
  311. int ipLength = 40;
  312. StringBuilder sb = new StringBuilder(ipLength);
  313. bool ret = Sdk.HP_Server_GetLocalAddress(pServer, connId, sb, ref ipLength, ref port) && ipLength > 0;
  314. if (ret == true)
  315. {
  316. ip = sb.ToString();
  317. }
  318. return ret;
  319. }
  320. /// <summary>
  321. /// 获取某个连接的远程地址信息
  322. /// </summary>
  323. /// <param name="connId"></param>
  324. /// <param name="ip"></param>
  325. /// <param name="port"></param>
  326. /// <returns></returns>
  327. public bool GetRemoteAddress(IntPtr connId, ref string ip, ref ushort port)
  328. {
  329. int ipLength = 40;
  330. StringBuilder sb = new StringBuilder(ipLength);
  331. bool ret = Sdk.HP_Server_GetRemoteAddress(pServer, connId, sb, ref ipLength, ref port) && ipLength > 0;
  332. if (ret == true)
  333. {
  334. ip = sb.ToString();
  335. }
  336. return ret;
  337. }
  338. /// <summary>
  339. /// 获取连接中未发出数据的长度
  340. /// </summary>
  341. /// <param name="connId"></param>
  342. /// <param name="length"></param>
  343. /// <returns></returns>
  344. public bool GetPendingDataLength(IntPtr connId, ref int length)
  345. {
  346. return Sdk.HP_Server_GetPendingDataLength(pServer, connId, ref length);
  347. }
  348. // 是否启动
  349. public bool IsStarted
  350. {
  351. get
  352. {
  353. if (pServer == IntPtr.Zero)
  354. {
  355. return false;
  356. }
  357. return Sdk.HP_Server_HasStarted(pServer);
  358. }
  359. }
  360. /// <summary>
  361. /// 状态
  362. /// </summary>
  363. public ServiceState State
  364. {
  365. get
  366. {
  367. return Sdk.HP_Server_GetState(pServer);
  368. }
  369. }
  370. /// <summary>
  371. /// 连接数
  372. /// </summary>
  373. public uint ConnectionCount
  374. {
  375. get
  376. {
  377. return Sdk.HP_Server_GetConnectionCount(pServer);
  378. }
  379. }
  380. /// <summary>
  381. /// 检测是否为安全连接(SSL/HTTPS)
  382. /// </summary>
  383. public bool IsSecure
  384. {
  385. get
  386. {
  387. return Sdk.HP_Server_IsSecure(pServer);
  388. }
  389. }
  390. /// <summary>
  391. /// 获取所有连接,未获取到连接返回null
  392. /// </summary>
  393. /// <returns></returns>
  394. public IntPtr[] GetAllConnectionIDs()
  395. {
  396. IntPtr[] arr = null;
  397. do
  398. {
  399. uint count = ConnectionCount;
  400. if (count == 0)
  401. {
  402. break;
  403. }
  404. arr = new IntPtr[count];
  405. if (Sdk.HP_Server_GetAllConnectionIDs(pServer, arr, ref count))
  406. {
  407. if (arr.Length > count)
  408. {
  409. IntPtr[] newArr = new IntPtr[count];
  410. Array.Copy(arr, newArr, count);
  411. arr = newArr;
  412. }
  413. break;
  414. }
  415. } while (true);
  416. return arr;
  417. }
  418. /// <summary>
  419. /// 获取监听socket的地址信息
  420. /// </summary>
  421. /// <param name="ip"></param>
  422. /// <param name="port"></param>
  423. /// <returns></returns>
  424. public bool GetListenAddress(ref string ip, ref ushort port)
  425. {
  426. int ipLength = 40;
  427. StringBuilder sb = new StringBuilder(ipLength);
  428. bool ret = Sdk.HP_Server_GetListenAddress(pServer, sb, ref ipLength, ref port);
  429. if (ret == true)
  430. {
  431. ip = sb.ToString();
  432. }
  433. return ret;
  434. }
  435. /// <summary>
  436. /// 获取指定连接的连接时长(毫秒)
  437. /// </summary>
  438. /// <param name="connId"></param>
  439. /// <param name="period"></param>
  440. /// <returns></returns>
  441. public bool GetConnectPeriod(IntPtr connId, ref uint period)
  442. {
  443. return Sdk.HP_Server_GetConnectPeriod(pServer, connId, ref period);
  444. }
  445. /// <summary>
  446. /// 获取某个连接静默时间(毫秒)
  447. /// </summary>
  448. /// <param name="connId"></param>
  449. /// <param name="period"></param>
  450. /// <returns></returns>
  451. public bool GetSilencePeriod(IntPtr connId, ref uint period)
  452. {
  453. return Sdk.HP_Server_GetSilencePeriod(pServer, connId, ref period);
  454. }
  455. /// <summary>
  456. /// 设置最大连接数(组件会根据设置值预分配内存,因此需要根据实际情况设置,不宜过大)
  457. /// </summary>
  458. public uint MaxConnectionCount
  459. {
  460. get
  461. {
  462. return Sdk.HP_Server_GetMaxConnectionCount(pServer);
  463. }
  464. set
  465. {
  466. Sdk.HP_Server_SetMaxConnectionCount(pServer, value);
  467. }
  468. }
  469. /// <summary>
  470. /// 读取或设置工作线程数量(通常设置为 2 * CPU + 2)
  471. /// </summary>
  472. public uint WorkerThreadCount
  473. {
  474. get
  475. {
  476. return Sdk.HP_Server_GetWorkerThreadCount(pServer);
  477. }
  478. set
  479. {
  480. Sdk.HP_Server_SetWorkerThreadCount(pServer, value);
  481. }
  482. }
  483. /// <summary>
  484. /// 读取或设置 Socket 缓存对象锁定时间(毫秒,在锁定期间该 Socket 缓存对象不能被获取使用)
  485. /// </summary>
  486. public uint FreeSocketObjLockTime
  487. {
  488. get
  489. {
  490. return Sdk.HP_Server_GetFreeSocketObjLockTime(pServer);
  491. }
  492. set
  493. {
  494. Sdk.HP_Server_SetFreeSocketObjLockTime(pServer, value);
  495. }
  496. }
  497. /// <summary>
  498. /// 读取或设置 Socket 缓存池大小(通常设置为平均并发连接数量的 1/3 - 1/2)
  499. /// </summary>
  500. public uint FreeSocketObjPool
  501. {
  502. get
  503. {
  504. return Sdk.HP_Server_GetFreeSocketObjPool(pServer);
  505. }
  506. set
  507. {
  508. Sdk.HP_Server_SetFreeSocketObjPool(pServer, value);
  509. }
  510. }
  511. /// <summary>
  512. /// 读取或设置内存块缓存池大小(通常设置为 Socket 缓存池大小的 2 - 3 倍)
  513. /// </summary>
  514. public uint FreeBufferObjPool
  515. {
  516. get
  517. {
  518. return Sdk.HP_Server_GetFreeBufferObjPool(pServer);
  519. }
  520. set
  521. {
  522. Sdk.HP_Server_SetFreeBufferObjPool(pServer, value);
  523. }
  524. }
  525. /// <summary>
  526. /// 读取或设置内存块缓存池大小(通常设置为 Socket 缓存池大小的 2 - 3 倍)
  527. /// </summary>
  528. public uint FreeSocketObjHold
  529. {
  530. get
  531. {
  532. return Sdk.HP_Server_GetFreeSocketObjHold(pServer);
  533. }
  534. set
  535. {
  536. Sdk.HP_Server_SetFreeSocketObjHold(pServer, value);
  537. }
  538. }
  539. /// <summary>
  540. /// 读取或设置内存块缓存池回收阀值(通常设置为内存块缓存池大小的 3 倍)
  541. /// </summary>
  542. public uint FreeBufferObjHold
  543. {
  544. get
  545. {
  546. return Sdk.HP_Server_GetFreeBufferObjHold(pServer);
  547. }
  548. set
  549. {
  550. Sdk.HP_Server_SetFreeBufferObjHold(pServer, value);
  551. }
  552. }
  553. /// <summary>
  554. /// 读取或设置是否标记静默时间(设置为 TRUE 时 DisconnectSilenceConnections() 和 GetSilencePeriod() 才有效,默认:FALSE)
  555. /// </summary>
  556. public bool MarkSilence
  557. {
  558. get
  559. {
  560. return Sdk.HP_Server_IsMarkSilence(pServer);
  561. }
  562. set
  563. {
  564. Sdk.HP_Server_SetMarkSilence(pServer, value);
  565. }
  566. }
  567. /// <summary>
  568. /// 获取或设置数据发送策略
  569. /// </summary>
  570. public SendPolicy SendPolicy
  571. {
  572. get
  573. {
  574. return Sdk.HP_Server_GetSendPolicy(pServer);
  575. }
  576. set
  577. {
  578. Sdk.HP_Server_SetSendPolicy(pServer, value);
  579. }
  580. }
  581. /// <summary>
  582. /// 获取或设置 OnSend 事件同步策略
  583. /// </summary>
  584. public OnSendSyncPolicy OnSendSyncPolicy
  585. {
  586. get
  587. {
  588. return Sdk.HP_Server_GetOnSendSyncPolicy(pServer);
  589. }
  590. set
  591. {
  592. Sdk.HP_Server_SetOnSendSyncPolicy(pServer, value);
  593. }
  594. }
  595. /// <summary>
  596. /// 获取系统返回的错误码
  597. /// </summary>
  598. public int SYSGetLastError()
  599. {
  600. return Sdk.SYS_GetLastError();
  601. }
  602. /// <summary>
  603. /// 调用系统的 ::WSAGetLastError() 方法获取通信错误代码
  604. /// </summary>
  605. public int SYSWSAGetLastError()
  606. {
  607. return Sdk.SYS_WSAGetLastError();
  608. }
  609. /// <summary>
  610. /// 获取错误码
  611. /// </summary>
  612. public SocketError ErrorCode
  613. {
  614. get
  615. {
  616. return Sdk.HP_Server_GetLastError(pServer);
  617. }
  618. }
  619. /// <summary>
  620. /// 版本号
  621. /// </summary>
  622. public string Version
  623. {
  624. get
  625. {
  626. return Sdk.GetHPSocketVersion();
  627. }
  628. }
  629. /// <summary>
  630. /// 获取错误信息
  631. /// </summary>
  632. public string ErrorMessage
  633. {
  634. get
  635. {
  636. IntPtr ptr = Sdk.HP_Server_GetLastErrorDesc(pServer);
  637. string desc = Marshal.PtrToStringAnsi(ptr);
  638. return desc;
  639. }
  640. }
  641. ///////////////////////////////////////////////////////////////////////////////////////
  642. protected Sdk.OnPrepareListen _OnPrepareListen = null;
  643. protected Sdk.OnAccept _OnAccept = null;
  644. protected Sdk.OnReceive _OnReceive = null;
  645. protected Sdk.OnSend _OnSend = null;
  646. protected Sdk.OnClose _OnClose = null;
  647. protected Sdk.OnShutdown _OnShutdown = null;
  648. protected Sdk.OnHandShake _OnHandShake = null;
  649. protected virtual void SetCallback()
  650. {
  651. _OnPrepareListen = new Sdk.OnPrepareListen(SDK_OnPrepareListen);
  652. _OnAccept = new Sdk.OnAccept(SDK_OnAccept);
  653. _OnSend = new Sdk.OnSend(SDK_OnSend);
  654. _OnReceive = new Sdk.OnReceive(SDK_OnReceive);
  655. _OnClose = new Sdk.OnClose(SDK_OnClose);
  656. _OnShutdown = new Sdk.OnShutdown(SDK_OnShutdown);
  657. _OnHandShake = new Sdk.OnHandShake(SDK_OnHandShake);
  658. Sdk.HP_Set_FN_Server_OnPrepareListen(pListener, _OnPrepareListen);
  659. Sdk.HP_Set_FN_Server_OnAccept(pListener, _OnAccept);
  660. Sdk.HP_Set_FN_Server_OnSend(pListener, _OnSend);
  661. Sdk.HP_Set_FN_Server_OnReceive(pListener, _OnReceive);
  662. Sdk.HP_Set_FN_Server_OnClose(pListener, _OnClose);
  663. Sdk.HP_Set_FN_Server_OnShutdown(pListener, _OnShutdown);
  664. Sdk.HP_Set_FN_Server_OnHandShake(pListener, _OnHandShake);
  665. }
  666. protected HandleResult SDK_OnHandShake(IntPtr pSender, IntPtr connId)
  667. {
  668. if (OnHandShake != null)
  669. {
  670. return OnHandShake(this, connId);
  671. }
  672. return HandleResult.Ignore;
  673. }
  674. protected HandleResult SDK_OnPrepareListen(IntPtr pSender, IntPtr soListen)
  675. {
  676. if (OnPrepareListen != null)
  677. {
  678. return OnPrepareListen(this, soListen);
  679. }
  680. return HandleResult.Ignore;
  681. }
  682. protected HandleResult SDK_OnAccept(IntPtr pSender, IntPtr connId, IntPtr pClient)
  683. {
  684. if (OnAccept != null)
  685. {
  686. return OnAccept(this, connId, pClient);
  687. }
  688. return HandleResult.Ignore;
  689. }
  690. protected HandleResult SDK_OnSend(IntPtr pSender, IntPtr connId, IntPtr pData, int length)
  691. {
  692. if (OnSend != null)
  693. {
  694. byte[] bytes = new byte[length];
  695. Marshal.Copy(pData, bytes, 0, length);
  696. return OnSend(this, connId, bytes);
  697. }
  698. return HandleResult.Ignore;
  699. }
  700. protected HandleResult SDK_OnReceive(IntPtr pSender, IntPtr connId, IntPtr pData, int length)
  701. {
  702. if (OnPointerDataReceive != null)
  703. {
  704. return OnPointerDataReceive(this, connId, pData, length);
  705. }
  706. else if (OnReceive != null)
  707. {
  708. byte[] bytes = new byte[length];
  709. Marshal.Copy(pData, bytes, 0, length);
  710. return OnReceive(this, connId, bytes);
  711. }
  712. return HandleResult.Ignore;
  713. }
  714. protected HandleResult SDK_OnClose(IntPtr pSender, IntPtr connId, SocketOperation enOperation, int errorCode)
  715. {
  716. if (OnClose != null)
  717. {
  718. return OnClose(this, connId, enOperation, errorCode);
  719. }
  720. return HandleResult.Ignore;
  721. }
  722. protected HandleResult SDK_OnShutdown(IntPtr pSender)
  723. {
  724. if (OnShutdown != null)
  725. {
  726. return OnShutdown(this);
  727. }
  728. return HandleResult.Ignore;
  729. }
  730. /////////////////////////////////////////////////////////////////////////
  731. /// <summary>
  732. /// 根据错误码返回错误信息
  733. /// </summary>
  734. /// <param name="code"></param>
  735. /// <returns></returns>
  736. public string GetSocketErrorDesc(SocketError code)
  737. {
  738. IntPtr ptr = Sdk.HP_GetSocketErrorDesc(code);
  739. string desc = Marshal.PtrToStringAnsi(ptr);
  740. return desc;
  741. }
  742. /// <summary>
  743. /// 调用系统的 setsockopt()
  744. /// </summary>
  745. /// <param name="sock"></param>
  746. /// <param name="level"></param>
  747. /// <param name="name"></param>
  748. /// <param name="val"></param>
  749. /// <param name="len"></param>
  750. /// <returns></returns>
  751. ///
  752. public int SYS_SetSocketOption(IntPtr sock, int level, int name, IntPtr val, int len)
  753. {
  754. return Sdk.SYS_SetSocketOption(sock, level, name, val, len);
  755. }
  756. /// <summary>
  757. /// 调用系统的 getsockopt()
  758. /// </summary>
  759. /// <param name="sock"></param>
  760. /// <param name="level"></param>
  761. /// <param name="name"></param>
  762. /// <param name="val"></param>
  763. /// <param name="len"></param>
  764. /// <returns></returns>
  765. ///
  766. public int SYSGetSocketOption(IntPtr sock, int level, int name, IntPtr val, ref int len)
  767. {
  768. return Sdk.SYS_GetSocketOption(sock, level, name, val, ref len);
  769. }
  770. /// <summary>
  771. /// 调用系统的 ioctlsocket()
  772. /// </summary>
  773. /// <param name="sock"></param>
  774. /// <param name="cmd"></param>
  775. /// <param name="arg"></param>
  776. /// <returns></returns>
  777. ///
  778. public int SYSIoctlSocket(IntPtr sock, long cmd, IntPtr arg)
  779. {
  780. return Sdk.SYS_IoctlSocket(sock, cmd, arg);
  781. }
  782. }
  783. }