public static MyNetworkStream CreateStream(MySqlConnectionStringBuilder settings, bool unix)
        {
            MyNetworkStream myNetworkStream = null;

            IPAddress[] addressList = MyNetworkStream.GetHostEntry(settings.Server).AddressList;
            for (int i = 0; i < addressList.Length; i++)
            {
                IPAddress ip = addressList[i];
                try
                {
                    myNetworkStream = MyNetworkStream.CreateSocketStream(settings, ip, unix);
                    if (myNetworkStream != null)
                    {
                        break;
                    }
                }
                catch (Exception arg_2B_0)
                {
                    SocketException expr_30 = arg_2B_0 as SocketException;
                    if (expr_30 == null)
                    {
                        throw;
                    }
                    if (expr_30.SocketErrorCode != SocketError.ConnectionRefused)
                    {
                        throw;
                    }
                }
            }
            return(myNetworkStream);
        }
Exemplo n.º 2
0
        public static async Task <MyNetworkStream> CreateStream(MySqlConnectionStringBuilder settings, bool unix)
        {
            MyNetworkStream stream = null;
            IPHostEntry     ipHE   = await GetHostEntry(settings.Server);

            foreach (IPAddress address in ipHE.AddressList)
            {
                try
                {
                    stream = CreateSocketStream(settings, address, unix);
                    if (stream != null)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    SocketException socketException = ex as SocketException;
                    // if the exception is a ConnectionRefused then we eat it as we may have other address
                    // to attempt
                    if (socketException == null)
                    {
                        throw;
                    }
#if !CF
                    if (socketException.SocketErrorCode != SocketError.ConnectionRefused)
                    {
                        throw;
                    }
#endif
                }
            }
            return(stream);
        }
Exemplo n.º 3
0
        private static MyNetworkStream CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, bool unix)
        {
            EndPoint endPoint;

#if !CF && !NETSTANDARD1_5
            if (!Platform.IsWindows() && unix)
            {
                endPoint = CreateUnixEndPoint(settings.Server);
            }
            else
#endif

            endPoint = new IPEndPoint(ip, (int)settings.Port);

            Socket socket = unix ?
                            new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                            new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (settings.Keepalive > 0)
            {
                SetKeepAlive(socket, settings.Keepalive);
            }


            var connectTask = socket.ConnectAsync(endPoint);
            if (!connectTask.Wait((int)settings.ConnectionTimeout * 1000))
            {
                socket.Dispose();
                return(null);
            }

            MyNetworkStream stream = new MyNetworkStream(socket, true);
            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return(stream);
        }
Exemplo n.º 4
0
        public static MyNetworkStream CreateStream(MySqlConnectionStringBuilder settings, bool unix)
        {
            MyNetworkStream stream = null;

            foreach (IPAddress address in GetHostEntry(settings.Server).AddressList)
            {
                try
                {
                    stream = CreateSocketStream(settings, address, unix);
                    if (stream != null)
                    {
                        return(stream);
                    }
                }
                catch (Exception exception)
                {
                    SocketException exception2 = exception as SocketException;
                    if (exception2 == null)
                    {
                        throw;
                    }
                    if (exception2.SocketErrorCode != SocketError.ConnectionRefused)
                    {
                        throw;
                    }
                }
            }
            return(stream);
        }
Exemplo n.º 5
0
        public static MyNetworkStream CreateStream(MySqlConnectionStringBuilder settings, bool unix)
        {
            MyNetworkStream s = new MyNetworkStream(settings.Server, (int)settings.Port, (int)settings.ConnectionTimeout);

            s.Open();
            return(s);
        }
Exemplo n.º 6
0
 private static Stream GetUnixSocketStream(MySqlConnectionStringBuilder settings)
 {
     if (Platform.IsWindows())
     {
         throw new InvalidOperationException(Resources.NoUnixSocketsOnWindows);
     }
     return(MyNetworkStream.CreateStream(settings, true));
 }
        private static IPHostEntry GetHostEntry(string hostname)
        {
            IPHostEntry iPHostEntry = MyNetworkStream.ParseIPAddress(hostname);

            if (iPHostEntry != null)
            {
                return(iPHostEntry);
            }
            return(Dns.GetHostEntry(hostname));
        }
Exemplo n.º 8
0
        public static MyNetworkStream CreateStream(string server, uint connectionTimeout, uint keepAlive, uint port, bool unix)
        {
            if (unix)
            {
                return(new MyNetworkStream(StreamCreator.GetUnixSocket(server, connectionTimeout, keepAlive), true));
            }

            MyNetworkStream stream = null;

            IPHostEntry ipHE = GetHostEntry(server);

            foreach (IPAddress address in ipHE.AddressList)
            {
                try
                {
                    stream = CreateSocketStream(port, keepAlive, connectionTimeout, address, unix);
                    if (stream != null)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    string exTimeOutMessage = connectionTimeout == 0 ? ResourcesX.TimeOutSingleHost0ms : String.Format(ResourcesX.TimeOutSingleHost, connectionTimeout);

                    if (ex is TimeoutException)
                    {
                        throw new TimeoutException(exTimeOutMessage);
                    }

                    SocketException socketException = ex as SocketException;

#if NETSTANDARD1_6
                    socketException = ex.InnerException as SocketException;
#endif
                    // if the exception is a ConnectionRefused then we eat it as we may have other address
                    // to attempt
                    if (socketException == null)
                    {
                        throw;
                    }
                    if (socketException.SocketErrorCode == SocketError.TimedOut)
                    {
                        throw new TimeoutException(exTimeOutMessage);
                    }
                    if (socketException.SocketErrorCode != SocketError.ConnectionRefused)
                    {
                        throw;
                    }
                }
            }
            return(stream);
        }
Exemplo n.º 9
0
        private static MyNetworkStream CreateSocketStream(uint port, uint keepAlive, uint connectionTimeout, IPAddress ip, bool unix)
        {
            EndPoint endPoint;

            endPoint = new IPEndPoint(ip, (int)port);
            Socket socket = unix ?
                            new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                            new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            socket.NoDelay = true;

            if (keepAlive > 0)
            {
                SetKeepAlive(socket, keepAlive);
            }

            IAsyncResult ias = socket.BeginConnect(endPoint, null, null);

            if (connectionTimeout == 0)
            {
                if (!ias.AsyncWaitHandle.WaitOne())
                {
                    socket.Close();
                }
            }
            else
            {
                if (!ias.AsyncWaitHandle.WaitOne((int)connectionTimeout, false))
                {
                    socket.Close();
                    throw new TimeoutException();
                }
            }

            try
            {
                socket.EndConnect(ias);
            }
            catch (Exception)
            {
                socket.Close();
                throw;
            }

            MyNetworkStream stream = new MyNetworkStream(socket, true);

            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return(stream);
        }
Exemplo n.º 10
0
        private static MyNetworkStream CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, bool unix)
        {
            EndPoint endPoint;

            if (!Platform.IsWindows() && unix)
            {
                endPoint = CreateUnixEndPoint(settings.Server);
            }
            else
            {
                endPoint = new IPEndPoint(ip, (int)settings.Port);
            }

            Socket socket = unix ?
                            new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                            new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            if (settings.Keepalive > 0)
            {
                SetKeepAlive(socket, settings.Keepalive);
            }

            //This fails on mono linux

            /*IAsyncResult ias = socket.BeginConnect(endPoint, null, null);
             * if (!ias.AsyncWaitHandle.WaitOne((int)settings.ConnectionTimeout * 1000, false))
             * {
             *      socket.Close();
             *      return null;
             * }
             * try
             * {
             *      socket.EndConnect(ias);
             * }
             * catch (Exception)
             * {
             *      socket.Close();
             *      throw;
             * }*/

            // Instead do a regular blocking connect to the endpoint.
            try { socket.Connect(endPoint); }
            catch (Exception ex) { throw; }

            MyNetworkStream stream = new MyNetworkStream(socket, true);

            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return(stream);
        }
Exemplo n.º 11
0
        private Stream CreateSocketStream(IPAddress ip, bool unix)
        {
            EndPoint endPoint;

#if !CF
            if (!Platform.IsWindows() && unix)
            {
                endPoint = CreateUnixEndPoint(hostList);
            }
            else
#endif

            endPoint = new IPEndPoint(ip, (int)port);

#if !CF
            MySqlSecurityPermission.CreatePermissionSet(false).Assert();
#endif

            Socket socket = unix ?
                            new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                            new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (keepalive > 0)
            {
                SetKeepAlive(socket, keepalive);
            }


            IAsyncResult ias = socket.BeginConnect(endPoint, null, null);
            if (!ias.AsyncWaitHandle.WaitOne((int)timeOut * 1000, false))
            {
                socket.Close();
                return(null);
            }
            try
            {
                socket.EndConnect(ias);
            }
            catch (Exception)
            {
                socket.Close();
                throw;
            }
            MyNetworkStream stream = new MyNetworkStream(socket, true);
            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return(stream);
        }
Exemplo n.º 12
0
        private static MyNetworkStream CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, bool unix)
        {
            EndPoint endPoint;

#if !CF
            if (!Platform.IsWindows() && unix)
            {
                endPoint = CreateUnixEndPoint(settings.Server);
            }
            else
#endif

            endPoint = new IPEndPoint(ip, (int)settings.Port);

            Socket socket = unix ?
                            new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                            new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (settings.Keepalive > 0)
            {
                SetKeepAlive(socket, settings.Keepalive);
            }


            IAsyncResult ias = socket.BeginConnect(endPoint, null, null);
            if (!ias.AsyncWaitHandle.WaitOne((int)settings.ConnectionTimeout * 1000, false))
            {
                socket.Close();
                return(null);
            }
            try
            {
                socket.EndConnect(ias);
            }
            catch (Exception)
            {
                socket.Close();
                throw;
            }
            MyNetworkStream stream = new MyNetworkStream(socket, true);
            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return(stream);
        }
Exemplo n.º 13
0
        private static MyNetworkStream CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, bool unix)
        {
            EndPoint remoteEP;

            if (!Platform.IsWindows() & unix)
            {
                remoteEP = MyNetworkStream.CreateUnixEndPoint(settings.Server);
            }
            else
            {
                remoteEP = new IPEndPoint(ip, (int)settings.Port);
            }
            Socket socket = unix ? new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) : new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            if (settings.Keepalive > 0u)
            {
                MyNetworkStream.SetKeepAlive(socket, settings.Keepalive);
            }
            IAsyncResult asyncResult = socket.BeginConnect(remoteEP, null, null);

            if (!asyncResult.AsyncWaitHandle.WaitOne((int)(settings.ConnectionTimeout * 1000u), false))
            {
                socket.Close();
                return(null);
            }
            try
            {
                socket.EndConnect(asyncResult);
            }
            catch (Exception)
            {
                socket.Close();
                throw;
            }
            MyNetworkStream myNetworkStream = new MyNetworkStream(socket, true);

            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(myNetworkStream);
            return(myNetworkStream);
        }
Exemplo n.º 14
0
        private static MyNetworkStream CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, bool unix)
        {
            EndPoint endPoint;


            endPoint = new IPEndPoint(ip, (int)settings.Port);

            Socket socket = unix ?
                            new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                            new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            if (settings.Keepalive > 0)
            {
                SetKeepAlive(socket, settings.Keepalive);
            }


            var connectTask = socket.ConnectAsync(endPoint);

            if (!connectTask.Wait((int)settings.ConnectionTimeout * 1000))
            {
                socket.Dispose();
                return(null);
            }

            try
            {
            }
            catch (Exception)
            {
                socket.Dispose();
                throw;
            }
            MyNetworkStream stream = new MyNetworkStream(socket, true);

            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return(stream);
        }
Exemplo n.º 15
0
        public static MyNetworkStream CreateStream(MySqlConnectionStringBuilder settings, bool unix)
        {
            if (unix)
            {
                return(new MyNetworkStream(StreamCreator.GetUnixSocket(settings), true));
            }

            MyNetworkStream stream = null;

            IPHostEntry ipHE = GetHostEntry(settings.Server);

            foreach (IPAddress address in ipHE.AddressList)
            {
                try
                {
                    stream = CreateSocketStream(settings, address, unix);
                    if (stream != null)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    SocketException socketException = ex as SocketException;
                    // if the exception is a ConnectionRefused then we eat it as we may have other address
                    // to attempt
                    if (socketException == null)
                    {
                        throw;
                    }
                    if (socketException.SocketErrorCode != SocketError.ConnectionRefused)
                    {
                        throw;
                    }
                }
            }
            return(stream);
        }
Exemplo n.º 16
0
        private static MyNetworkStream CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, bool unix)
        {
            EndPoint endPoint = new IPEndPoint(ip, (int)settings.Port);

            Socket socket = unix ?
                new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (settings.Keepalive > 0)
            {
                SetKeepAlive(socket, settings.Keepalive);
            }

            IAsyncResult ias = socket.ConnectAsync(endPoint);
            if (!ias.AsyncWaitHandle.WaitOne((int)settings.ConnectionTimeout * 1000))
            {
                socket.Dispose();
                return null;
            }
            try
            {
                //socket.EndConnect();
            }
            catch (Exception)
            {
                socket.Dispose();
                throw;
            }
            MyNetworkStream stream = new MyNetworkStream(socket, true);
            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return stream;
        }
Exemplo n.º 17
0
        private static Stream GetTcpStream(MySqlConnectionStringBuilder settings)
        {
            MyNetworkStream s = MyNetworkStream.CreateStream(settings, false);

            return(s);
        }
 public static MyNetworkStream CreateStream(MySqlConnectionStringBuilder settings, bool unix)
 {
   MyNetworkStream s = new MyNetworkStream(settings.Server, (int)settings.Port, (int)settings.ConnectionTimeout);
   s.Open();
   return s;
 }
