89 lines
2.9 KiB
C#
89 lines
2.9 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)
|
|
{
|
|
try{
|
|
Console.WriteLine("收到战斗消息:{0}", msg.method);
|
|
switch(msg.method){
|
|
case "Check":
|
|
BattleReport report = ProtoDeSerialize<BattleReport>(msg.data.value);
|
|
bool ischeck = HotUpdateScripts.FightRunnerMgr.Instance.VerifyOnceFight(report);
|
|
Console.WriteLine("战斗校验结果:{0}", ischeck);
|
|
msg.data = new Google.Protobuf.WellKnownTypes.Any {
|
|
type_url = "type.googleapis.com/BattleCheckResults",
|
|
value = ProtoSerialize(new BattleCheckResults{ischeck=ischeck}),
|
|
};
|
|
socket.Send(ProtoSerialize(msg));
|
|
break;
|
|
}
|
|
} catch (IOException ex){
|
|
Console.WriteLine("战斗异常 {0}",ex.Message);
|
|
socket.Close();
|
|
}
|
|
}
|
|
|
|
/// <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
|
|
{
|
|
msg = msg == null ? new byte[]{} : msg;
|
|
using (var ms = new MemoryStream(msg))
|
|
{
|
|
var data = ProtoBuf.Serializer.Deserialize<T>(ms);
|
|
return data;
|
|
}
|
|
}
|
|
}
|
|
} |