// 执行初始化 // 同步模式 // parameters: // strResultInfo [out]返回说明初始化结果的文字 // return Value: // -1 出错 // 0 成功 async Task <InitialResult> Initial( TargetInfo targetinfo, bool bIgnoreReferenceID, string reference_id) { string strResultInfo = ""; BerTree tree = new BerTree(); INIT_REQUEST struInit_request = new INIT_REQUEST(); // TargetInfo targetinfo = connection.TargetInfo; if (this._channel.Initialized == true) { return new InitialResult { Value = -1, ErrorInfo = "先前已经初始化过了,不应重复初始化" } } ; // 不能重复调用 struInit_request.m_strReferenceId = reference_id; struInit_request.m_strOptions = "yynnnnnnnnnnnnnnnn"; // "yyynynnyynynnnyn"; struInit_request.m_lPreferredMessageSize = 0x100000; ////16384; struInit_request.m_lExceptionalRecordSize = 0x100000; if (String.IsNullOrEmpty(targetinfo.UserName) == false) { struInit_request.m_strID = targetinfo.UserName; struInit_request.m_strPassword = targetinfo.Password; struInit_request.m_strGroupID = targetinfo.GroupID; struInit_request.m_nAuthenticationMethod = targetinfo.AuthenticationMethod; } else { struInit_request.m_strID = ""; struInit_request.m_strPassword = ""; struInit_request.m_strGroupID = ""; struInit_request.m_nAuthenticationMethod = -1; } struInit_request.m_strImplementationId = "DigitalPlatform"; struInit_request.m_strImplementationVersion = "1.2.0"; struInit_request.m_strImplementationName = "Z3950_library"; if (targetinfo.CharNegoUTF8 == true) { struInit_request.m_charNego = new CharsetNeogatiation(); struInit_request.m_charNego.EncodingLevelOID = CharsetNeogatiation.Utf8OID; // "1.0.10646.1.0.8"; // utf-8 struInit_request.m_charNego.RecordsInSelectedCharsets = (targetinfo.CharNegoRecordsUTF8 == true ? 1 : 0); } int nRet = tree.InitRequest(struInit_request, targetinfo.DefaultQueryTermEncoding, out byte[] baPackage); if (nRet == -1) { return new InitialResult { Value = -1, ErrorInfo = "CBERTree::InitRequest() fail!" } } ; if (this._channel.Connected == false) { this.CloseConnection(); return(new InitialResult { Value = -1, ErrorInfo = "socket尚未连接或者已经被关闭" }); } #if DUMPTOFILE DeleteFile("initrequest.bin"); DumpPackage("initrequest.bin", (char *)baPackage.GetData(), baPackage.GetSize()); DeleteFile("initrequest.txt"); tree.m_RootNode.DumpToFile("initrequest.txt"); #endif RecvResult result = await this._channel.SendAndRecv( baPackage); if (result.Value == -1) { return(new InitialResult(result)); } #if DUMPTOFILE DeleteFile("initresponse.bin"); DumpPackage("initresponse.bin", (char *)baOutPackage.GetData(), baOutPackage.GetSize()); #endif //////////////////////////////////////////////////////////////// BerTree tree1 = new BerTree(); tree1.m_RootNode.BuildPartTree(result.Package, 0, result.Package.Length, out int nTotalLen); #if DUMPTOFILE DeleteFile("InitResponse.txt"); tree1.m_RootNode.DumpDebugInfoToFile("InitResponse.txt"); #endif INIT_RESPONSE init_response = new INIT_RESPONSE(); nRet = BerTree.GetInfo_InitResponse(tree1.GetAPDuRoot(), ref init_response, out string strError); if (nRet == -1) { return new InitialResult { Value = -1, ErrorInfo = strError } } ; if (bIgnoreReferenceID == false) { // 可以帮助发现旧版本dp2zserver的错误 if (struInit_request.m_strReferenceId != init_response.m_strReferenceId) { strError = "请求的 reference id [" + struInit_request.m_strReferenceId + "] 和 响应的 reference id [" + init_response.m_strReferenceId + "] 不一致!"; return(new InitialResult { Value = -1, ErrorInfo = strError }); } } // 2007/11/5检查version和options bool bOption_0 = BerTree.GetBit(init_response.m_strOptions, 0); if (bOption_0 == false) { strError = "服务器响应的 option bit 0 表示不支持 search"; return(new InitialResult { Value = -1, ErrorInfo = strError }); } bool bOption_1 = BerTree.GetBit(init_response.m_strOptions, 1); if (bOption_1 == false) { strError = "服务器响应的 option bit 1 表示不支持 present"; return(new InitialResult { Value = -1, ErrorInfo = strError }); } if (init_response.m_nResult != 0) { strError = "Initial OK"; } else { strError = "Initial被拒绝。\r\n\r\n错误码 [" + init_response.m_lErrorCode.ToString() + "]\r\n错误消息[" + init_response.m_strErrorMessage + "]"; strResultInfo = BuildInitialResultInfo(init_response); return(new InitialResult { Value = -1, ErrorInfo = strError, ResultInfo = strResultInfo }); } /* * this->m_init_strOption = init_response.m_strOptions; * this->m_init_lPreferredMessageSize = init_response.m_lPreferredMessageSize; * this->m_init_lExceptionalRecordSize = init_response.m_lExceptionalRecordSize; * this->m_init_nResult = init_response.m_nResult; * */ this._channel.Initialized = true; // 字符集协商 if (init_response.m_charNego != null && BerTree.GetBit(init_response.m_strOptions, 17) == true) { if (init_response.m_charNego.EncodingLevelOID == CharsetNeogatiation.Utf8OID) { // 临时修改检索词的编码方式。 // 但是还无法反映到PropertyDialog上。最好能反馈。 targetinfo.DefaultQueryTermEncoding = Encoding.UTF8; targetinfo.Changed = true; if (init_response.m_charNego.RecordsInSelectedCharsets == 1) { this.ForcedRecordsEncoding = Encoding.UTF8; } } } strResultInfo = BuildInitialResultInfo(init_response); return(new InitialResult { Value = 0, ErrorInfo = strError, ResultInfo = strResultInfo }); }
// 解释包 void menuItem_explainContent_Click(object sender, EventArgs e) { TreeNode node = this.treeView_ber.SelectedNode; if (node == null) { MessageBox.Show(this, "尚未选定节点"); return; } BerNode bernode = (BerNode)node.Tag; if (TreeNodeLevel(node) != 1) { MessageBox.Show(this, "必须是Ber包根节点"); return; } BerNode root = bernode; int nRet = 0; string strError = ""; string strDebugInfo = "OK"; if (root.m_uTag == BerTree.z3950_initRequest) { // 观察Initial请求包 nRet = BerTree.GetInfo_InitRequest( root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_searchRequest) { nRet = BerTree.GetInfo_SearchRequest( root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_presentRequest) { nRet = BerTree.GetInfo_PresentRequest( root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_initResponse) { nRet = BerTree.GetInfo_InitResponse(root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_searchResponse) { nRet = BerTree.GetInfo_SearchResponse(root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_presentResponse) { RecordCollection records = null; SEARCH_RESPONSE search_response = new SEARCH_RESPONSE(); nRet = BerTree.GetInfo_PresentResponse(root, ref search_response, out records, true, out strError); } if (nRet == -1) { MessageBox.Show(this, strError); } else { MessageBox.Show(this, strDebugInfo); } }