Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="call"></param>
        /// <param name="prefixInfo"></param>
        /// <returns></returns>
        internal XDocument QRZRequest(string call)
        {
            WebResponse webResponse;
            WebRequest  webRequest;
            XDocument   xDocument  = new XDocument();
            string      requestUri = null;

            try
            {
                if (QRZSessionKey == null)
                {
                    OnErrorDetected?.Invoke("Session key is missing.");
                }

                requestUri = string.Format("http://xmldata.qrz.com/xml/current/?s={0};callsign={1}", QRZSessionKey, call);
                webRequest = WebRequest.Create(requestUri);

                webResponse = webRequest.GetResponse();
                xDocument   = XDocument.Load(webResponse.GetResponseStream());

                if (!CheckForValidData(xDocument))
                {
                    xDocument = null;
                }
            }
            catch (Exception ex)
            {
                OnErrorDetected?.Invoke(ex.Message);
            }

            return(xDocument);
        }
        private bool SendMatrixBytes(Socket sock, byte[] bytes)
        {
            byte[] buffer      = new byte[BUFSIZE];
            int    bytesToSend = bytes.Length;

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                do
                {
                    try
                    {
                        int packet = ms.Read(buffer, 0, BUFSIZE);
                        sock.Send(buffer);
                        bytesToSend -= packet;
                    }
                    catch (Exception e)
                    {
                        if (OnErrorDetected != null)
                        {
                            OnErrorDetected.Invoke(e.ToString());
                        }
                        return(false);
                    }
                } while (bytesToSend > 0);
            }

            return(true);
        }
        private byte[] ReceiveMatrixBytes(Socket sock)
        {
            int bytesToReceive;

            byte[] recv = new byte[BUFSIZE];

            try
            {
                byte[] dataLen  = new byte[INT32BDEPTH];
                int    numbytes = sock.Receive(dataLen);

                if (numbytes != INT32BDEPTH)
                {
                    return(new byte[0]);
                }

                bytesToReceive = BitConverter.ToInt32(dataLen, 0);

                if (bytesToReceive == 0)
                {
                    return(new byte[0]);
                }
            }
            catch (Exception e)
            {
                if (OnErrorDetected != null)
                {
                    OnErrorDetected.Invoke(e.ToString());
                }
                return(new byte[0]);
            }

            byte[] bytes = new byte[bytesToReceive];
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                while (bytesToReceive > 0)
                {
                    try
                    {
                        int packet = sock.Receive(recv);
                        ms.Write(recv, 0, packet);
                        bytesToReceive -= packet;
                    }
                    catch (Exception e)
                    {
                        if (OnErrorDetected != null)
                        {
                            OnErrorDetected.Invoke(e.ToString());
                        }
                        bytes = new byte[0];
                        break;
                    }
                }
            }
            return(bytes);
        }
 private bool TryConnect(Socket sock)
 {
     try
     {
         sock.Connect(ipEndPoint);
         return(true);
     }
     catch (Exception e)
     {
         if (OnErrorDetected != null)
         {
             OnErrorDetected.Invoke(e.ToString());
         }
         sock.Disconnect(true);
         return(false);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Logon to QRZ.com - TODO: Add credentials to Config screen
        /// Save the session key.
        /// </summary>
        /// <returns></returns>
        internal bool QRZLogon(string userId, string password)
        {
            switch (QRZSessionKey)
            {
            case null:
            {
                var       requestUri = string.Format("http://xmldata.qrz.com/xml/current/?username={0};password={1};{2}={3}", userId, password, "CallParser", "2.0");
                var       request    = WebRequest.Create(requestUri);
                var       response   = request.GetResponse();
                XDocument xDocument  = XDocument.Load(response.GetResponseStream());

                XNamespace xname = "http://xmldata.qrz.com";

                var error = xDocument.Descendants(xname + "Session").Select(x => x.Element(xname + "Error")).FirstOrDefault();

                if (error == null)
                {
                    var key = xDocument.Descendants(xname + "Session").Select(x => x.Element(xname + "Key").Value).FirstOrDefault();
                    QRZSessionKey = key.ToString();
                    return(true);
                }
                else
                {
                    QRZSessionKey = null;
                    OnErrorDetected?.Invoke(error.Value);
                }

                break;
            }

            default:
                return(true);
            }

            return(false);
        }