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

Commit

Permalink
Merge pull request #81 from Jie2GG/Test
Browse files Browse the repository at this point in the history
V3.0.7.0607
  • Loading branch information
Jie2GG authored Jun 8, 2019
2 parents 3e3d07d + 82ef6b0 commit 5aab9fe
Show file tree
Hide file tree
Showing 73 changed files with 58,211 additions and 1,945 deletions.
190 changes: 112 additions & 78 deletions Native.Csharp.Sdk/Cqp/CqApi.cs

Large diffs are not rendered by default.

140 changes: 140 additions & 0 deletions Native.Csharp.Sdk/Cqp/Other/BinaryReaderExpand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Native.Csharp.Sdk.Cqp.Other
{
/// <summary>
/// <see cref="BinaryReader"/> 类的扩展方法集
/// </summary>
public static class BinaryReaderExpand
{
#region --公开方法--
/// <summary>
/// 获取基础流的剩余长度
/// </summary>
/// <param name="binary"></param>
/// <returns></returns>
public static long Length (this BinaryReader binary)
{
return binary.BaseStream.Length - binary.BaseStream.Position;
}

/// <summary>
/// 从字节数组中的指定点开始,从流中读取所有字节。
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="IOException">出现 I/O 错误。</exception>
public static byte[] ReadAll_Ex (this BinaryReader binary)
{
return GetBinary (binary, binary.BaseStream.Length, false);
}

/// <summary>
/// 从字节数组中的指定点开始,从流中读取指定字节长度。
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <param name="len">要读取的字节数。</param>
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
/// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception>
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="IOException">出现 I/O 错误。</exception>
public static byte[] ReadBin_Ex (this BinaryReader binary, long len)
{
return GetBinary (binary, len);
}

/// <summary>
/// 从字节数组中的指定点开始,从流中读取 2 字节长度并反序为 <see cref="int"/> 值。
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <returns>读入 2 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="IOException">出现 I/O 错误。</exception>
public static short ReadInt16_Ex (this BinaryReader binary)
{
return BitConverter.ToInt16 (GetBinary (binary, 2, true), 0);
}

/// <summary>
/// 从字节数组中的指定点开始,从流中读取 4 字节长度并反序为 <see cref="int"/> 值。
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <returns>读入 4 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="IOException">出现 I/O 错误。</exception>
public static int ReadInt32_Ex (this BinaryReader binary)
{
return BitConverter.ToInt32 (GetBinary (binary, 4, true), 0);
}

/// <summary>
/// 从字节数组中的指定点开始,从流中读取 8 字节长度并反序为 <see cref="long"/> 值。
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <returns>读入 8 字节的结果值,如果可用的字节没有那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="IOException">出现 I/O 错误。</exception>
public static long ReadInt64_Ex (this BinaryReader binary)
{
return BitConverter.ToInt64 (GetBinary (binary, 8, true), 0);
}

/// <summary>
/// 从字节数组中的指定点开始,从流中读取指定字节长度。
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
/// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception>
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="IOException">出现 I/O 错误。</exception>
public static byte[] ReadToken_Ex (this BinaryReader binary)
{
short len = ReadInt16_Ex (binary);
return GetBinary (binary, len);
}

/// <summary>
/// 从字节数组中的指定点开始,从流中读取指定编码的字符串。
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <param name="encoding"></param>
/// <returns>读入 buffer 的字节数。 如果可用的字节没有请求的那么多,此数可能小于所请求的字节数;如果到达了流的末尾,此数可能为零。</returns>
/// <exception cref="ArgumentOutOfRangeException">len 为负数。</exception>
/// <exception cref="ArgumentException">已解码要读取的字符数超过了边界。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="IOException">出现 I/O 错误。</exception>
public static string ReadString_Ex (this BinaryReader binary, Encoding encoding = null)
{
if (encoding == null)
{
encoding = Encoding.ASCII;
}

return encoding.GetString (ReadToken_Ex (binary));
}
#endregion

#region --私有方法--
private static byte[] GetBinary (BinaryReader binary, long len, bool isReverse = false)
{
byte[] buffer = new byte[len];
binary.Read (buffer, 0, buffer.Length);
if (isReverse)
{
buffer = buffer.Reverse ().ToArray ();
}
return buffer;
}
#endregion
}
}
97 changes: 97 additions & 0 deletions Native.Csharp.Sdk/Cqp/Other/BinaryWriterExpand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Native.Csharp.Sdk.Cqp.Other
{
/// <summary>
/// <see cref="BinaryReader"/> 类的扩展方法集
/// </summary>
public static class BinaryWriterExpand
{
#region --公开方法--
/// <summary>
/// 将写入基础流的 <see cref="short"/> 数值
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <param name="value">要写入的值</param>
/// <exception cref="IOException">出现 I/O 错误。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
public static void Write_Ex (this BinaryWriter binary, short value)
{
SetBinary (binary, BitConverter.GetBytes (value), true);
}

/// <summary>
/// 将写入基础流的 <see cref="int"/> 数值
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <param name="value">要写入的值</param>
/// <exception cref="IOException">出现 I/O 错误。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
public static void Write_Ex (this BinaryWriter binary, int value)
{
SetBinary (binary, BitConverter.GetBytes (value), true);
}

/// <summary>
/// 将写入基础流的 <see cref="long"/> 数值
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <param name="value">要写入的值</param>
/// <exception cref="IOException">出现 I/O 错误。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
public static void Write_Ex (this BinaryWriter binary, long value)
{
SetBinary (binary, BitConverter.GetBytes (value), true);
}

/// <summary>
/// 将写入基础流的 <see cref="string"/> 数值
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
/// <param name="value">要写入的值</param>
/// <exception cref="IOException">出现 I/O 错误。</exception>
/// <exception cref="ObjectDisposedException">流已关闭。</exception>
/// <exception cref="ArgumentNullException">buffer 为 null。</exception>
public static void Write_Ex (this BinaryWriter binary, string value)
{
byte[] buffer = Encoding.Default.GetBytes (value);
Write_Ex (binary, (short)buffer.Length);
SetBinary (binary, buffer, false);
}

/// <summary>
/// 将基础流转换为相同的字节数组
/// </summary>
/// <param name="binary">基础 <see cref="BinaryWriter"/> 对象</param>
public static byte[] ToArray (this BinaryWriter binary)
{
long position = binary.BaseStream.Position; // 记录原指针位置

byte[] buffer = new byte[binary.BaseStream.Length];
binary.BaseStream.Position = 0; // 设置读取位置为 0
binary.BaseStream.Read (buffer, 0, buffer.Length);

binary.BaseStream.Position = position; // 还原原指针位置
return buffer;
}
#endregion

#region --私有方法--
private static void SetBinary (BinaryWriter binary, byte[] buffer, bool isReverse)
{
if (isReverse)
{
buffer = buffer.Reverse ().ToArray ();
}
binary.Write (buffer);
}
#endregion
}
}
86 changes: 86 additions & 0 deletions Native.Csharp.Sdk/Cqp/Other/OtherExpand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Native.Csharp.Sdk.Cqp.Other
{
/// <summary>
/// 其它类扩展方法集
/// </summary>
public static class OtherExpand
{
#region --Kernel32--
[DllImport ("kernel32.dll", EntryPoint = "lstrlenA", CharSet = CharSet.Ansi)]
internal extern static int LstrlenA (IntPtr ptr);
#endregion

/// <summary>
/// 获取 Unix 时间戳的 <see cref="DateTime"/> 表示形式
/// </summary>
/// <param name="unixTime">unix 时间戳</param>
/// <returns></returns>
public static DateTime ToDateTime (this long unixTime)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1, 1));
TimeSpan toNow = new TimeSpan (unixTime);
DateTime daTime = dtStart.Add (toNow);
return daTime;
}

