Exemple #1
0
 static NUClient()
 {
     client                 = null;
     m_onConnected          = null;
     m_onConnectionTimeout  = null;
     m_onPacketReceived     = null;
     dataQueue              = null;
     seqDataList            = null;
     broadcastDataQueue     = null;
     hasDisconnected        = false;
     hasConnected           = false;
     dataQueueLock          = new object();
     seqDataQueueLock       = new object();
     broadcastDataQueueLock = new object();
     multiPartBuffers       = null;
     multiPartLock          = new object();
     clientComponent        = null;
     broadcaster            = null;
     broadcastServerPort    = 0;
     lastPacketId           = -1;
 }
Exemple #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;
            }
        }