Пример #1
0
        public void Start()
        {
            m_MarineExchangeDB = new MarineExchangeEntities();

            // clients are authenticated either by IP or by iDevice ID
            // if by IP, the client just listens, and never sends any data
            // so check if IP is authorized first (there are problems with this - authorizing a single IP may authorize more users than anticipated. MXAK is aware of this)
            bool isAuthorized = false;

            IPAddress remoteIP;
            if (IPAddress.TryParse(((IPEndPoint)this.ClientSocket.RemoteEndPoint).Address.ToString(), out remoteIP))
            {
                // query the database for a client who has authorized this IP address
                try
                {
                    System.Data.Objects.ObjectResult<int?> hasClient = m_MarineExchangeDB.CheckUserByIP(remoteIP.ToString());

                    if (hasClient != null)
                    {
                        foreach (int clientID in hasClient)
                        {
                            isAuthorized = true;
                            ClientID = clientID;
                            DeviceID = "";

                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    m_MarineExchangeDB.LogError(ex.Message,
                                                ex.StackTrace,
                                                "AsynchronousClient.Start",
                                                ex.InnerException.ToString(),
                                                ex.TargetSite.ToString(),
                                                DateTime.Now,
                                                null,
                                                ClientID,
                                                DeviceID);

                    // reset the Client and Device IDs
                    ClientID = 0;
                    DeviceID = "";
                }
            }

            // if the client's IP address is not authorized, accept data from client
            // if this is an authorized client (iDevice - iPhone, iPad, etc), it'll send a 40 character string with it's identifier
            if (!isAuthorized)
            {
                this.sendDone.Reset();

                // call client's method to BeginReceive
                try
                {
                    this.ClientSocket.BeginReceive(this.buffer, 0, 1024, 0, new AsyncCallback(ServerReceiveCallback), this);
                }
                catch (Exception ex)
                {
                    m_MarineExchangeDB.LogError(ex.Message,
                            ex.StackTrace,
                            "AsynchronousClient.Start",
                            ex.InnerException.ToString(),
                            ex.TargetSite.ToString(),
                            DateTime.Now,
                            null,
                            ClientID,
                            DeviceID);

                    // response to any socket error is to close the socket and abort
                    this.ClientSocket.Close();
                    ClientID = 0;
                    DeviceID = "";
                }

                this.sendDone.WaitOne();
            }

            // log this connection, whether authorized or not - if a socket error occurs in BeginReceive, the client will not show up here
            AccessIP = remoteIP != null ? remoteIP.ToString() : "";
            m_MarineExchangeDB.LogOpenConnection(AccessIP, ClientID, DeviceID);

            // once the client has identified itself, begin sending data
            if (ClientID > 0)
            {
                FeedSubscriptions = new List<int>();

                // check client's subscriptions
                try
                {
                    System.Data.Objects.ObjectResult<CheckUser_Result> clientSubscriptions = m_MarineExchangeDB.FetchClientSubscriptions(ClientID);

                    if (clientSubscriptions != null)
                    {
                        foreach (CheckUser_Result subscription in clientSubscriptions)
                        {
                            FeedSubscriptions.Add(subscription.nmea_feed_id);
                        }
                    }
                }
                catch (Exception ex)
                {
                    m_MarineExchangeDB.LogError(ex.Message,
                                                ex.StackTrace,
                                                "AsynchronousClient.Start",
                                                ex.InnerException.ToString(),
                                                ex.TargetSite.ToString(),
                                                DateTime.Now,
                                                null,
                                                ClientID,
                                                DeviceID);
                }

                while (this.ClientSocket.Connected)
                {
                    if (FeedSubscriptions.Count <= 0)
                    {
                        // disconnect clients with no feeds
                        this.ClientSocket.Close();
                        m_MarineExchangeDB.LogCloseConnection(AccessIP, ClientID, DeviceID);
                        break;
                    }
                    else
                        ServerSend(this);
                }
            }
        }