public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType) { Console.WriteLine("[Server] ReceiveUnconnected {0}. From: {1}. Data: {2}", messageType, remoteEndPoint, reader.GetString(100)); NetDataWriter wrtier = new NetDataWriter(); wrtier.Put("SERVER DISCOVERY RESPONSE :)"); Server.SendDiscoveryResponse(wrtier, remoteEndPoint); }
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); }
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); }
void Start() { _dataWriter = new NetDataWriter(); _netServer = new NetServer(this, 100, "sample_app"); _netServer.Start(5000); _netServer.DiscoveryEnabled = true; _netServer.UpdateTime = 15; }
/// <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); }
public void SetSource(NetDataWriter dataWriter) { _data = dataWriter.Data; _position = 0; _dataSize = dataWriter.Length; }
public static void PutPackedShort(this NetDataWriter writer, short value) { PutPackedInt(writer, value); }
public override void WriteArray(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: Enum[]"); }
public override void Write(T inf, NetDataWriter w) { w.Put((int)Property.GetValue(inf, null)); }
public override void WriteList(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<" + typeof(TProperty) + ">"); }
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf), _maxLength); }
public bool SendDiscoveryResponse(NetDataWriter writer, NetEndPoint remoteEndPoint) { return SendDiscoveryResponse(writer.Data, 0, writer.Length, remoteEndPoint); }
public static void PutPackedUShort(this NetDataWriter writer, ushort value) { PutPackedULong(writer, value); }
/// <summary> /// Send message without connection /// </summary> /// <param name="writer">Data serializer</param> /// <param name="remoteEndPoint">Packet destination</param> /// <returns>Operation result</returns> public bool SendUnconnectedMessage(NetDataWriter writer, NetEndPoint remoteEndPoint) { return SendUnconnectedMessage(writer.Data, 0, writer.Length, remoteEndPoint); }
public bool SendDiscoveryRequest(NetDataWriter writer, int port) { return SendDiscoveryRequest(writer.Data, 0, writer.Length, port); }
public static void PutValue <TType>(this NetDataWriter writer, TType value) { writer.PutValue(typeof(TType), value); }
public static void PutPackedULong(this NetDataWriter writer, ulong value) { if (value <= 240) { writer.Put((byte)value); return; } if (value <= 2287) { writer.Put((byte)((value - 240) / 256 + 241)); writer.Put((byte)((value - 240) % 256)); return; } if (value <= 67823) { writer.Put((byte)249); writer.Put((byte)((value - 2288) / 256)); writer.Put((byte)((value - 2288) % 256)); return; } if (value <= 16777215) { writer.Put((byte)250); writer.Put((byte)(value & 0xFF)); writer.Put((byte)((value >> 8) & 0xFF)); writer.Put((byte)((value >> 16) & 0xFF)); return; } if (value <= 4294967295) { writer.Put((byte)251); writer.Put((byte)(value & 0xFF)); writer.Put((byte)((value >> 8) & 0xFF)); writer.Put((byte)((value >> 16) & 0xFF)); writer.Put((byte)((value >> 24) & 0xFF)); return; } if (value <= 1099511627775) { writer.Put((byte)252); writer.Put((byte)(value & 0xFF)); writer.Put((byte)((value >> 8) & 0xFF)); writer.Put((byte)((value >> 16) & 0xFF)); writer.Put((byte)((value >> 24) & 0xFF)); writer.Put((byte)((value >> 32) & 0xFF)); return; } if (value <= 281474976710655) { writer.Put((byte)253); writer.Put((byte)(value & 0xFF)); writer.Put((byte)((value >> 8) & 0xFF)); writer.Put((byte)((value >> 16) & 0xFF)); writer.Put((byte)((value >> 24) & 0xFF)); writer.Put((byte)((value >> 32) & 0xFF)); writer.Put((byte)((value >> 40) & 0xFF)); return; } if (value <= 72057594037927935) { writer.Put((byte)254); writer.Put((byte)(value & 0xFF)); writer.Put((byte)((value >> 8) & 0xFF)); writer.Put((byte)((value >> 16) & 0xFF)); writer.Put((byte)((value >> 24) & 0xFF)); writer.Put((byte)((value >> 32) & 0xFF)); writer.Put((byte)((value >> 40) & 0xFF)); writer.Put((byte)((value >> 48) & 0xFF)); return; } // all others writer.Put((byte)255); writer.Put((byte)(value & 0xFF)); writer.Put((byte)((value >> 8) & 0xFF)); writer.Put((byte)((value >> 16) & 0xFF)); writer.Put((byte)((value >> 24) & 0xFF)); writer.Put((byte)((value >> 32) & 0xFF)); writer.Put((byte)((value >> 40) & 0xFF)); writer.Put((byte)((value >> 48) & 0xFF)); writer.Put((byte)((value >> 56) & 0xFF)); }
public static void PutPackedUInt(this NetDataWriter writer, uint value) { PutPackedULong(writer, value); }
public void SendNatIntroduceRequest(NetEndPoint masterServerEndPoint, string additionalInfo) { if (!_netBase.IsRunning) return; //prepare outgoing data NetDataWriter dw = new NetDataWriter(); string networkIp = NetUtils.GetLocalIp(true); int networkPort = _netBase.LocalEndPoint.Port; NetEndPoint localEndPoint = new NetEndPoint(networkIp, networkPort); dw.Put(localEndPoint); dw.Put(additionalInfo, MaxTokenLength); //prepare packet _netBase.SendRaw(NetPacket.CreateRawPacket(PacketProperty.NatIntroductionRequest, dw), masterServerEndPoint); }
/// <summary> /// Serialize object to NetDataWriter (fast) /// </summary> /// <param name="writer">Serialization target NetDataWriter</param> /// <param name="obj">Object to serialize</param> /// <exception cref="InvalidTypeException"><typeparamref name="T"/>'s fields are not supported, or it has no fields</exception> public void Serialize <T>(NetDataWriter writer, T obj) where T : class, new() { RegisterInternal <T>().Write(obj, writer); }
public static void PutVector3Int(this NetDataWriter writer, Vector3Int value) { writer.Put(value.x); writer.Put(value.y); writer.Put(value.z); }
protected abstract void ElementWrite(NetDataWriter w, ref TProperty prop);
protected override void ElementWrite(NetDataWriter w, ref char prop) { w.Put(prop); }
public override void Write(TClass inf, NetDataWriter w) { var elem = Getter(inf); ElementWrite(w, ref elem); }
protected override void ElementWrite(NetDataWriter w, ref IPEndPoint prop) { w.Put(prop); }
public override void Write(TClass inf, NetDataWriter w) { _writer(w, Getter(inf)); }
public override void WriteArray(T inf, NetDataWriter w) { w.PutArray(GetterArr(inf), _maxLength); }
public override void Write(TClass inf, NetDataWriter w) { _p = Getter(inf); _p.Serialize(w); }
public override void WriteList(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<Enum>"); }
public abstract void WriteArray(T inf, NetDataWriter w);
public static void PutPackedInt(this NetDataWriter writer, int value) { PutPackedUInt(writer, (uint)((value << 1) ^ (value >> 31))); }
public abstract void WriteList(T inf, NetDataWriter w);
public NetDataReader(NetDataWriter writer) { SetSource(writer); }
public void SendToClients(NetDataWriter writer, SendOptions options, NetPeer excludePeer) { lock (_peers) foreach (NetPeer netPeer in _peers.Values) { if (netPeer != excludePeer) { netPeer.Send(writer, options); } } }
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(); }
public void Init(PacketProperty property, NetDataWriter dataWriter) { int headerSize = GetHeaderSize(property); RawData = new byte[headerSize + dataWriter.Length]; Property = property; Buffer.BlockCopy(dataWriter.Data, 0, RawData, headerSize, dataWriter.Length); }
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 static void PutQuaternion(this NetDataWriter writer, Quaternion value) { writer.Put(value.eulerAngles.x); writer.Put(value.eulerAngles.y); writer.Put(value.eulerAngles.z); }
public override void WriteArray(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: " + typeof(TProperty) + "[]"); }
public static void PutPackedLong(this NetDataWriter writer, long value) { PutPackedInt(writer, (int)(value >> 32)); PutPackedInt(writer, (int)(value & uint.MaxValue)); }
public static byte[] CreateRawPacket(PacketProperty property, NetDataWriter dataWriter) { int headerSize = GetHeaderSize(property); byte[] rawData = new byte[headerSize + dataWriter.Length]; rawData[0] = (byte)property; Buffer.BlockCopy(dataWriter.Data, 0, rawData, headerSize, dataWriter.Length); return rawData; }
public override void WriteArray(T inf, NetDataWriter w) { w.PutSBytesWithLength(GetterArr(inf)); }
public override void Write(T inf, NetDataWriter w) { w.Put(Getter(inf)); }
public void Send(NetDataWriter dataWriter, SendOptions options) { Send(dataWriter.Data, 0, dataWriter.Length, options); }