Esempio n. 1
0
        public void Connect()
        {
            //if (DebugSocket != null)
            //if(DebugSocket.RemoteEndPoint.Equals(new IPEndPoint(RemoteIpAddress, DebugPort))) DebugSocket.Close();
            if (DataSocket != null)
            {
                if (DataSocket.RemoteEndPoint.Equals(new IPEndPoint(RemoteIpAddress, DataPort)))
                {
                    DataSocket.Close();
                }
            }

            var tcpConnector = new Connector();

            //DebugSocket = tcpConnector.ConnectTo(RemoteIpAddress, DebugPort);
            try
            {
                DataSocket = tcpConnector.ConnectTo(RemoteIpAddress, DataPort);
                OnConnected();
            }
            catch (Exception e)
            {
                Debug.Print(e.Message + e.InnerException);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Disconnect from the server.
        /// </summary>
        /// <param name="async">If true then async</param>
        public void Disconnect(bool async = false)
        {
            if (WSServer != null)
            {
                throw new Exception("Cannot both be a server and a client." +
                                    " Please use Disconnect if you are diconnecting from a server or StopListening to stop the server.");
            }

            if (WS == null)
            {
                return;
            }

            if (!WS.IsAlive)
            {
                return;
            }

            if (async)
            {
                WS.CloseAsync();
            }
            else
            {
                WS.Close();
            }

            DataSocket.Close();
        }
Esempio n. 3
0
        public bool Setup()
        {
            if (Config.Address == 0)
            {
                throw new InvalidOperationException("Configuration address not configured");
            }
            if (Config.Port == 0)
            {
                throw new InvalidOperationException("Configuration port not configured");
            }

            try
            {
                //check if the camera is active
                DataSocket.Connect(new IPEndPoint(Config.Address, Config.Port));
                DataSocket.Send(Encoding.ASCII.GetBytes((int)CameraRequest.Alive + Constants.EndOfMessage));

                //grab data
                byte[] recieveData = new byte[1000];
                int    bytesRec    = DataSocket.Receive(recieveData);
                //if there was no data the camera must have been off
                if (bytesRec <= 0)
                {
                    Console.WriteLine("Camera not active, No data recieved");
                    DataSocket.Shutdown(SocketShutdown.Both);
                    DataSocket.Close();
                    return(false);
                }
                else
                {
                    Console.WriteLine("Camera response = {0}",
                                      Encoding.ASCII.GetString(recieveData, 0, bytesRec - Constants.EndOfMessage.Length));
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Socket Exception : {0}", e);

                if (!DataSocket.Connected)
                {
                    return(false);
                }

                DataSocket.Shutdown(SocketShutdown.Both);
                DataSocket.Close();
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Stops listening to remote connections.
        /// </summary>
        public void StopListening()
        {
            if (WS != null)
            {
                throw new Exception("Cannot both be a server and a client." +
                                    " Please use Disconnect if you are diconnecting from a server or StopListening to stop the server.");
            }

            if (WSServer == null)
            {
                return;
            }

            if (WSServer.IsListening)
            {
                WSServer.Stop();
            }

            DataSocket.Close();
        }
Esempio n. 5
0
        public void Dispose()
        {
            if (WS != null && WS.IsAlive)
            {
                WS.Close();
            }
            if (WSServer != null && WSServer.IsListening)
            {
                WSServer.Stop();
            }

            if (DataSocket != null)
            {
                DataSocket.Close();
            }

            WS         = null;
            WSServer   = null;
            DataSocket = null;
            Serializer = null;
        }