public void OnPeerConnected(NetPeer peer) { Console.WriteLine("[Client] connected to: {0}:{1}", peer.EndPoint.Host, peer.EndPoint.Port); NetDataWriter dataWriter = new NetDataWriter(); for (int i = 0; i < 5; i++) { dataWriter.Reset(); dataWriter.Put(0); dataWriter.Put(i); peer.Send(dataWriter, SendOptions.ReliableUnordered); dataWriter.Reset(); dataWriter.Put(1); dataWriter.Put(i); peer.Send(dataWriter, SendOptions.ReliableOrdered); dataWriter.Reset(); dataWriter.Put(2); dataWriter.Put(i); peer.Send(dataWriter, SendOptions.Sequenced); dataWriter.Reset(); dataWriter.Put(3); dataWriter.Put(i); peer.Send(dataWriter, SendOptions.Unreliable); } //And test fragment byte[] testData = new byte[13218]; testData[0] = 192; testData[13217] = 31; peer.Send(testData, SendOptions.ReliableOrdered); }
/// <summary> /// Кладет структуру в Writer(обертка над массивом байт) /// Только для структур(MarshalAs for pointers) /// </summary> public static bool PutInWriter(this LiteNetLib.Utils.NetDataWriter writer, object obj, bool acceptOffset = true, bool reset = false) { try { if (reset) { writer.Reset(); } int size = Marshal.SizeOf(obj.GetType()); writer.ResizeIfNeed(writer.Length + size); obj.CopyTo(writer.Data, writer.Length); if (acceptOffset) { writer.AddOffset(size); } return(true); } catch (Exception e) { Debug.LogError("PutInWriter is bad=" + e); } return(false); }
/// <summary> /// Serialize struct to byte array /// </summary> /// <param name="obj">Struct to serialize</param> /// <returns>byte array with serialized data</returns> public byte[] Serialize <T>(T obj) where T : class, new() { _writer.Reset(); Serialize(_writer, obj); return(_writer.CopyData()); }
public void Send <T>(NetPeer peer, T packet, DeliveryMethod options) where T : class, new() { _netDataWriter.Reset(); Write(_netDataWriter, packet); peer.Send(_netDataWriter, options); }
/// <summary> /// Serialize struct to byte array /// </summary> /// <param name="obj">Struct to serialize</param> /// <returns>byte array with serialized data</returns> public byte[] Serialize <T>(T obj) where T : struct { _writer.Reset(); Serialize(_writer, obj); return(_writer.CopyData()); }
public void NatIntroduce( NetEndPoint hostInternal, NetEndPoint hostExternal, NetEndPoint clientInternal, NetEndPoint clientExternal, string additionalInfo) { NetDataWriter dw = new NetDataWriter(); //First packet (server) //send to client dw.Put(ClientByte); dw.Put(hostInternal); dw.Put(hostExternal); dw.Put(additionalInfo, MaxTokenLength); _netBase.SendRaw(NetPacket.CreateRawPacket(PacketProperty.NatIntroduction, dw), clientExternal); //Second packet (client) //send to server dw.Reset(); dw.Put(HostByte); dw.Put(clientInternal); dw.Put(clientExternal); dw.Put(additionalInfo, MaxTokenLength); _netBase.SendRaw(NetPacket.CreateRawPacket(PacketProperty.NatIntroduction, dw), hostExternal); }
private void HandleNatIntroduction(NetDataReader dr) { // read intro byte hostByte = dr.GetByte(); NetEndPoint remoteInternal = dr.GetNetEndPoint(); NetEndPoint remoteExternal = dr.GetNetEndPoint(); string token = dr.GetString(MaxTokenLength); NetUtils.DebugWrite(ConsoleColor.Cyan, "[NAT] introduction received; we are designated " + (hostByte == HostByte ? "host" : "client")); NetDataWriter writer = new NetDataWriter(); // send internal punch writer.Put(hostByte); writer.Put(token); _netBase.SendRaw(NetPacket.CreateRawPacket(PacketProperty.NatPunchMessage, writer), remoteInternal); NetUtils.DebugWrite(ConsoleColor.Cyan, "[NAT] internal punch sent to " + remoteInternal); // send external punch writer.Reset(); writer.Put(hostByte); writer.Put(token); _netBase.SendRaw(NetPacket.CreateRawPacket(PacketProperty.NatPunchMessage, writer), remoteExternal); NetUtils.DebugWrite(ConsoleColor.Cyan, "[NAT] external punch sent to " + remoteExternal); }
public void Run() { //Server _serverListener = new ServerListener(); NetServer server = new NetServer(_serverListener, 2, "myapp1"); server.DiscoveryEnabled = true; if (!server.Start(9050)) { Console.WriteLine("Server start failed"); Console.ReadKey(); return; } _serverListener.Server = server; //Client _clientListener1 = new ClientListener(); NetClient client1 = new NetClient(_clientListener1, "myapp1"); _clientListener1.Client = client1; client1.SimulateLatency = true; client1.SimulationMaxLatency = 1500; if (!client1.Start()) { Console.WriteLine("Client1 start failed"); return; } _clientListener2 = new ClientListener(); NetClient client2 = new NetClient(_clientListener2, "myapp1"); _clientListener2.Client = client2; client2.SimulateLatency = true; client2.SimulationMaxLatency = 1500; client2.Start(); //Send broadcast NetDataWriter writer = new NetDataWriter(); writer.Put("CLIENT 1 DISCOVERY REQUEST"); client1.SendDiscoveryRequest(writer, 9050); writer.Reset(); writer.Put("CLIENT 2 DISCOVERY REQUEST"); client2.SendDiscoveryRequest(writer, 9050); while (!Console.KeyAvailable) { client1.PollEvents(); client2.PollEvents(); server.PollEvents(); Thread.Sleep(15); } client1.Stop(); client2.Stop(); server.Stop(); Console.ReadKey(); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }