Exemple #1
0
    IEnumerator IniciaConexion()
    {
        IPAddress[] addresses = Dns.GetHostAddresses(m_URL);
        try
        {
            if (addresses.Length > 0)
            {
                m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                m_socket.ReceiveBufferSize = 2048;
                m_socket.NoDelay           = true;

                StartCoroutine(conectarSocket(new IPEndPoint(addresses[0], m_Port)));
            }
            else
            {
                if (OnError != null)
                {
                    OnError("Error al resolver la URL.");
                }
            }
        }
        catch (Exception e)
        {
            Debug.Log("Error al inicia la red. " + e);
            if (OnError != null)
            {
                OnError("Error al inicia la red. " + e);
            }
            ErrorHub.ThrowError(7, (_name) => { Application.LoadLevel("menu"); });
        }
        yield return(true);
    }
Exemple #2
0
 // Update is called once per frame
 void Update()
 {
     if (m_socket == null)
     {
         return;
     }
     try
     {
         if (SocketConnected(m_socket))
         {
             SocketError errorCode;
             int         cnt = 0;
             while (m_socket != null && cnt < 10)
             { // datos suficientes para un mensaje.
                 if (m_socket.Available >= 2)
                 {
                     m_socket.Receive(m_eBuffer, 0, 2, SocketFlags.Peek, out errorCode);
                     if (errorCode == SocketError.Success)
                     {
                         deserialize();
                     }
                     else
                     if (errorCode != SocketError.WouldBlock && errorCode != SocketError.Success)
                     {
                         throw (new Exception("Connection reset " + errorCode));
                     }
                 }
                 else
                 {
                     break;
                 }
                 cnt++;
             }
         }
         else
         {
             throw (new Exception("Disconnected."));
         }
     }
     catch (Exception e)
     {
         Debug.LogError("Error: " + e.ToString());
         ErrorHub.ThrowError(8, (_name) => { Application.LoadLevel("Menus"); });
         Desconectar();
         if (OnDisconnect != null)
         {
             OnDisconnect();
         }
     }
 }
Exemple #3
0
 IEnumerator conectarSocket(IPEndPoint ipEndPoint)
 {
     try
     {
         m_socket.Connect(ipEndPoint);
         if (m_socket.Connected)
         {
             m_socket.Blocking = false;
             m_conectado       = true;
             if (OnConnect != null)
             {
                 OnConnect();
             }
         }
         else
         {
             m_socket.Shutdown(SocketShutdown.Both);
             m_socket.Close();
             m_socket = null;
             if (OnError != null)
             {
                 OnError("Error al conectar.");
             }
         }
     }
     catch (Exception e)
     {
         Debug.Log("CNetwork.beginConnect inicia la conexion. " + e);
         ErrorHub.ThrowError(7, (_name) => { Application.LoadLevel("Menus"); });
         if (OnError != null)
         {
             OnError("CNetwork.beginConnect inicia la conexion. " + e);
         }
     }
     yield return(true);
 }
Exemple #4
0
 public override void process()
 {
     ErrorHub.ProcessError(m_code);
 }