Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit 5aab9fe

Browse files
authored
Merge pull request #81 from Jie2GG/Test
V3.0.7.0607
2 parents 3e3d07d + 82ef6b0 commit 5aab9fe

File tree

73 files changed

+58211
-1945
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+58211
-1945
lines changed

Native.Csharp.Sdk/Cqp/CqApi.cs

Lines changed: 112 additions & 78 deletions
Large diffs are not rendered by default.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
7+
namespace Native.Csharp.Sdk.Cqp.Other
8+
{
9+
/// <summary>
10+
/// <see cref="BinaryReader"/> 类的扩展方法集
11+
/// </summary>
12+
public static class BinaryReaderExpand
13+
{
14+
#region --公开方法--
15+
/// <summary>
16+
/// 获取基础流的剩余长度
17+
/// </summary>
18+
/// <param name="binary"></param>
19+
/// <returns></returns>
20+
public static long Length (this BinaryReader binary)
21+
{
22+
return binary.BaseStream.Length - binary.BaseStream.Position;
23+
}
24+
25+
/// <summary>
26+
/// 从字节数组中的指定点开始,从流中读取所有字节。
27+
/// </summary>
28+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
29+
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
30+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
31+
/// <exception cref="IOException">出现 I/O 错误。</exception>
32+
public static byte[] ReadAll_Ex (this BinaryReader binary)
33+
{
34+
return GetBinary (binary, binary.BaseStream.Length, false);
35+
}
36+
37+
/// <summary>
38+
/// 从字节数组中的指定点开始,从流中读取指定字节长度。
39+
/// </summary>
40+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
41+
/// <param name="len">要读取的字节数。</param>
42+
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
43+
/// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception>
44+
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
45+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
46+
/// <exception cref="IOException">出现 I/O 错误。</exception>
47+
public static byte[] ReadBin_Ex (this BinaryReader binary, long len)
48+
{
49+
return GetBinary (binary, len);
50+
}
51+
52+
/// <summary>
53+
/// 从字节数组中的指定点开始,从流中读取 2 字节长度并反序为 <see cref="int"/> 值。
54+
/// </summary>
55+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
56+
/// <returns>读入 2 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
57+
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
58+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
59+
/// <exception cref="IOException">出现 I/O 错误。</exception>
60+
public static short ReadInt16_Ex (this BinaryReader binary)
61+
{
62+
return BitConverter.ToInt16 (GetBinary (binary, 2, true), 0);
63+
}
64+
65+
/// <summary>
66+
/// 从字节数组中的指定点开始,从流中读取 4 字节长度并反序为 <see cref="int"/> 值。
67+
/// </summary>
68+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
69+
/// <returns>读入 4 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
70+
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
71+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
72+
/// <exception cref="IOException">出现 I/O 错误。</exception>
73+
public static int ReadInt32_Ex (this BinaryReader binary)
74+
{
75+
return BitConverter.ToInt32 (GetBinary (binary, 4, true), 0);
76+
}
77+
78+
/// <summary>
79+
/// 从字节数组中的指定点开始,从流中读取 8 字节长度并反序为 <see cref="long"/> 值。
80+
/// </summary>
81+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
82+
/// <returns>读入 8 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
83+
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
84+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
85+
/// <exception cref="IOException">出现 I/O 错误。</exception>
86+
public static long ReadInt64_Ex (this BinaryReader binary)
87+
{
88+
return BitConverter.ToInt64 (GetBinary (binary, 8, true), 0);
89+
}
90+
91+
/// <summary>
92+
/// 从字节数组中的指定点开始,从流中读取指定字节长度。
93+
/// </summary>
94+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
95+
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
96+
/// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception>
97+
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
98+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
99+
/// <exception cref="IOException">出现 I/O 错误。</exception>
100+
public static byte[] ReadToken_Ex (this BinaryReader binary)
101+
{
102+
short len = ReadInt16_Ex (binary);
103+
return GetBinary (binary, len);
104+
}
105+
106+
/// <summary>
107+
/// 从字节数组中的指定点开始,从流中读取指定编码的字符串。
108+
/// </summary>
109+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
110+
/// <param name="encoding"></param>
111+
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
112+
/// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception>
113+
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
114+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
115+
/// <exception cref="IOException">出现 I/O 错误。</exception>
116+
public static string ReadString_Ex (this BinaryReader binary, Encoding encoding = null)
117+
{
118+
if (encoding == null)
119+
{
120+
encoding = Encoding.ASCII;
121+
}
122+
123+
return encoding.GetString (ReadToken_Ex (binary));
124+
}
125+
#endregion
126+
127+
#region --私有方法--
128+
private static byte[] GetBinary (BinaryReader binary, long len, bool isReverse = false)
129+
{
130+
byte[] buffer = new byte[len];
131+
binary.Read (buffer, 0, buffer.Length);
132+
if (isReverse)
133+
{
134+
buffer = buffer.Reverse ().ToArray ();
135+
}
136+
return buffer;
137+
}
138+
#endregion
139+
}
140+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
7+
namespace Native.Csharp.Sdk.Cqp.Other
8+
{
9+
/// <summary>
10+
/// <see cref="BinaryReader"/> 类的扩展方法集
11+
/// </summary>
12+
public static class BinaryWriterExpand
13+
{
14+
#region --公开方法--
15+
/// <summary>
16+
/// 将写入基础流的 <see cref="short"/> 数值
17+
/// </summary>
18+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
19+
/// <param name="value">要写入的值</param>
20+
/// <exception cref="IOException">出现 I/O 错误。</exception>
21+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
22+
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
23+
public static void Write_Ex (this BinaryWriter binary, short value)
24+
{
25+
SetBinary (binary, BitConverter.GetBytes (value), true);
26+
}
27+
28+
/// <summary>
29+
/// 将写入基础流的 <see cref="int"/> 数值
30+
/// </summary>
31+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
32+
/// <param name="value">要写入的值</param>
33+
/// <exception cref="IOException">出现 I/O 错误。</exception>
34+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
35+
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
36+
public static void Write_Ex (this BinaryWriter binary, int value)
37+
{
38+
SetBinary (binary, BitConverter.GetBytes (value), true);
39+
}
40+
41+
/// <summary>
42+
/// 将写入基础流的 <see cref="long"/> 数值
43+
/// </summary>
44+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
45+
/// <param name="value">要写入的值</param>
46+
/// <exception cref="IOException">出现 I/O 错误。</exception>
47+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
48+
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
49+
public static void Write_Ex (this BinaryWriter binary, long value)
50+
{
51+
SetBinary (binary, BitConverter.GetBytes (value), true);
52+
}
53+
54+
/// <summary>
55+
/// 将写入基础流的 <see cref="string"/> 数值
56+
/// </summary>
57+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
58+
/// <param name="value">要写入的值</param>
59+
/// <exception cref="IOException">出现 I/O 错误。</exception>
60+
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
61+
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
62+
public static void Write_Ex (this BinaryWriter binary, string value)
63+
{
64+
byte[] buffer = Encoding.Default.GetBytes (value);
65+
Write_Ex (binary, (short)buffer.Length);
66+
SetBinary (binary, buffer, false);
67+
}
68+
69+
/// <summary>
70+
/// 将基础流转换为相同的字节数组
71+
/// </summary>
72+
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
73+
public static byte[] ToArray (this BinaryWriter binary)
74+
{
75+
long position = binary.BaseStream.Position; // 记录原指针位置
76+
77+
byte[] buffer = new byte[binary.BaseStream.Length];
78+
binary.BaseStream.Position = 0; // 设置读取位置为 0
79+
binary.BaseStream.Read (buffer, 0, buffer.Length);
80+
81+
binary.BaseStream.Position = position; // 还原原指针位置
82+
return buffer;
83+
}
84+
#endregion
85+
86+
#region --私有方法--
87+
private static void SetBinary (BinaryWriter binary, byte[] buffer, bool isReverse)
88+
{
89+
if (isReverse)
90+
{
91+
buffer = buffer.Reverse ().ToArray ();
92+
}
93+
binary.Write (buffer);
94+
}
95+
#endregion
96+
}
97+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
7+
namespace Native.Csharp.Sdk.Cqp.Other
8+
{
9+
/// <summary>
10+
/// 其它类扩展方法集
11+
/// </summary>
12+
public static class OtherExpand
13+
{
14+
#region --Kernel32--
15+
[DllImport ("kernel32.dll", EntryPoint = "lstrlenA", CharSet = CharSet.Ansi)]
16+
internal extern static int LstrlenA (IntPtr ptr);
17+
#endregion
18+
19+
/// <summary>
20+
/// 获取 Unix 时间戳的 <see cref="DateTime"/> 表示形式
21+
/// </summary>
22+
/// <param name="unixTime">unix 时间戳</param>
23+
/// <returns></returns>
24+
public static DateTime ToDateTime (this long unixTime)
25+
{
26+
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1, 1));
27+
TimeSpan toNow = new TimeSpan (unixTime);
28+
DateTime daTime = dtStart.Add (toNow);
29+
return daTime;
30+
}
31+
32+
/// <summary>
33+
/// 获取 Unix 时间戳的 <see cref="DateTime"/> 表示形式
34+
/// </summary>
35+
/// <param name="unixTime">unix 时间戳</param>
36+
/// <returns></returns>
37+
public static DateTime ToDateTime (this int unixTime)
38+
{
39+
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1, 1));
40+
TimeSpan toNow = new TimeSpan (unixTime);
41+
DateTime daTime = dtStart.Add (toNow);
42+
return daTime;
43+
}
44+
45+
/// <summary>
46+
/// 转换字符串的 <see cref="IntPtr"/> 实例对象
47+
/// </summary>
48+
/// <param name="value">将转换的字符串</param>
49+
/// <param name="encoding">目标编码格式</param>
50+
/// <returns></returns>
51+
public static IntPtr ToIntPtr (this string source, Encoding encoding = null)
52+
{
53+
if (encoding == null)
54+
{
55+
encoding = Encoding.ASCII;
56+
}
57+
byte[] buffer = encoding.GetBytes (source);
58+
GCHandle hobj = GCHandle.Alloc (buffer, GCHandleType.Pinned);
59+
return hobj.AddrOfPinnedObject ();
60+
}
61+
62+
/// <summary>
63+
/// 读取指针内所有的字节数组并编码为指定字符串
64+
/// </summary>
65+
/// <param name="strPtr">字符串的 <see cref="IntPtr"/> 对象</param>
66+
/// <param name="encoding">目标编码格式</param>
67+
/// <returns></returns>
68+
public static string ToString (this IntPtr strPtr, Encoding encoding = null)
69+
{
70+
if (encoding == null)
71+
{
72+
encoding = Encoding.Default;
73+
}
74+
75+
int len = LstrlenA (strPtr); //获取指针中数据的长度
76+
if (len == 0)
77+
{
78+
return string.Empty;
79+
}
80+
81+
byte[] buffer = new byte[len];
82+
Marshal.Copy (strPtr, buffer, 0, len);
83+
return encoding.GetString (buffer);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)