Exemplo n.º 19
0
        private static MyNetworkStream CreateSocketStream(MySqlConnectionStringBuilder settings, IPAddress ip, bool unix)
        {
            EndPoint endPoint;
            #if !CF
              if (!Platform.IsWindows() && unix)
            endPoint = CreateUnixEndPoint(settings.Server);
              else
            #endif

            endPoint = new IPEndPoint(ip, (int)settings.Port);

              Socket socket = unix ?
              new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
              new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
              if (settings.Keepalive > 0)
              {
            SetKeepAlive(socket, settings.Keepalive);
              }

              IAsyncResult ias = socket.BeginConnect(endPoint, null, null);
              if (!ias.AsyncWaitHandle.WaitOne((int)settings.ConnectionTimeout * 1000, false))
              {
            socket.Close();
            return null;
              }
              try
              {
            socket.EndConnect(ias);
              }
              catch (Exception)
              {
            socket.Close();
            throw;
              }
              MyNetworkStream stream = new MyNetworkStream(socket, true);
              GC.SuppressFinalize(socket);
              GC.SuppressFinalize(stream);
              return stream;
        }
Exemplo n.º 20
0
        //#if NETSTANDARD1_6
        //    private static EndPoint CreateUnixEndPoint(string host)
        //    {
        //      // first we need to load the Mono.posix assembly
        //      Assembly a = Assembly.Load(@"Mono.Posix, Version=2.0.0.0,
        //                Culture=neutral, PublicKeyToken=0738eb9f132ed756");


        //      // then we need to construct a UnixEndPoint object
        //      EndPoint ep = (EndPoint)a.CreateInstance("Mono.Posix.UnixEndPoint",
        //          false, BindingFlags.CreateInstance, null,
        //          new object[1] { host }, null, null);
        //      return ep;
        //    }
        //#endif

        private static MyNetworkStream CreateSocketStream(uint port, uint keepAlive, uint connectionTimeout, IPAddress ip, bool unix)
        {
            EndPoint endPoint;

            //#if NETSTANDARD1_6
            //      if (!Platform.IsWindows() && unix)
            //      {
            //        endPoint = CreateUnixEndPoint(settings.Server);
            //        //endPoint = new DnsEndPoint(settings.Server, (int)settings.Port);
            //      }
            //      else
            //      {

            endPoint = new IPEndPoint(ip, (int)port);
            //      }
            Socket socket = unix ?
                            new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
                            new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            socket.NoDelay = true;

            if (keepAlive > 0)
            {
                SetKeepAlive(socket, keepAlive);
            }

#if NETSTANDARD1_6
            try
            {
                Task ias = socket.ConnectAsync(endPoint);
                if (connectionTimeout == 0)
                {
                    ias.Wait();
                }
                else
                {
                    if (!ias.Wait(((int)connectionTimeout)))
                    {
                        socket.Dispose();
                        throw new TimeoutException();
                    }
                }
            }
            catch (Exception)
            {
                socket.Dispose();
                throw;
            }
#else
            IAsyncResult ias = socket.BeginConnect(endPoint, null, null);

            if (connectionTimeout == 0)
            {
                if (!ias.AsyncWaitHandle.WaitOne())
                {
                    socket.Close();
                }
            }
            else
            {
                if (!ias.AsyncWaitHandle.WaitOne((int)connectionTimeout, false))
                {
                    socket.Close();
                    throw new TimeoutException();
                }
            }

            try
            {
                socket.EndConnect(ias);
            }
            catch (Exception)
            {
                socket.Close();
                throw;
            }
#endif

            MyNetworkStream stream = new MyNetworkStream(socket, true);
            GC.SuppressFinalize(socket);
            GC.SuppressFinalize(stream);
            return(stream);
        }
Exemplo n.º 21
0
    private Stream CreateSocketStream(IPAddress ip, bool unix)
    {
      EndPoint endPoint;
#if !CF
      if (!Platform.IsWindows() && unix)
        endPoint = CreateUnixEndPoint(hostList);
      else
#endif
      
      endPoint = new IPEndPoint(ip, (int)port);

#if !CF 
      MySqlSecurityPermission.CreatePermissionSet(false).Assert();
#endif

      Socket socket = unix ?
          new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) :
          new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
      if (keepalive > 0)
      {
        SetKeepAlive(socket, keepalive);
      }


      IAsyncResult ias = socket.BeginConnect(endPoint, null, null);
      if (!ias.AsyncWaitHandle.WaitOne((int)timeOut * 1000, false))
      {
        socket.Close();
        return null;
      }
      try
      {
        socket.EndConnect(ias);
      }
      catch (Exception)
      {
        socket.Close();
        throw;
      }
      MyNetworkStream stream = new MyNetworkStream(socket, true);
      GC.SuppressFinalize(socket);
      GC.SuppressFinalize(stream);
      return stream;
    }
Exemplo n.º 22
0
        private static async Task <Stream> GetTcpStream(MySqlConnectionStringBuilder settings)
        {
            var s = await MyNetworkStream.CreateStream(settings, false);

            return(s);
        }