(转)hp socket 5.8.2 高性能网络扩展库
感谢: fish 分享
aardio的扩展库里已经有个jacenHe提供的HPsocket库,位置如下:
此次分享的是由fish封装的, 目前只封装了 ssl http client 部分, 测试例子在 simple 目录下.
https://github.com/btx638/HP-Socket-bindings-for-aardio
有需要的可以学习下.
使用例子代码如下:
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio form";right=466;bottom=144) winform.add( btnBaidu={cls="button";text="访问 baidu";left=25;top=16;right=156;bottom=47;z=2}; btnCnbeta={cls="button";text="访问 cnbeta";left=176;top=17;right=307;bottom=48;z=3}; edInfo={cls="edit";text="网页源码保存在同目录的 test.html";left=208;top=89;right=423;bottom=116;edge=1;multiline=1;readonly=1;z=1} ) /*}}*/ io.open() import aaz.libhpsocket.ssl.listener.httpClient; var listener = aaz.libhpsocket.ssl.listener.httpClient(); var component = listener.createComponent(); listener.onPrepareConnect = function(component, connId, soListen){ io.print("[onPrepareConnect]", connId, soListen) } listener.onConnect = function(component, connId){ io.print("[onConnect]", connId) } listener.onHandShake = function(component, connId){ io.print("[onHandShake]", connId) } // 开始解析 listener.onMessageBegin = function(component, connId){ io.print("[onMessageBegin]", component, connId) component.reallocString(1) } // 状态行解析完成 (仅用于 HTTP 客户端) listener.onStatusLine = function(component, connId, usStatusCode, lpszDesc){ io.print("[onStatusLine]", component, connId, usStatusCode, lpszDesc ) } // 请求头 listener.onHeader = function(component, connId, pszName, lpszValue){ io.print("[onHeader]", component, connId, pszName, lpszValue) } // 请求头完成 listener.onHeadersComplete = function(component, connId){ io.print("[onHeadersComplete]", component, connId) if(component.getHeader("Transfer-Encoding") == "chunked"){ io.print("[onHeadersComplete]->分块传输") } else { io.print("[onHeadersComplete]->content-length", component.contentLength) } io.print("------all headers-----------") var headers = component.getAllHeaders() for(i=1;#headers;1){ io.print( headers[i].name, headers[i].value ) } io.print("------headers-----------") var headers = component.getHeaders("Set-Cookie"); for(i=1;#headers;1){ io.print(headers[i]) } io.print("------all cookies-----------") var cookies = component.getAllCookies() for(i=1;#cookies;1){ io.print( cookies[i].name, cookies[i].value ) } } // Chunked 报文头 listener.onChunkHeader = function(component, connId, iLength){ io.print("[onChunkHeader]", component, connId, iLength) } // Chunked 报文结束 listener.onChunkComplete = function(component, connId){ io.print("[onChunkComplete]", component, connId) } // BODY 报文 listener.onBody = function(component, connId, pData, len){ io.print("[onBody]", component, connId, pData, len) component.appendString(pData, len) } // 完成解析 listener.onMessageComplete = function(component, connId){ io.print("[onMessageComplete]", component, connId) var html = component.getString(); string.save("\test.html", html) } // 升级协议 listener.onUpgrade = function(component, connId, enUpgradeType){ io.print("[onUpgrade]", component, connId, enUpgradeType) } // 解析错误 listener.onParseError = function(component, connId, iErrorCode, lpszErrorDesc){ io.print("[onParseError]", component, connId, iErrorCode, lpszErrorDesc) } // Web Socket 数据包头 listener.onWsMessageHeader = function(component, connId, bFinal, iReserved, iOperationCode, lpszMask, ullBodyLen){ io.print("[onWsMessageHeader]", component, connId, bFinal, iReserved, iOperationCode, lpszMask, ullBodyLen) } // Web Socket 数据包体 listener.onWsMessageBody = function(component, connId, pData, iLength){ io.print("[onWsMessageBody]", component, connId, pData, iLength) } // Web Socket 数据包体 listener.onWsMessageComplete = function(component, connId){ io.print("[onWsMessageComplete]", component, connId) } listener.onClose = function(component, connId){ io.print("[onClose]", component, connId) component.reallocString(0) } component.setupSSLContext() winform.btnCnbeta.oncommand = function(id,event){ component.start("www.cnbeta.com",443) // 自己想办法在触发 onHandShake 事件后才运行 component.sendGet( "/", { ["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"; ["accept-language"] = "zh-CN,zh;q=0.9,ja;q=0.8,zh-TW;q=0.7"; } ) } winform.btnBaidu.oncommand = function(id,event){ component.start("www.baidu.com",443) // 自己想办法在触发 onHandShake 事件后才运行 component.sendGet( "/", { ["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"; ["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"; ["accept-language"] = "zh-CN,zh;q=0.9,ja;q=0.8,zh-TW;q=0.7"; } ) } winform.show(); win.loopMessage();
hp socket 说已经提供了 线程池组件的方案,包含了 OnWorkerThreadStart/End的接口
具体看 https://github.com/ldcsaa/HP-Socket/issues/205#issuecomment-1001312752
API 位置 https://github.com/ldcsaa/HP-Socket/blob/dev/Windows/Include/HPSocket/HPSocket4C.h#L2558
回复#11 @terrorist :
新版本hpsocket.dll提供的线程池组件与socket(server,client等)组件线程回调无关,只是帮助我们创建管理自己的线程池,与socket组件的线程并不是同一个池子,所以并没有实现我们需要的onThreadEnd事件。既然onThreadEnd没有实现,那为什么aardio中的hp库能监测onThreadStart呢?其实也不能监控,只是大佬换了一个思路,用一个global变量在首次回调触发时当成onThreadStart来处理。我们也没必要纠结这个点,只要在使用com前主动调用thread.callbackInitialize,使用完com后再调用 thread.callbackUninitialize
登录后方可回帖
感谢回复,已删除 thread.callbackInitialize 相关代码