Esempio n. 1
0
        void m_Connection_StatusChange(object sender, ConnectionStatusChangeEventArgs e)
        {
            var StatusChangeArgs = new ClientInterfaceStatusChangeEventArgs();

            StatusChangeArgs.Status = e.State;

            OnStatusChange(StatusChangeArgs);
        }
Esempio n. 2
0
        protected virtual void OnStatusChange(ConnectionStatusChangeEventArgs e)
        {
            var handler = StatusChange;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Esempio n. 3
0
        void m_Connection_StatusChange(object sender, ConnectionStatusChangeEventArgs e)
        {
            // Log.WriteLog(Log.LogLevelType.Comm, "CommandInterface::StatusChange(): " +
            //     "[" + m_Connection.Address + ":" + m_Connection.Port + "] " +
            //      "State Change to " + e.State.ToString().ToUpper());

            var StatusChangeArgs = new CommandInterfaceStatusChangeEventArgs();

            StatusChangeArgs.Status = e.State;

            OnStatusChange(StatusChangeArgs);
        }
Esempio n. 4
0
        private void ConnectCallback(IAsyncResult ar)
        {
            StateObject state  = (StateObject)ar.AsyncState;
            Socket      client = state.WorkSocket;

            m_Connecting = false;

            //ensure current client connection is the same as
            //the one contained in the callback object
            if (client != ClientSocket)
            {
                return;
            }

            try
            {
                client.EndConnect(ar);
            }
            catch (SocketException e)
            {
                ConnectionErrorEventArgs args = new ConnectionErrorEventArgs();
                args.ConnectionID = m_ID;
                args.ErrorCode    = e.ErrorCode;
                args.ErrorMessage = e.Message;
                OnError(args);

                Disconnect();
                return;
            }
            catch (Exception)
            {
                Disconnect();
                return;
            }

            if (client != ClientSocket)
            {
                return;
            }

            if (client.Connected == true)
            {
                ConnectionStatusChangeEventArgs StatusArgs = new ConnectionStatusChangeEventArgs();
                StatusArgs.ConnectionID = m_ID;
                StatusArgs.State        = ConnectionStatus.CONNECTED;
                OnStatusChange(StatusArgs);

                if (client != ClientSocket)
                {
                    Disconnect();
                    return;
                }

                if (ClientSocket == null)
                {
                    return;
                }

                if (client.Connected == false)
                {
                    Disconnect();
                    return;
                }

                try
                {
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                }
                catch (SocketException e)
                {
                    ConnectionErrorEventArgs args = new ConnectionErrorEventArgs();
                    args.ConnectionID = m_ID;
                    args.ErrorCode    = e.ErrorCode;
                    args.ErrorMessage = e.Message;
                    OnError(args);
                    return;
                }
                catch (Exception)
                {
                    return;
                }
            }
        }
Esempio n. 5
0
        public Boolean Disconnect()
        {
            m_Connecting = false;

            //if socket is created
            if (ClientSocket != null)
            {
                //if socket is connected, disconnect
                if (ClientSocket.Connected == true)
                {
                    try
                    {
                        //Turn off Sending
                        ClientSocket.Shutdown(SocketShutdown.Send);
                        //ClientSocket.Disconnect(True)

                        //wait for socket to be empty
                        Byte[] b = new Byte[256];
                        while (ClientSocket.Receive(b, 256, SocketFlags.None) > 0)
                        {
                            System.Threading.Thread.Sleep(1);
                        }

                        //Shutdown the Recieve Socket!
                        if (ClientSocket != null)
                        {
                            ClientSocket.Shutdown(SocketShutdown.Receive);
                        }
                    }
                    catch (SocketException e)
                    {
                        ConnectionErrorEventArgs args = new ConnectionErrorEventArgs();
                        args.ConnectionID = m_ID;
                        args.ErrorCode    = e.ErrorCode;
                        args.ErrorMessage = e.Message;
                        OnError(args);
                    }
                    catch (Exception)
                    {
                        /* We are trying to catch the NullReferenceException
                         * but we still want to trigger the ConnectionStatusChangeEventArgs
                         * so allow the error to fall through
                         */
                    }

                    if (ClientSocket != null)
                    {
                        try
                        {
                            ClientSocket.Disconnect(false);
                        }
                        catch (SocketException e)
                        {
                            ConnectionErrorEventArgs args = new ConnectionErrorEventArgs();
                            args.ConnectionID = m_ID;
                            args.ErrorCode    = e.ErrorCode;
                            args.ErrorMessage = e.Message;
                            OnError(args);
                        }
                        catch (Exception)
                        {
                            /* We are trying to catch the NullReferenceException
                             * but we still want to trigger the ConnectionStatusChangeEventArgs
                             * so allow the error to fall through
                             */
                        }
                    }
                }

                //close socket object
                if (ClientSocket != null)
                {
                    ClientSocket.Close(1);
                    ClientSocket = null;
                }

                ConnectionStatusChangeEventArgs StatusArgs = new ConnectionStatusChangeEventArgs();
                StatusArgs.ConnectionID = m_ID;
                StatusArgs.State        = ConnectionStatus.DISCONNECTED;
                OnStatusChange(StatusArgs);
            }

            Address = "";
            Port    = 0;

            return(true);
        }
Esempio n. 6
0
        public Boolean Connect(int Port, string Address)
        {
            if (Address == null)
            {
                return(false);
            }

            if ((Port == 0) || (Address.Trim() == ""))
            {
                return(false);
            }

            //already connected
            if (Connected)
            {
                return(false);
            }

            //already in async connect
            if (m_Connecting == true)
            {
                return(true);
            }

            //trying to connect when already connected, return true
            //if (ClientSocket != null)
            //    if (ClientSocket.Connected == true)
            //        return true;

            this.Address = Address;
            this.Port    = Port;

            //create new connection
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            StateObject state = new StateObject();

            state.WorkSocket = ClientSocket;

            m_Connecting = true;

            ConnectionStatusChangeEventArgs StatusArgs = new ConnectionStatusChangeEventArgs();

            StatusArgs.ConnectionID = m_ID;
            StatusArgs.State        = ConnectionStatus.CONNECTING;
            OnStatusChange(StatusArgs);

            try
            {
                ClientSocket.BeginConnect(Address, Port, new AsyncCallback(ConnectCallback), state);
            }
            catch (SocketException e)
            {
                /* Generate Event */
                ConnectionErrorEventArgs args = new ConnectionErrorEventArgs();
                args.ConnectionID = m_ID;
                args.ErrorCode    = e.ErrorCode;
                args.ErrorMessage = e.Message;
                OnError(args);

                Disconnect();
                return(false);
            }
            catch (Exception)
            {
                Disconnect();
                return(false);
            }

            return(true);
        }