// 编码(构造) Search响应包 int Encode_SearchResponse(SearchRequestInfo info, out byte[] baPackage, out string strError) { baPackage = null; int nRet = 0; long lRet = 0; strError = ""; DiagFormat diag = null; BerTree tree = new BerTree(); BerNode root = null; long lSearchStatus = 0; // 0 失败;1成功 long lHitCount = 0; string strQueryXml = ""; // 根据逆波兰表进行检索 // return: // -1 error // 0 succeed nRet = BuildQueryXml( info.m_dbnames, info.m_rpnRoot, out strQueryXml, out strError); if (nRet == -1) { SetPresentDiagRecord(ref diag, 2, // temporary system error strError); } string strResultSetName = info.m_strResultSetName; if (String.IsNullOrEmpty(strResultSetName) == true) strResultSetName = "default"; if (diag == null) { lRet = _channel.Search(null, strQueryXml, strResultSetName, "", // strOutputStyle out strError); /* // 测试检索失败 lRet = -1; strError = "测试检索失败"; * */ if (lRet == -1) { lSearchStatus = 0; // failed SetPresentDiagRecord(ref diag, 2, // temporary system error strError); } else { lHitCount = lRet; lSearchStatus = 1; // succeed } } root = tree.m_RootNode.NewChildConstructedNode( BerTree.z3950_searchResponse, BerNode.ASN1_CONTEXT); // reference id if (String.IsNullOrEmpty(info.m_strReferenceId) == false) { root.NewChildCharNode(BerTree.z3950_ReferenceId, BerNode.ASN1_CONTEXT, Encoding.UTF8.GetBytes(info.m_strReferenceId)); } // resultCount root.NewChildIntegerNode(BerTree.z3950_resultCount, // 23 BerNode.ASN1_CONTEXT, // ASNI_PRIMITIVE BUG!!!! BitConverter.GetBytes((long)lHitCount)); // numberOfRecordsReturned root.NewChildIntegerNode(BerTree.z3950_NumberOfRecordsReturned, // 24 BerNode.ASN1_CONTEXT, // ASNI_PRIMITIVE BUG!!!! BitConverter.GetBytes((long)0/*info.m_lNumberOfRecordReturned*/)); // 0 // nextResultSetPosition root.NewChildIntegerNode(BerTree.z3950_NextResultSetPosition, // 25 BerNode.ASN1_CONTEXT, // ASNI_PRIMITIVE BUG!!!! BitConverter.GetBytes((long)1/*info.m_lNextResultSetPosition*/)); // 2007/11/7 原来本项位置不对,现在移动到这里 // bool // searchStatus root.NewChildIntegerNode(BerTree.z3950_searchStatus, // 22 BerNode.ASN1_CONTEXT, // ASNI_PRIMITIVE BUG!!!! BitConverter.GetBytes((long)lSearchStatus)); // resultSetStatus OPTIONAL // 2007/11/7 // presentStatus root.NewChildIntegerNode(BerTree.z3950_presentStatus, // 27 BerNode.ASN1_CONTEXT, // ASNI_PRIMITIVE BUG!!!! BitConverter.GetBytes((long)0)); // 诊断记录 if (diag != null) { BerNode nodeDiagRoot = root.NewChildConstructedNode(BerTree.z3950_nonSurrogateDiagnostic, // 130 BerNode.ASN1_CONTEXT); diag.BuildBer(nodeDiagRoot); } baPackage = null; root.EncodeBERPackage(ref baPackage); return 0; }
// 解码Search请求包 public static int Decode_SearchRequest( BerNode root, out SearchRequestInfo info, out string strError) { strError = ""; int nRet = 0; info = new SearchRequestInfo(); Debug.Assert(root != null, ""); if (root.m_uTag != BerTree.z3950_searchRequest) { strError = "root tag is not z3950_searchRequest"; return -1; } for (int i = 0; i < root.ChildrenCollection.Count; i++) { BerNode node = root.ChildrenCollection[i]; switch (node.m_uTag) { case BerTree.z3950_ReferenceId: // 2 info.m_strReferenceId = node.GetCharNodeData(); break; case BerTree.z3950_smallSetUpperBound: // 13 smallSetUpperBound (Integer) info.m_lSmallSetUpperBound = node.GetIntegerNodeData(); break; case BerTree.z3950_largeSetLowerBound: // 14 largeSetLowerBound (Integer) info.m_lLargeSetLowerBound = node.GetIntegerNodeData(); break; case BerTree.z3950_mediumSetPresentNumber: // 15 mediumSetPresentNumber (Integer) info.m_lMediumSetPresentNumber = node.GetIntegerNodeData(); break; case BerTree.z3950_replaceIndicator: // 16 replaceIndicator, (boolean) info.m_lReplaceIndicator = node.GetIntegerNodeData(); break; case BerTree.z3950_resultSetName: // 17 resultSetName (string) info.m_strResultSetName = node.GetCharNodeData(); break; case BerTree.z3950_databaseNames: // 18 dbNames (sequence) /* // sequence is constructed, // have child with case = 105, (string) m_saDBName.RemoveAll(); DecodeDBName(pNode, m_saDBName, m_bIsCharSetUTF8); * */ { List<string> dbnames = null; nRet = DecodeDbnames(node, out dbnames, out strError); if (nRet == -1) return -1; info.m_dbnames = dbnames; } break; case BerTree.z3950_query: // 21 query (query) // DecodeSearchQuery(pNode, m_strSQLWhere, pRPNStructureRoot); { BerNode rpn_root = GetRPNStructureRoot(node, out strError); if (rpn_root == null) return -1; info.m_rpnRoot = rpn_root; } break; default: break; } } return 0; }