编写后台数据交互API
This commit is contained in:
@@ -18,8 +18,8 @@ namespace Interact
|
|||||||
private static Thread readLoopThread;
|
private static Thread readLoopThread;
|
||||||
//数据发送线程
|
//数据发送线程
|
||||||
private static Thread sendLoopThread;
|
private static Thread sendLoopThread;
|
||||||
//某些请求需要持续的等待服务器返回数据包,然后将所有数据包处理后提供给用户
|
//某些请求需要持续的等待服务器返回数据包,然后将所有数据包处理后提供给用户,
|
||||||
// ,此字段用于暂存这些未接收完全的数据包。它的key是对应请求的Token字串。
|
// 此字段用于暂存这些未接收完全的数据包。它的key是对应请求的Token字串。
|
||||||
private static Dictionary<string, object> workingDictionary;
|
private static Dictionary<string, object> workingDictionary;
|
||||||
|
|
||||||
//属性控制字段,任何地方都不应该修改它们
|
//属性控制字段,任何地方都不应该修改它们
|
||||||
@@ -13,7 +13,7 @@ namespace Interact
|
|||||||
{
|
{
|
||||||
public partial class StormClient
|
public partial class StormClient
|
||||||
{
|
{
|
||||||
//处理连接中断异常
|
//处理连接中断异常。
|
||||||
private static void HandleConnectionBroken(Exception ex)
|
private static void HandleConnectionBroken(Exception ex)
|
||||||
{
|
{
|
||||||
lock (statusLocker)
|
lock (statusLocker)
|
||||||
@@ -44,10 +44,13 @@ namespace Interact
|
|||||||
HandleMessage(jsonObj, data);
|
HandleMessage(jsonObj, data);
|
||||||
break;
|
break;
|
||||||
case Operations.SendMessage:
|
case Operations.SendMessage:
|
||||||
|
HandleSendMessageResult(GetResultHead(jsonObj));
|
||||||
break;
|
break;
|
||||||
case Operations.Offline:
|
case Operations.Offline:
|
||||||
|
HandlePanic(GetResultHead(jsonObj));
|
||||||
break;
|
break;
|
||||||
case Operations.UpdateUserInfo:
|
case Operations.UpdateUserInfo:
|
||||||
|
HandleUpdateUserInfoDone(GetResultHead(jsonObj));
|
||||||
break;
|
break;
|
||||||
case Operations.GetUsers:
|
case Operations.GetUsers:
|
||||||
HandleGetUsersPack(jsonObj, data);
|
HandleGetUsersPack(jsonObj, data);
|
||||||
@@ -72,8 +75,8 @@ namespace Interact
|
|||||||
//处理服务器异常断开连接消息
|
//处理服务器异常断开连接消息
|
||||||
private static void HandlePanic(ResultHead head)
|
private static void HandlePanic(ResultHead head)
|
||||||
{
|
{
|
||||||
ConnectionBrokenException ex = new ConnectionBrokenException(head.Error);
|
SetStatus(ClientStatus.Stopped);
|
||||||
HandleConnectionBroken(ex);
|
OnPanic?.Invoke(head);
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理登录反馈消息
|
//处理登录反馈消息
|
||||||
@@ -118,6 +121,25 @@ namespace Interact
|
|||||||
OnMessage?.Invoke(message);
|
OnMessage?.Invoke(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//发送消息执行结果
|
||||||
|
private static void HandleSendMessageResult(ResultHead head)
|
||||||
|
{
|
||||||
|
OnSendMessageDone?.Invoke(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
//处理掉线、强制登出
|
||||||
|
private static void HandleOffiline(ResultHead head)
|
||||||
|
{
|
||||||
|
SetStatus(ClientStatus.Stopped);
|
||||||
|
OnOffline?.Invoke(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
//处理更改信息结果
|
||||||
|
private static void HandleUpdateUserInfoDone(ResultHead head)
|
||||||
|
{
|
||||||
|
OnUpdateUserInfoDone?.Invoke(head);
|
||||||
|
}
|
||||||
|
|
||||||
//处理获取用户列表的不连续返回包。当所有包接收完毕后返回给客户
|
//处理获取用户列表的不连续返回包。当所有包接收完毕后返回给客户
|
||||||
private static void HandleGetUsersPack(JObject head, byte[] data)
|
private static void HandleGetUsersPack(JObject head, byte[] data)
|
||||||
{
|
{
|
||||||
@@ -16,8 +16,12 @@ namespace Interact
|
|||||||
{
|
{
|
||||||
Packet packet = null;
|
Packet packet = null;
|
||||||
NetworkStream stream = tcpClient.GetStream();
|
NetworkStream stream = tcpClient.GetStream();
|
||||||
|
//json序列化时忽略值为null的项
|
||||||
|
JsonSerializerSettings setting = new JsonSerializerSettings();
|
||||||
|
setting.NullValueHandling = NullValueHandling.Ignore;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
//阻塞式的从发送队列中取出数据
|
||||||
foreach (Packet i in sendQueue.GetConsumingEnumerable())
|
foreach (Packet i in sendQueue.GetConsumingEnumerable())
|
||||||
{
|
{
|
||||||
packet = i; //为了在代码块外引用i
|
packet = i; //为了在代码块外引用i
|
||||||
@@ -26,7 +30,7 @@ namespace Interact
|
|||||||
break;
|
break;
|
||||||
//向tcp连接写数据
|
//向tcp连接写数据
|
||||||
byte[] lenBuffer; //数据长度缓存
|
byte[] lenBuffer; //数据长度缓存
|
||||||
byte[] headData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(packet.Head));
|
byte[] headData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(packet.Head, setting));
|
||||||
//写包头
|
//写包头
|
||||||
lenBuffer = BitConverter.GetBytes((UInt32)IPAddress.HostToNetworkOrder(headData.Length));
|
lenBuffer = BitConverter.GetBytes((UInt32)IPAddress.HostToNetworkOrder(headData.Length));
|
||||||
stream.Write(lenBuffer, 0, 4);
|
stream.Write(lenBuffer, 0, 4);
|
||||||
+46
-20
@@ -24,28 +24,43 @@ namespace Interact
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static partial class StormClient
|
public static partial class StormClient
|
||||||
{
|
{
|
||||||
//连接中断事件
|
/// <summary>
|
||||||
|
/// 连接中断事件。注意,此事件并不保证连接中断时总是触发,例如主动关闭连接、登出操作
|
||||||
|
/// </summary>
|
||||||
public static event ConnectionBrokenHandler OnDisconnect;
|
public static event ConnectionBrokenHandler OnDisconnect;
|
||||||
//服务器异常消息处理事件
|
/// <summary>
|
||||||
|
/// 服务器异常消息处理事件
|
||||||
|
/// </summary>
|
||||||
public static event ResultHandler OnPanic;
|
public static event ResultHandler OnPanic;
|
||||||
//服务器强制注销登陆事件
|
/// <summary>
|
||||||
|
/// 服务器强制注销登陆事件
|
||||||
|
/// </summary>
|
||||||
public static event ResultHandler OnOffline;
|
public static event ResultHandler OnOffline;
|
||||||
//消息到达事件
|
/// <summary>
|
||||||
|
/// 消息到达事件
|
||||||
|
/// </summary>
|
||||||
public static event MessageHandler OnMessage;
|
public static event MessageHandler OnMessage;
|
||||||
//发送消息处理完成反馈事件(仅在DoesSendMessageReturn为true时有效)
|
/// <summary>
|
||||||
|
/// 发送消息处理完成反馈事件(仅在DoesSendMessageReturn为true时有效)
|
||||||
|
/// </summary>
|
||||||
public static event ResultHandler OnSendMessageDone;
|
public static event ResultHandler OnSendMessageDone;
|
||||||
//更新用户信息结果处理事件
|
/// <summary>
|
||||||
|
/// 更新用户信息结果处理事件
|
||||||
|
/// </summary>
|
||||||
public static event ResultHandler OnUpdateUserInfoDone;
|
public static event ResultHandler OnUpdateUserInfoDone;
|
||||||
//登录结果处理事件
|
/// <summary>
|
||||||
|
/// 登录结果处理事件
|
||||||
|
/// </summary>
|
||||||
public static event LoginDoneHandler OnLoginDone;
|
public static event LoginDoneHandler OnLoginDone;
|
||||||
//获取用户列表结果处理事件
|
/// <summary>
|
||||||
|
/// 获取用户列表结果处理事件
|
||||||
|
/// </summary>
|
||||||
public static event GetUserListDoneHandler OnGetUserListDone;
|
public static event GetUserListDoneHandler OnGetUserListDone;
|
||||||
|
|
||||||
//当前连接状态
|
//当前连接状态
|
||||||
public static ClientStatus Status
|
public static ClientStatus Status
|
||||||
{
|
{
|
||||||
get { lock (statusLocker) { return clientStatus; } }
|
get { lock (statusLocker) { return clientStatus; } }
|
||||||
set { lock (statusLocker) { clientStatus = value; } }
|
|
||||||
}
|
}
|
||||||
//发送消息时是否要求服务器返回处理结果
|
//发送消息时是否要求服务器返回处理结果
|
||||||
public static bool DoesSendMessageReturn
|
public static bool DoesSendMessageReturn
|
||||||
@@ -60,7 +75,13 @@ namespace Interact
|
|||||||
tcpClient = new TcpClient();
|
tcpClient = new TcpClient();
|
||||||
sendQueue = new BlockingCollection<Packet>();
|
sendQueue = new BlockingCollection<Packet>();
|
||||||
workingDictionary = new Dictionary<string, object>();
|
workingDictionary = new Dictionary<string, object>();
|
||||||
Status = ClientStatus.Uninitialized;
|
SetStatus(ClientStatus.Uninitialized);
|
||||||
|
}
|
||||||
|
|
||||||
|
//设置当前连接状态
|
||||||
|
internal static void SetStatus(ClientStatus value)
|
||||||
|
{
|
||||||
|
lock (statusLocker) { clientStatus = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -89,7 +110,7 @@ namespace Interact
|
|||||||
sendLoopThread = new Thread(new ThreadStart(SendLoop));
|
sendLoopThread = new Thread(new ThreadStart(SendLoop));
|
||||||
readLoopThread = new Thread(new ThreadStart(ReadLoop));
|
readLoopThread = new Thread(new ThreadStart(ReadLoop));
|
||||||
sendLoopThread.IsBackground = readLoopThread.IsBackground = true;
|
sendLoopThread.IsBackground = readLoopThread.IsBackground = true;
|
||||||
StormClient.Status = ClientStatus.Running;
|
SetStatus(ClientStatus.Running);
|
||||||
sendLoopThread.Start();
|
sendLoopThread.Start();
|
||||||
readLoopThread.Start();
|
readLoopThread.Start();
|
||||||
return true;
|
return true;
|
||||||
@@ -125,7 +146,7 @@ namespace Interact
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">要发送的消息文本</param>
|
/// <param name="text">要发送的消息文本</param>
|
||||||
/// <param name="to">消息接收者</param>
|
/// <param name="to">消息接收者</param>
|
||||||
/// <param name="callback">数据发送完毕时回调函数</param>
|
/// <param name="callback">数据发送完毕时回调函数。</param>
|
||||||
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
||||||
public static bool QueueSendMessage(Message message, Action<ResultHead> callback = null)
|
public static bool QueueSendMessage(Message message, Action<ResultHead> callback = null)
|
||||||
{
|
{
|
||||||
@@ -148,7 +169,7 @@ namespace Interact
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求登出。异步,此方法将请求放入请求队列后返回。
|
/// 请求登出。异步,此方法将请求放入请求队列后返回。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="callback">数据发送完毕时回调函数</param>
|
/// <param name="callback">数据发送完毕时回调函数。</param>
|
||||||
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
||||||
public static bool QueueLogout(Action<ResultHead> callback = null)
|
public static bool QueueLogout(Action<ResultHead> callback = null)
|
||||||
{
|
{
|
||||||
@@ -163,13 +184,15 @@ namespace Interact
|
|||||||
Data = null,
|
Data = null,
|
||||||
CallBack = callback
|
CallBack = callback
|
||||||
};
|
};
|
||||||
return Send(packet);
|
bool success = Send(packet);
|
||||||
|
SetStatus(ClientStatus.Stopped);
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求用户列表。异步,此方法将请求放入请求队列后返回。
|
/// 请求用户列表。异步,此方法将请求放入请求队列后返回。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="callback">数据发送完毕时回调函数</param>
|
/// <param name="callback">数据发送完毕时回调函数。</param>
|
||||||
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
||||||
public static bool QueueGetUserList(Action<ResultHead> callback = null)
|
public static bool QueueGetUserList(Action<ResultHead> callback = null)
|
||||||
{
|
{
|
||||||
@@ -190,12 +213,13 @@ namespace Interact
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求更新当前登录用户信息。异步,此方法将请求放入请求队列后返回。
|
/// 请求更新当前登录用户信息。异步,此方法将请求放入请求队列后返回。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="callback">数据发送完毕时回调函数</param>
|
/// <param name="userInfo">新的用户信息,如果某项值为null,则不改变该项。</param>
|
||||||
|
/// <param name="callback">数据发送完毕时回调函数。</param>
|
||||||
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
/// <returns>成功将请求加入发送队列返回true,否则返回false。</returns>
|
||||||
public static bool QueueUpdateUserInfo(UserInfo userInfo, Action<ResultHead> callback = null)
|
public static bool QueueUpdateUserInfo(UserInfo userInfo, Action<ResultHead> callback = null)
|
||||||
{
|
{
|
||||||
byte[] photoData = null; //头像数据
|
byte[] photoData = null; //头像数据
|
||||||
//将头像数据转换到字符数组
|
//将头像数据转换到字符数组
|
||||||
if (userInfo.Photo != null)
|
if (userInfo.Photo != null)
|
||||||
{
|
{
|
||||||
MemoryStream memStream = new MemoryStream();
|
MemoryStream memStream = new MemoryStream();
|
||||||
@@ -207,7 +231,7 @@ namespace Interact
|
|||||||
//构建数据包
|
//构建数据包
|
||||||
JsonUserInfoHead jsonObj = new JsonUserInfoHead()
|
JsonUserInfoHead jsonObj = new JsonUserInfoHead()
|
||||||
{
|
{
|
||||||
Token = "",
|
Token = Guid.NewGuid().ToString(),
|
||||||
Operation = Operations.UpdateUserInfo,
|
Operation = Operations.UpdateUserInfo,
|
||||||
NickName = userInfo.NickName,
|
NickName = userInfo.NickName,
|
||||||
Password = userInfo.Password,
|
Password = userInfo.Password,
|
||||||
@@ -223,7 +247,9 @@ namespace Interact
|
|||||||
return Send(packet);
|
return Send(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
//客户端连接状态
|
/// <summary>
|
||||||
|
/// 客户端连接状态
|
||||||
|
/// </summary>
|
||||||
public enum ClientStatus
|
public enum ClientStatus
|
||||||
{
|
{
|
||||||
Uninitialized, //未启动
|
Uninitialized, //未启动
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Interact
|
namespace Interact
|
||||||
{
|
{
|
||||||
class ConnectionBrokenException : Exception
|
public class ConnectionBrokenException : Exception
|
||||||
{
|
{
|
||||||
public ConnectionBrokenException(string message) : base(message) { }
|
public ConnectionBrokenException(string message) : base(message) { }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,9 +46,9 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Client.cs" />
|
<Compile Include="Client.cs" />
|
||||||
<Compile Include="Exception.cs" />
|
<Compile Include="Exception.cs" />
|
||||||
<Compile Include="Fields.cs" />
|
<Compile Include="Client-Fields.cs" />
|
||||||
<Compile Include="Handle.cs" />
|
<Compile Include="Client-Handle.cs" />
|
||||||
<Compile Include="JsonObject.cs" />
|
<Compile Include="jsonObject.cs" />
|
||||||
<Compile Include="Message.cs" />
|
<Compile Include="Message.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Properties\Resources.Designer.cs">
|
<Compile Include="Properties\Resources.Designer.cs">
|
||||||
@@ -56,8 +56,8 @@
|
|||||||
<DesignTime>True</DesignTime>
|
<DesignTime>True</DesignTime>
|
||||||
<DependentUpon>Resources.resx</DependentUpon>
|
<DependentUpon>Resources.resx</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Read.cs" />
|
<Compile Include="Client-Read.cs" />
|
||||||
<Compile Include="Send.cs" />
|
<Compile Include="Client-Send.cs" />
|
||||||
<Compile Include="AttrNames.cs" />
|
<Compile Include="AttrNames.cs" />
|
||||||
<Compile Include="User.cs" />
|
<Compile Include="User.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user