Exemplo n.º 1
0
        internal void AcceptCallback(IAsyncResult ares)
        {
            try
            {
#if SSHARP
                AcceptSocket = ((CrestronListenerSocket)curSocket).EndAccept(ares);
#else
                AcceptSocket = curSocket.EndAccept(ares);
#endif
            }
            catch (SocketException ex)
            {
                SocketError = ex.SocketErrorCode;
            }
            catch (ObjectDisposedException)
            {
                SocketError = SocketError.OperationAborted;
            }
            finally
            {
                if (AcceptSocket == null)
#if SSHARP
                { AcceptSocket = new CrestronServerSocket((CrestronListenerSocket)curSocket, 0xffffffff); }
#else
                { AcceptSocket = new Socket(curSocket.AddressFamily, curSocket.SocketType, curSocket.ProtocolType, (IntPtr)(-1)); }
#endif
                OnCompleted(this);
            }
        }
        private static void OnAccept(object sender, EventArgs e)
        {
            SocketAsyncEventArgs args = (SocketAsyncEventArgs)e;
            EndPointListener     epl  = (EndPointListener)args.UserToken;

#if SSHARP
            CrestronServerSocket accepted = null;
#else
            Socket accepted = null;
#endif
            if (args.SocketError == SocketError.Success)
            {
                accepted          = args.AcceptSocket;
                args.AcceptSocket = null;
            }

            try
            {
                if (epl.sock != null)
#if SSHARP
                { epl.sock.BeginAccept(acceptCallback, Tuple.Create(epl.sock, args)); }
#else
                { epl.sock.AcceptAsync(args); }
#endif
            }
            catch
            {
                if (accepted != null)
                {
                    try
                    {
                        accepted.Close();
                    }
                    catch
                    {
                    }
                    accepted = null;
                }
            }

            if (accepted == null)
            {
                return;
            }

            if (epl.secure && (epl.cert == null || epl.key == null))
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert, epl.key);
            lock (epl.unregistered)
            {
                epl.unregistered[conn] = conn;
            }
            conn.BeginReadRequest();
        }
Exemplo n.º 3
0
        private static void onAccept(IAsyncResult asyncResult)
        {
            var lsnr = (EndPointListener)asyncResult.AsyncState;

#if SSHARP
            CrestronServerSocket sock = null;
#else
            Socket sock = null;
#endif
            try
            {
                sock = lsnr._socket.EndAccept(asyncResult);
            }
            catch (SocketException sex)
            {
                lsnr._logger.Error(sex.Message);
            }
            catch (ObjectDisposedException)
            {
                return;
            }

            try
            {
                lsnr._socket.BeginAccept(onAccept, lsnr);
            }
            catch
            {
                if (sock != null)
                {
                    sock.Close();
                }

                return;
            }

            if (sock == null)
            {
                return;
            }

            processAccepted(sock, lsnr);
        }
Exemplo n.º 4
0
        private static void onAccept(IAsyncResult asyncResult)
        {
            var lsnr = (EndPointListener)asyncResult.AsyncState;

#if SSHARP
            CrestronServerSocket sock = null;
#else
            Socket sock = null;
#endif
            try
            {
                sock = lsnr._socket.EndAccept(asyncResult);
            }
            catch (SocketException)
            {
                // TODO: Should log the error code when this class has a logging.
            }
            catch (ObjectDisposedException)
            {
                return;
            }

            try
            {
                lsnr._socket.BeginAccept(onAccept, lsnr);
            }
            catch
            {
                if (sock != null)
                {
                    sock.Close();
                }

                return;
            }

            if (sock == null)
            {
                return;
            }

            processAccepted(sock, lsnr);
        }
Exemplo n.º 5
0
 private static void processAccepted(CrestronServerSocket socket, EndPointListener listener)
Exemplo n.º 6
0
        private void OpenDataConnection()
        {
            FtpStatus status;

#if SSHARP
            object s = InitDataConnection();
#else
            Socket s = InitDataConnection();
#endif

            // Handle content offset
            if (offset > 0)
            {
                status = SendCommand(RestCommand, offset.ToString());
                if (status.StatusCode != FtpStatusCode.FileCommandPending)
                {
                    throw CreateExceptionFromResponse(status);
                }
            }

            if (method != WebRequestMethods.Ftp.ListDirectory && method != WebRequestMethods.Ftp.ListDirectoryDetails &&
                method != WebRequestMethods.Ftp.UploadFileWithUniqueName)
            {
                status = SendCommand(method, file_name);
            }
            else
            {
                status = SendCommand(method);
            }

            if (status.StatusCode != FtpStatusCode.OpeningData && status.StatusCode != FtpStatusCode.DataAlreadyOpen)
            {
                throw CreateExceptionFromResponse(status);
            }

            if (usePassive)
            {
#if SSHARP
                origDataStream = new NetworkStream((CrestronClientSocket)s, true);
#else
                origDataStream = new NetworkStream(s, true);
#endif
                dataStream = origDataStream;
                if (EnableSsl)
                {
                    ChangeToSSLSocket(ref dataStream);
                }
            }
            else
            {
                // Active connection (use Socket.Blocking to true)
#if SSHARP
                CrestronServerSocket incoming = null;
                try
                {
                    incoming = ((CrestronListenerSocket)s).Accept();
                }
#else
                Socket incoming = null;
                try
                {
                    incoming = s.Accept();
                }
#endif
                catch (SocketException)
                {
#if SSHARP
                    ((CrestronListenerSocket)s).Close();
#else
                    s.Close();
#endif
                    if (incoming != null)
                    {
                        incoming.Close();
                    }

                    throw new ProtocolViolationException("Server commited a protocol violation.");
                }

#if SSHARP
                ((CrestronListenerSocket)s).Close();
#else
                s.Close();
#endif
                origDataStream = new NetworkStream(incoming, true);
                dataStream     = origDataStream;
                if (EnableSsl)
                {
                    ChangeToSSLSocket(ref dataStream);
                }
            }

            ftpResponse.UpdateStatus(status);
        }