#pragma warning disable IDE1006 protected void iRPCHandler(NeutronPlayer owner, NeutronPlayer sender, short viewId, byte rpcId, byte instanceId, byte[] buffer, RegisterMode registerType, TargetTo targetTo, CacheMode cache, Protocol protocol) #pragma warning restore IDE1006 { void Run((int, int, RegisterMode) key) { bool Send() { MatchmakingTo matchmakingTo = MatchmakingTo.Auto; if (targetTo == TargetTo.Me) { matchmakingTo = MatchmakingTo.Me; } using (NeutronStream stream = Neutron.PooledNetworkStreams.Pull()) { NeutronStream.IWriter writer = stream.Writer; writer.WritePacket((byte)Packet.iRPC); writer.WritePacket((byte)registerType); writer.Write(viewId); writer.Write(rpcId); writer.Write(instanceId); writer.WriteNext(buffer); MatchmakingHelper.Internal.AddCache(rpcId, viewId, writer, owner, cache, CachedPacket.iRPC); owner.Write(sender, writer, targetTo, matchmakingTo, protocol); } return(true); } if (MatchmakingHelper.Server.GetNetworkObject(key, owner, out NeutronView neutronView)) { if (neutronView.iRPCs.TryGetValue((rpcId, instanceId), out RPCInvoker remoteProceduralCall)) { try { iRPCAttribute iRPCAttribute = remoteProceduralCall.iRPC; if (ReflectionHelper.iRPC(buffer, remoteProceduralCall, owner)) { Send(); } } catch (Exception ex) { LogHelper.Stacktrace(ex); } } else { Send(); } }
protected void SynchronizeHandler(NeutronPlayer player, Protocol protocol) { //* Envia todos os jogadores conectados para mim. using (NeutronStream stream = Neutron.PooledNetworkStreams.Pull()) { NeutronStream.IWriter writer = stream.Writer; NeutronPlayer[] players = MatchmakingHelper.Internal.GetPlayersByMatchmakingTo(player, MatchmakingTo.Auto); players = players.Where(x => x.Id != player.Id).ToArray(); writer.WritePacket((byte)Packet.Synchronize); writer.Write((byte)1); writer.WriteNext(players.Serialize().Compress(CompressionMode.Deflate)); player.Write(writer, TargetTo.Me, MatchmakingTo.Me, protocol); } //* Envia-me para todos os jogadores conectados. using (NeutronStream stream = Neutron.PooledNetworkStreams.Pull()) { NeutronStream.IWriter writer = stream.Writer; writer.WritePacket((byte)Packet.Synchronize); writer.Write((byte)2); writer.WriteNext(player.Serialize().Compress(CompressionMode.Deflate)); player.Write(writer, TargetTo.Others, MatchmakingTo.Auto, protocol); } }
//* Aqui os dados são enviados aos seus clientes. public void OnSendingData(NeutronPlayer player, NeutronPacket neutronPacket) { try { using (NeutronStream stream = Neutron.PooledNetworkStreams.Pull()) { var playerId = (short)neutronPacket.Sender.Id; //* Get the player id. byte[] pBuffer = neutronPacket.Buffer.Compress(); //* Compress the packet. switch (neutronPacket.Protocol) { case Protocol.Tcp: { NeutronStream.IWriter wHeader = stream.Writer; wHeader.WriteByteArrayWithAutoSize(pBuffer); //* Write the packet and its size. wHeader.Write(playerId); //* Write the player id in header. byte[] hBuffer = wHeader.ToArray(); //* Get the header buffer. wHeader.Write(); //* End the header. NetworkStream networkStream = player.TcpClient.GetStream(); //* Get the network stream. switch (Helper.GetConstants().SendModel) { case SendType.Synchronous: networkStream.Write(hBuffer, 0, hBuffer.Length); //* Send the header. break; default: if (Helper.GetConstants().SendAsyncPattern == AsynchronousType.APM) { networkStream.Write(hBuffer, 0, hBuffer.Length); //* Send the header. } else { SocketHelper.SendTcpAsync(networkStream, hBuffer, player.TokenSource.Token); //* Send the header. } break; } NeutronStatistics.ServerTCP.AddOutgoing(hBuffer.Length); //* Add the outgoing bytes to the statistics. } break; case Protocol.Udp: { NeutronStream.IWriter wHeader = stream.Writer; wHeader.Write(playerId); //* Write the player id in header. wHeader.WriteNext(pBuffer); //* Write the packet and its size. byte[] hBuffer = wHeader.ToArray(); //* Get the header buffer. wHeader.Write(); //* End the header. player.StateObject.SendDatagram = hBuffer; //* Set the datagram to send. if (player.StateObject.UdpIsReady()) //* Check if the player is ready to send. { NonAllocEndPoint remoteEp = (NonAllocEndPoint)player.StateObject.NonAllocEndPoint; //* Get the remote end point, prevent GC pressure/allocations. switch (Helper.GetConstants().SendModel) { case SendType.Synchronous: SocketHelper.SendBytes(player.UdpClient, hBuffer, remoteEp); //* Send the datagram. break; default: { switch (Helper.GetConstants().SendAsyncPattern) { case AsynchronousType.APM: { SocketHelper.BeginSendBytes(player.UdpClient, hBuffer, remoteEp, (ar) => { SocketHelper.EndSendBytes(player.UdpClient, ar); //* End the send. }); //* Begin the send. break; } default: SocketHelper.SendUdpAsync(player.UdpClient, player.StateObject, remoteEp); //* Send the datagram. break; } break; } } NeutronStatistics.ServerUDP.AddOutgoing(hBuffer.Length); //* Add the outgoing bytes to the statistics. } else { LogHelper.Error($"{player.StateObject.TcpRemoteEndPoint} Cannot receive UDP data. trying... if you are running on WSL2, change the ip from \"localhost\" to the IP address of WSL2 on the client."); } } break; } } } catch (ThreadAbortException) { } catch (SocketException) { } catch (ObjectDisposedException) { } catch (OperationCanceledException) { } catch (Exception) { } }