Put() public method

public Put ( NetEndPoint endPoint ) : void
endPoint NetEndPoint
return void
Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
            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);
            }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
0
 public override void Write(T inf, NetDataWriter w)
 {
     w.Put((int)Property.GetValue(inf, null));
 }
Exemplo n.º 5
0
 public override void Write(T inf, NetDataWriter w)
 {
     w.Put(Getter(inf), _maxLength);
 }
Exemplo n.º 6
0
 protected override void ElementWrite(NetDataWriter w, ref IPEndPoint prop)
 {
     w.Put(prop);
 }
Exemplo n.º 7
0
 protected override void ElementWrite(NetDataWriter w, ref char prop)
 {
     w.Put(prop);
 }
 public static void PutVector2(this NetDataWriter writer, Vector2 value)
 {
     writer.Put(value.x);
     writer.Put(value.y);
 }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        public static void PutValue(this NetDataWriter writer, Type type, object value)
        {
            #region Generic Values
            if (type == typeof(bool))
            {
                writer.Put((bool)value);
                return;
            }

            if (type == typeof(bool[]))
            {
                writer.PutArray((bool[])value);
                return;
            }

            if (type == typeof(byte))
            {
                writer.Put((byte)value);
                return;
            }

            if (type == typeof(byte[]))
            {
                writer.PutBytesWithLength((byte[])value);
                return;
            }

            if (type == typeof(char))
            {
                writer.Put((char)value);
                return;
            }

            if (type == typeof(double))
            {
                writer.Put((double)value);
                return;
            }

            if (type == typeof(double[]))
            {
                writer.PutArray((double[])value);
                return;
            }

            if (type == typeof(float))
            {
                writer.Put((float)value);
                return;
            }

            if (type == typeof(float[]))
            {
                writer.PutArray((float[])value);
                return;
            }

            if (type == typeof(int))
            {
                writer.Put((int)value);
                return;
            }

            if (type == typeof(int[]))
            {
                writer.PutArray((int[])value);
                return;
            }

            if (type == typeof(long))
            {
                writer.Put((long)value);
                return;
            }

            if (type == typeof(long[]))
            {
                writer.PutArray((long[])value);
                return;
            }

            if (type == typeof(sbyte))
            {
                writer.Put((sbyte)value);
                return;
            }

            if (type == typeof(short))
            {
                writer.Put((short)value);
                return;
            }

            if (type == typeof(short[]))
            {
                writer.PutArray((short[])value);
                return;
            }

            if (type == typeof(string))
            {
                writer.Put((string)value);
                return;
            }

            if (type == typeof(uint))
            {
                writer.Put((uint)value);
                return;
            }

            if (type == typeof(uint[]))
            {
                writer.PutArray((uint[])value);
                return;
            }

            if (type == typeof(ulong))
            {
                writer.Put((ulong)value);
                return;
            }

            if (type == typeof(ulong[]))
            {
                writer.PutArray((ulong[])value);
                return;
            }

            if (type == typeof(ushort))
            {
                writer.Put((ushort)value);
                return;
            }

            if (type == typeof(ushort[]))
            {
                writer.PutArray((ushort[])value);
                return;
            }
            #endregion

            #region Unity Values
            if (type == typeof(Color))
            {
                writer.Put((Color)value);
                return;
            }

            if (type == typeof(Quaternion))
            {
                writer.Put((Quaternion)value);
                return;
            }

            if (type == typeof(Vector2))
            {
                writer.Put((Vector2)value);
                return;
            }

            if (type == typeof(Vector2Int))
            {
                writer.Put((Vector2Int)value);
                return;
            }

            if (type == typeof(Vector3))
            {
                writer.Put((Vector3)value);
                return;
            }

            if (type == typeof(Vector3Int))
            {
                writer.Put((Vector3Int)value);
                return;
            }

            if (type == typeof(Vector4))
            {
                writer.Put((Vector4)value);
                return;
            }
            #endregion

            if (typeof(INetSerializable).IsAssignableFrom(type))
            {
                (value as INetSerializable).Serialize(writer);
                return;
            }

            throw new ArgumentException("NetDataReader cannot write type " + value.GetType().Name);
        }
Exemplo n.º 11
0
 protected virtual void WriteHash <T>(NetDataWriter writer)
 {
     writer.Put(GetHash <T>());
 }
Exemplo n.º 12
0
 public static void Put(this NetDataWriter writer, Vector3 value)
 {
     writer.Put(value.X);
     writer.Put(value.Y);
     writer.Put(value.Z);
 }
Exemplo n.º 13
0
 protected virtual void WriteHash(Type type, NetDataWriter writer)
 {
     writer.Put(GetHash(type));
 }
