public static string BuildInitialResultInfo(INIT_RESPONSE info) { string strText = ""; strText += "reference-id:\t" + info.m_strReferenceId + "\r\n"; strText += "options:\t" + info.m_strOptions + "\r\n"; strText += "preferred-message-size:\t" + info.m_lPreferredMessageSize.ToString() + "\r\n"; strText += "exceptional-record-size:\t" + info.m_lExceptionalRecordSize.ToString() + "\r\n"; strText += "\r\n--- Result Code ---\r\n"; strText += "initial-result:\t" + info.m_nResult.ToString() + "\r\n"; strText += "\r\n--- Implementation information ---\r\n"; strText += "implementation-id:\t" + info.m_strImplementationId + "\r\n"; strText += "implementation-name:\t" + info.m_strImplementationName + "\r\n"; strText += "implementation-version:\t" + info.m_strImplementationVersion + "\r\n"; strText += "\r\n--- Error Code and Message ---\r\n"; strText += "error-code:\t" + info.m_lErrorCode.ToString() + "\r\n"; strText += "error-messsage:\t" + info.m_strErrorMessage + "\r\n"; if (info.m_charNego != null) { strText += "\r\n--- Charset Negotiation Parameters ---\r\n"; if (BerTree.GetBit(info.m_strOptions, 17) == false) { strText += "options bit 17 is false, not allow negotiation\r\n"; } strText += "charnego-encoding-level-oid:\t" + info.m_charNego.EncodingLevelOID + "(note: UTF-8 is: " + CharsetNeogatiation.Utf8OID + ")\r\n"; strText += "charnego-records-in-selected-charsets:\t" + info.m_charNego.RecordsInSelectedCharsets.ToString() + "\r\n"; } return(strText); }
// 执行初始化 // 同步模式 // 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 }); }