public Action <uint, byte[]> RegisterSync(Action <uint, byte[]> syncHandler) { this.syncHandler = syncHandler; return(delegate(uint socketId, byte[] data) { byte[] packet = PacketBuilder.New().Add((UInt32)PacketSyncPacket).Add((UInt32)socketId).Add((UInt32)data.Length).Add(data).Build(); socketHandler.Send(socketId, packet); }); }
public void Handle(INetworkSocketHandler handler, uint socketId, uint packetId, BinaryReader reader) { uint MessageId = reader.ReadUInt32(); if (RPCMethods.ContainsKey(packetId)) { uint length = reader.ReadUInt32(); byte[] data = new byte[length]; reader.Read(data, 0, (int)length); var rpcdata = RPCMethods[packetId]; byte[] outputdata = rpcdata.Handler.Invoke(socketId, MessageId, data); handler.Send(socketId, PacketBuilder.New().Add(rpcdata.ResponsePacketId).Add(MessageId).Add <UInt32>((uint)outputdata.Length).Add(outputdata).Build()); } else { if (MessageCallbacks.ContainsKey(MessageId)) { uint length = reader.ReadUInt32(); byte[] data = new byte[length]; reader.Read(data, 0, (int)length); MessageCallbacks[MessageId].Invoke(data); MessageCallbacks.Remove(MessageId); } } }
public Func <uint, I, Action <O>, uint> RegisterRPC <I, O>(uint packetId) { packetIds.Add(packetId); return(delegate(uint socketId, I input, Action <O> callback) { uint MessageId = MessageIdCounter.Get(); MemoryStream memory = new MemoryStream(); binaryFmt.Serialize(memory, input); byte[] inputdata = memory.ToArray(); MessageCallbacks.Add(MessageId, delegate(byte[] data) { O output = (O)binaryFmt.Deserialize(new MemoryStream(data)); callback.Invoke(output); } ); socketHandler.Send(socketId, PacketBuilder.New().Add <UInt32>(packetId).Add(MessageId).Add <UInt32>((uint)inputdata.Length).Add(inputdata).Build()); return MessageId; }); }
public void Handle(INetworkSocketHandler handler, uint socketId, uint packetId, BinaryReader reader) { if (packetId == PacketIdHandleAction) { //Todo, maybe just maybe this should not crash master... if (AsMaster) { throw new Exception("Master should not receive this packet!"); } HandleActionId(reader.ReadUInt32()); } else if (packetId == PacketIdStep) { if (AsMaster) { throw new Exception("Server should not receive this packet!"); } uint stepId = reader.ReadUInt32(); onStepUpdate(stepId); } else if (packetId == PacketSyncPacket) { uint ClientId = reader.ReadUInt32(); uint length = reader.ReadUInt32(); byte[] data = new byte[length]; reader.Read(data, 0, (int)length); syncHandler(ClientId, data); } else { if (AsMaster) { if (Actions.ContainsKey(packetId)) { uint eventId = reader.ReadUInt32(); handler.Send(socketId, PacketBuilder.New().Add((UInt32)PacketIdHandleAction).Add((UInt32)eventId).Build()); uint length = reader.ReadUInt32(); byte[] data = new byte[length]; reader.Read(data, 0, (int)length); if (Actions[packetId].Invoke(socketId, data)) { ((INetworkSocketHandlerServer)socketHandler).BroadCast(PacketBuilder.New().Add((UInt32)packetId).Add((UInt32)socketId).Add((UInt32)data.Length).Add(data).Build()); } } else { throw new Exception("Unknown Action Packet Id!"); } } else { if (Actions.ContainsKey(packetId)) { uint sender = reader.ReadUInt32(); uint length = reader.ReadUInt32(); byte[] data = new byte[length]; reader.Read(data, 0, (int)length); Actions[packetId].Invoke(sender, data); } else { throw new Exception("Unknown Action Packet Id!"); } } } }