Exemplo n.º 14
0
        public static void PutValue <TType>(this NetDataWriter writer, TType value)
        {
            #region Generic Values
            if (value is bool)
            {
                writer.Put((bool)(object)value);
                return;
            }

            if (value is bool[])
            {
                writer.PutArray((bool[])(object)value);
                return;
            }

            if (value is byte)
            {
                writer.Put((byte)(object)value);
                return;
            }

            if (value is char)
            {
                writer.Put((char)(object)value);
                return;
            }

            if (value is double)
            {
                writer.Put((double)(object)value);
                return;
            }

            if (value is double[])
            {
                writer.PutArray((double[])(object)value);
                return;
            }

            if (value is float)
            {
                writer.Put((float)(object)value);
                return;
            }

            if (value is float[])
            {
                writer.PutArray((float[])(object)value);
                return;
            }

            if (value is int)
            {
                writer.Put((int)(object)value);
                return;
            }

            if (value is int[])
            {
                writer.PutArray((int[])(object)value);
                return;
            }

            if (value is long)
            {
                writer.Put((long)(object)value);
                return;
            }

            if (value is long[])
            {
                writer.PutArray((long[])(object)value);
                return;
            }

            if (value is sbyte)
            {
                writer.Put((sbyte)(object)value);
                return;
            }

            if (value is short)
            {
                writer.Put((short)(object)value);
                return;
            }

            if (value is short[])
            {
                writer.PutArray((short[])(object)value);
                return;
            }

            if (typeof(TType) == typeof(string))
            {
                writer.Put((string)(object)value);
                return;
            }

            if (value is uint)
            {
                writer.Put((uint)(object)value);
                return;
            }

            if (value is uint[])
            {
                writer.PutArray((uint[])(object)value);
                return;
            }

            if (value is ulong)
            {
                writer.Put((ulong)(object)value);
                return;
            }

            if (value is ulong[])
            {
                writer.PutArray((ulong[])(object)value);
                return;
            }

            if (value is ushort)
            {
                writer.Put((ushort)(object)value);
                return;
            }

            if (value is ushort[])
            {
                writer.PutArray((ushort[])(object)value);
                return;
            }
            #endregion

            #region Unity Values
            if (value is Color)
            {
                writer.Put((Color)(object)value);
                return;
            }

            if (value is Quaternion)
            {
                writer.Put((Quaternion)(object)value);
                return;
            }

            if (value is Vector2)
            {
                writer.Put((Vector2)(object)value);
                return;
            }

            if (value is Vector2Int)
            {
                writer.Put((Vector2Int)(object)value);
                return;
            }

            if (value is Vector3)
            {
                writer.Put((Vector3)(object)value);
                return;
            }

            if (value is Vector3Int)
            {
                writer.Put((Vector3Int)(object)value);
                return;
            }

            if (value is Vector4)
            {
                writer.Put((Vector4)(object)value);
                return;
            }
            #endregion

            if (value is INetSerializable)
            {
                ((INetSerializable)value).Serialize(writer);
                return;
            }

            throw new ArgumentException("NetDataReader cannot write type " + value.GetType().Name);
        }
Exemplo n.º 15
0
        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 override void WriteHash(ulong hash, NetDataWriter writer)
 {
     writer.Put(hash);
 }
Exemplo n.º 17
0
 public override void Write(T inf, NetDataWriter w)
 {
     w.Put(Getter(inf));
 }
Exemplo n.º 18
0
 public static void Put(this NetDataWriter writer, Quaternion value)
 {
     writer.Put(value.eulerAngles.x);
     writer.Put(value.eulerAngles.y);
     writer.Put(value.eulerAngles.z);
 }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 20
0
 public static void Put(this NetDataWriter writer, Vector3Int value)
 {
     writer.Put(value.x);
     writer.Put(value.y);
     writer.Put(value.z);
 }
Exemplo n.º 21
0
 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 PutValue(this NetDataWriter writer, Type type, object value)
        {
            #region Generic Values
            if (type.IsEnum)
            {
                type = type.GetEnumUnderlyingType();
            }

            if (type == typeof(bool))
            {
                writer.Put((bool)value);
                return;
            }

            if (type == typeof(byte))
            {
                writer.Put((byte)value);
                return;
            }

            if (type == typeof(char))
            {
                writer.Put((char)value);
                return;
            }

            if (type == typeof(double))
            {
                writer.Put((double)value);
                return;
            }

            if (type == typeof(float))
            {
                writer.Put((float)value);
                return;
            }

            if (type == typeof(int))
            {
                writer.PutPackedInt((int)value);
                return;
            }

            if (type == typeof(long))
            {
                writer.PutPackedLong((long)value);
                return;
            }

            if (type == typeof(sbyte))
            {
                writer.Put((sbyte)value);
                return;
            }

            if (type == typeof(short))
            {
                writer.PutPackedShort((short)value);
                return;
            }

            if (type == typeof(string))
            {
                writer.Put((string)value);
                return;
            }

            if (type == typeof(uint))
            {
                writer.PutPackedUInt((uint)value);
                return;
            }

            if (type == typeof(ulong))
            {
                writer.PutPackedULong((ulong)value);
                return;
            }

            if (type == typeof(ushort))
            {
                writer.PutPackedUShort((ushort)value);
                return;
            }
            #endregion

            #region Unity Values
            if (type == typeof(Color))
            {
                writer.PutColor((Color)value);
                return;
            }

            if (type == typeof(Quaternion))
            {
                writer.PutQuaternion((Quaternion)value);
                return;
            }

            if (type == typeof(Vector2))
            {
                writer.PutVector2((Vector2)value);
                return;
            }

            if (type == typeof(Vector2Int))
            {
                writer.PutVector2Int((Vector2Int)value);
                return;
            }

            if (type == typeof(Vector3))
            {
                writer.PutVector3((Vector3)value);
                return;
            }

            if (type == typeof(Vector3Int))
            {
                writer.PutVector3Int((Vector3Int)value);
                return;
            }

            if (type == typeof(Vector4))
            {
                writer.PutVector4((Vector4)value);
                return;
            }
            #endregion

            if (typeof(INetSerializable).IsAssignableFrom(type))
            {
                (value as INetSerializable).Serialize(writer);
                return;
            }

            throw new ArgumentException("NetDataWriter cannot write type " + value.GetType().Name);
        }