80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
using Fleck;
|
|
|
|
namespace BattleServer
|
|
{
|
|
/// <summary>
|
|
/// 客户端链接对象
|
|
/// </summary>
|
|
class Client
|
|
{
|
|
private IWebSocketConnection socket;
|
|
|
|
public Client(IWebSocketConnection _socket)
|
|
{
|
|
socket = _socket;
|
|
socket.OnBinary = doBinary;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收rpc数据
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
private void doBinary(byte[] message)
|
|
{
|
|
BattleRpcMessage msg = ProtoDeSerialize<BattleRpcMessage>(message);
|
|
// Pb.BattleRpcMessage msg = Deserialize<Pb.BattleRpcMessage>(message);
|
|
Task.Run(() =>{
|
|
handle(msg);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理远程请求
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
private void handle(BattleRpcMessage msg)
|
|
{
|
|
Console.WriteLine("收到战斗消息:{0}", msg.ToString());
|
|
int end = battle.Manager.CheckBattle((int)msg.rid);
|
|
Console.WriteLine("CheckBattle:{0}", end);
|
|
switch(msg.method){
|
|
case "Check":
|
|
BattleReport report = ProtoDeSerialize<BattleReport>(msg.Data.value);
|
|
HotUpdateScripts.FightRunnerMgr.Instance.StartOnceFight(report);
|
|
socket.Send(ProtoSerialize(msg));
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 序列化并返回二进制
|
|
/// Serialize an Object and return byte[]
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static byte[] ProtoSerialize<T>(T obj) where T : class
|
|
{
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
ProtoBuf.Serializer.Serialize(stream, obj);
|
|
return stream.ToArray();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过二进制反序列化
|
|
/// Deserialize with byte[]
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T ProtoDeSerialize<T>(byte[] msg) where T : class
|
|
{
|
|
using (var ms = new MemoryStream(msg))
|
|
{
|
|
var data = ProtoBuf.Serializer.Deserialize<T>(ms);
|
|
return data;
|
|
}
|
|
}
|
|
}
|
|
} |