/// <summary>
/// 获取 Unix 时间戳的 <see cref="DateTime"/> 表示形式
/// </summary>
/// <param name="unixTime">unix 时间戳</param>
/// <returns></returns>
public static DateTime ToDateTime (this int unixTime)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1, 1));
TimeSpan toNow = new TimeSpan (unixTime);
DateTime daTime = dtStart.Add (toNow);
return daTime;
}

/// <summary>
/// 转换字符串的 <see cref="IntPtr"/> 实例对象
/// </summary>
/// <param name="value">将转换的字符串</param>
/// <param name="encoding">目标编码格式</param>
/// <returns></returns>
public static IntPtr ToIntPtr (this string source, Encoding encoding = null)
{
if (encoding == null)
{
encoding = Encoding.ASCII;
}
byte[] buffer = encoding.GetBytes (source);
GCHandle hobj = GCHandle.Alloc (buffer, GCHandleType.Pinned);
return hobj.AddrOfPinnedObject ();
}

/// <summary>
/// 读取指针内所有的字节数组并编码为指定字符串
/// </summary>
/// <param name="strPtr">字符串的 <see cref="IntPtr"/> 对象</param>
/// <param name="encoding">目标编码格式</param>
/// <returns></returns>
public static string ToString (this IntPtr strPtr, Encoding encoding = null)
{
if (encoding == null)
{
encoding = Encoding.Default;
}

int len = LstrlenA (strPtr); //获取指针中数据的长度
if (len == 0)
{
return string.Empty;
}

byte[] buffer = new byte[len];
Marshal.Copy (strPtr, buffer, 0, len);
return encoding.GetString (buffer);
}
}
}
Loading

0 comments on commit 5aab9fe

Please sign in to comment.