Beispiel #1
0
        Task <NetDataBufferReader> IRequestWaiter.SendRequestAndReceive(ISession session, NetDataWriter writer, TimeSpan?timeout, DeliveryMethod deliveryMethod, int extra)
        {
            var task = session.SessionRequest.RequestAsync(writer.Data, 0, writer.Length, deliveryMethod, timeout);

            try
            {
                return(task);
            }
            catch (Exception e)
            {
                throw e.InnerException ?? e;
            }
        }
 internal virtual void SerializeValue(NetDataWriter writer)
 {
     writer.PutValue(GetFieldType(), GetValue());
 }
 internal override void SerializeValue(NetDataWriter writer)
 {
     writer.PutArray(Value);
 }
Beispiel #4
0
 /// <summary>
 /// Send data to peer
 /// </summary>
 /// <param name="dataWriter">DataWriter with data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 public void Send(NetDataWriter dataWriter, SendOptions options)
 {
     Send(dataWriter.Data, 0, dataWriter.Length, options);
 }
Beispiel #5
0
 public void SendMessage(byte dataChannel, DeliveryMethod deliveryMethod, NetDataWriter writer)
 {
     SendMessage(-1, dataChannel, deliveryMethod, writer);
 }
Beispiel #6
0
 public bool SendDiscoveryRequest(NetDataWriter writer, int port)
 {
     return(SendDiscoveryRequest(writer.Data, 0, writer.Length, port));
 }
Beispiel #7
0
 public override void SerializeClientReadyData(NetDataWriter writer)
 {
     writer.Put(GameInstance.UserId);
     writer.Put(GameInstance.UserToken);
     writer.Put(GameInstance.SelectedCharacterId);
 }
        public bool ServerSend(long connectionId, byte dataChannel, DeliveryMethod deliveryMethod, NetDataWriter writer)
        {
            TransportEventData data = new TransportEventData();

            data.type   = ENetworkEvent.DataEvent;
            data.reader = new NetDataReader(writer.CopyData());
            serverData.Enqueue(data);
            return(true);
        }
