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