Example #1
0
        /// <summary>
        /// Send a Packet through a reliable but latency prone way with sequence guarantee.
        /// <para>This call might have more latency then <see cref="SendReliable(Packet, Action)"/>.</para>
        /// </summary>
        /// <param name="packet"></param>
        public static void SendReliableSequenced(Packet packet, Action callback = null)
        {
            if (packet.id == -1)
            {
                packet.OverrideID(NUUtilities.GeneratePacketId());
            }

            SendReliable(packet, callback);
        }
Example #2
0
        private static void EndConnect(IAsyncResult asyncResult)
        {
            //Extract TcpClient from AsyncState
            TcpClient tcpClient = (TcpClient)asyncResult.AsyncState;

            //Try to connect to the given EndPoint
            try
            {
                //Finish connecting
                tcpClient.EndConnect(asyncResult);

                //Get Client EndPoint
                IPEndPoint localEndPoint  = (IPEndPoint)tcpClient.Client.LocalEndPoint;
                IPEndPoint remoteEndPoint = (IPEndPoint)tcpClient.Client.RemoteEndPoint;

                //Create UdpClient
                Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
                {
                    ExclusiveAddressUse = true
                };
                udpClient.Bind(localEndPoint);                 //Bind it locally to the TCP Address

                //Create NUClientInfo if it doesn't exists already
                if (client == null)
                {
                    client = new NUClientInfo(Guid.Empty, localEndPoint.Address, ref tcpClient, ref udpClient);
                }
                else
                {
                    //Override information with last client id but current addresses and sockets
                    client = new NUClientInfo(client.guid, localEndPoint.Address, ref tcpClient, ref udpClient);
                }

                //Debug.Log("Connected to server (" + remoteEndPoint.Address + ":" + remoteEndPoint.Port + ")!" +
                //    "\nSending GUID " + client.id + " and Waiting for response...");

                //Send Current GUID - Null if this is not a reconnect
                Packet guidPacket = new Packet(client.guid.ToByteArray(), null, Packet.TypeFlag.GUID, NUUtilities.GeneratePacketId());
                TcpTransmissionState transmissionState = new TcpTransmissionState(guidPacket.data.Length, ref tcpClient, client, null);
                tcpClient.GetStream().BeginWrite(guidPacket.data, 0, guidPacket.data.Length,
                                                 new AsyncCallback(EndReliableSend), transmissionState);

                //Start receiving messages from server
                TcpTransmissionState tcpTransmissionState = new TcpTransmissionState(
                    NUUtilities.MTU, ref tcpClient, client, null);
                tcpClient.GetStream().BeginRead(tcpTransmissionState.data, 0, NUUtilities.MTU,
                                                new AsyncCallback(EndReliableReceive), tcpTransmissionState);
                UdpTransmissionState udpTransmissionState = new UdpTransmissionState(
                    NUUtilities.MTU, ref udpClient, client, null);
                udpClient.BeginReceive(udpTransmissionState.data, 0, NUUtilities.MTU, SocketFlags.None,
                                       new AsyncCallback(EndUnreliableReceive), udpTransmissionState);

                Debug.Log("Connected to Server!");
            }
            catch (Exception ex)
            {
                //Setup disconnection flag
                hasDisconnected = true;

                Debug.LogError("Could not connect to Server! " + ex.ToString());

                return;
            }
        }