Beispiel #9
0
        public void Run()
        {
            Console.WriteLine("=== Broadcast Test ===");
            //Server
            _serverListener = new ServerListener();

            NetManager server = new NetManager(_serverListener)
            {
                BroadcastReceiveEnabled = true,
                IPv6Mode = IPv6Mode.DualMode
            };

            if (!server.Start(9050))
            {
                Console.WriteLine("Server start failed");
                Console.ReadKey();
                return;
            }
            _serverListener.Server = server;

            //Client
            _clientListener1 = new ClientListener();

            NetManager client1 = new NetManager(_clientListener1)
            {
                UnconnectedMessagesEnabled = true,
                SimulateLatency            = true,
                SimulationMaxLatency       = 1500,
                IPv6Mode = IPv6Mode.DualMode
            };

            _clientListener1.Client = client1;
            if (!client1.Start())
            {
                Console.WriteLine("Client1 start failed");

                return;
            }

            _clientListener2 = new ClientListener();
            NetManager client2 = new NetManager(_clientListener2)
            {
                UnconnectedMessagesEnabled = true,
                SimulateLatency            = true,
                SimulationMaxLatency       = 1500,
                IPv6Mode = IPv6Mode.DualMode
            };

            _clientListener2.Client = client2;
            client2.Start();

            //Send broadcast
            NetDataWriter writer = new NetDataWriter();

            writer.Put("CLIENT 1 DISCOVERY REQUEST");
            client1.SendBroadcast(writer, 9050);
            writer.Reset();

            writer.Put("CLIENT 2 DISCOVERY REQUEST");
            client2.SendBroadcast(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();
        }
Beispiel #10
0
 public static void SerializeCharacterData_UMA(IPlayerCharacterData characterData, NetDataWriter writer)
 {
     characterData.UmaAvatarData.Serialize(writer);
 }
 public override void Serialize(NetDataWriter writer)
 {
     writer.Put(Value.x);
     writer.Put(Value.y);
 }
Beispiel #12
0
 public void Serialize(NetDataWriter writer)
 {
     writer.Put(id);
 }
Beispiel #13
0
 public bool SendBroadcast(NetDataWriter writer, int port)
 {
     return(SendBroadcast(writer.Data, 0, writer.Length, port));
 }
 public override void WritePayload(NetDataWriter message)
 {
     base.WritePayload(message);
     message.Put(Fullscreen);
 }
Beispiel #15
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="writer">DataWriter with data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 /// <param name="excludePeer">Excluded peer</param>
 public void SendToAll(NetDataWriter writer, DeliveryMethod options, NetPeer excludePeer)
 {
     SendToAll(writer.Data, 0, writer.Length, options, excludePeer);
 }
Beispiel #16
0
 public void Serialize(NetDataWriter writer, INetSerializable packet)
 {
     writer.Put(packet.GetType().ToString());
     packet.Serialize(writer);
 }
Beispiel #17
0
 /// <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, IPEndPoint remoteEndPoint)
 {
     return(SendUnconnectedMessage(writer.Data, 0, writer.Length, remoteEndPoint));
 }
 /// <summary>
 /// Override this function to write custom data to send from server to client
 /// </summary>
 public virtual void OnSerialize(NetDataWriter writer)
 {
 }
Beispiel #19
0
 public bool SendDiscoveryResponse(NetDataWriter writer, IPEndPoint remoteEndPoint)
 {
     return(SendDiscoveryResponse(writer.Data, 0, writer.Length, remoteEndPoint));
 }
Beispiel #20
0
 public void Serialize(NetDataWriter writer)
 {
 }
Beispiel #21
0
        static void Main(string[] args)
        {
            EventBasedNetListener      netListener      = new EventBasedNetListener();
            EventBasedNatPunchListener netPunchListener = new EventBasedNatPunchListener();

            NetManager client = new NetManager(netListener);
            NetPeer    peer   = null;

            netPunchListener.NatIntroductionRequest += NetPunchListener_NatIntroductionRequest1;

            netPunchListener.NatIntroductionSuccess += (point, token) =>
            {
                peer = client.Connect(point, ConnectionKey);//peer必须马上用 否则就没了?
                Console.WriteLine("Success . Connecting to : {0}, connection created: {1}", point, peer != null);
                //peer.Send(Encoding.UTF8.GetBytes("hello1"), DeliveryMethod.ReliableOrdered);
            };


            netListener.NetworkReceiveEvent += NetListener_NetworkReceiveEvent;
            netListener.PeerConnectedEvent  += NetListener_PeerConnectedEvent;

            //netListener.PeerConnectedEvent += peer =>
            //{
            //    Console.WriteLine("PeerConnected: " + peer.EndPoint.ToString());
            //};

            //netListener.NetworkReceiveEvent += NetListener_NetworkReceiveEvent;
            netListener.ConnectionRequestEvent += request =>
            {
                request.AcceptIfKey(ConnectionKey);//关键方法
                Console.WriteLine("acceptifkey: connectionkey");
            };

            netListener.PeerDisconnectedEvent += (peer, disconnectInfo) =>
            {
                Console.WriteLine("PeerDisconnected: " + disconnectInfo.Reason);
                if (disconnectInfo.AdditionalData.AvailableBytes > 0)
                {
                    Console.WriteLine("Disconnect data: " + disconnectInfo.AdditionalData.GetInt());
                }
            };

            client.NatPunchEnabled = true;
            client.NatPunchModule.Init(netPunchListener);
            //client.LocalPort;
            client.Start();
            client.NatPunchModule.SendNatIntroduceRequest(NetUtils.MakeEndPoint("2017studio.imwork.net", ServerPort), "token2");
            while (true)
            {
                if (Console.KeyAvailable)
                {
                    var key = Console.ReadKey(true).Key;
                    if (key == ConsoleKey.Escape)
                    {
                        break;
                    }
                    if (key == ConsoleKey.A)
                    {
                        //NetDataWriter ndw = new NetDataWriter();

                        //peer.Send()
                        //peer.Send();
                        NetDataWriter ndw = new NetDataWriter();
                        ndw.Put("hello,im" + netListener.GetHashCode());
                        ndw.Put(new byte[67000]);
                        peer.Send(ndw, DeliveryMethod.ReliableOrdered);
                        //client.SendToAll(Encoding.ASCII.GetBytes("hello,im" + netListener.GetHashCode()), DeliveryMethod.ReliableOrdered);
                    }
                }
                client.NatPunchModule.PollEvents();
                client.PollEvents();
                Thread.Sleep(10);
            }
            Console.WriteLine("Hello World!");
        }
Beispiel #22
0
        /// <summary>
        /// Connect to remote host
        /// </summary>
        /// <param name="address">Server IP or hostname</param>
        /// <param name="port">Server Port</param>
        /// <param name="connectionData">Additional data for remote peer</param>
        /// <returns>Null if connections limit reached, New NetPeer if new connection, Old NetPeer if already connected</returns>
        /// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
        public NetPeer Connect(string address, int port, NetDataWriter connectionData)
        {
            var ep = NetUtils.MakeEndPoint(address, port);

            return(Connect(ep, connectionData));
        }
Beispiel #23
0
 public override void SendMessage(long connectionId, byte dataChannel, DeliveryMethod deliveryMethod, NetDataWriter writer)
 {
     Transport.ClientSend(dataChannel, deliveryMethod, writer);
 }
Beispiel #24
0
 /// <summary>
 /// Connect to remote host
 /// </summary>
 /// <param name="target">Server end point (ip and port)</param>
 /// <param name="key">Connection key</param>
 /// <returns>Null if connections limit reached, New NetPeer if new connection, Old NetPeer if already connected</returns>
 /// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
 public NetPeer Connect(IPEndPoint target, string key)
 {
     return(Connect(target, NetDataWriter.FromString(key)));
 }
 internal void Serialize(NetDataWriter writer)
 {
     SerializeValue(writer);
 }
Beispiel #26
0
 /// <summary>
 /// Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 /// <param name="writer">additional data</param>
 public void DisconnectPeer(NetPeer peer, NetDataWriter writer)
 {
     DisconnectPeer(peer, writer.Data, 0, writer.Length);
 }
 private void SerializeForSend(NetDataWriter writer)
 {
     LiteNetLibElementInfo.SerializeInfo(GetInfo(), writer);
     Serialize(writer);
 }
Beispiel #28
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="writer">DataWriter with data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 public void SendToAll(NetDataWriter writer, DeliveryMethod options)
 {
     SendToAll(writer.Data, 0, writer.Length, options);
 }
Beispiel #29
0
 public void ExecuteServerSide(NetPeer thisPeer, NetDataWriter writer)
 {
     ServerObjects.Instance.MovePlayer(this, thisPeer, writer);
 }
Beispiel #30
0
 void IRequestWaiter.SendRequest(ISession session, NetDataWriter writer, DeliveryMethod deliveryMethod, DeliveryTarget deliveryTarget, int extra)
 {
     session.SessionRequest.Notification(writer.Data, 0, writer.Length, deliveryMethod);
 }