80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
using Fleck;
|
|
using System;
|
|
using battle;
|
|
using pb = global::Google.Protobuf;
|
|
|
|
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)
|
|
{
|
|
Pb.BattleRpcMessage msg = Deserialize<Pb.BattleRpcMessage>(message);
|
|
Task.Run(() =>
|
|
{
|
|
handle(msg);
|
|
});
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理远程请求
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
private void handle(Pb.BattleRpcMessage msg)
|
|
{
|
|
Console.WriteLine("收到战斗消息:{0}", msg.ToString());
|
|
int end = battle.Manager.CheckBattle((int)msg.Rid);
|
|
Console.WriteLine("CheckBattle:{0}", end);
|
|
Pb.BattleTestMessage message = new Pb.BattleTestMessage();
|
|
message.Id = "123123";
|
|
message.Msg = "succ";
|
|
msg.Data = pb.WellKnownTypes.Any.Pack(message);
|
|
socket.Send(Serialization(msg));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 反序列化protobuf
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="dataBytes"></param>
|
|
/// <returns></returns>
|
|
public static T Deserialize<T>(byte[] dataBytes) where T : pb.IMessage, new()
|
|
{
|
|
T msg = new T();
|
|
msg = (T)msg.Descriptor.Parser.ParseFrom(dataBytes);
|
|
return msg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 反序列化protobuf
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="dataBytes"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Serialization<T>(T msg) where T : pb.IMessage, new()
|
|
{
|
|
byte[] result = new byte[msg.CalculateSize()];
|
|
pb.CodedOutputStream output = new pb.CodedOutputStream(result);
|
|
msg.WriteTo(output);
|
|
output.CheckNoSpaceLeft();
|
|
return result;
|
|
}
|
|
}
|
|
} |