Exemple #1
0
        public void StartListening()
        {
            m_MarineExchangeDB = new MarineExchangeEntities();

            byte[] bytes = new Byte[BufferSize];

            IPHostEntry ipHostInfo = new IPHostEntry();
            ipHostInfo.AddressList = new IPAddress[] { _serverIP };
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port);
            // Create a TCP/IP socket.
            listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.BeginConnect(remoteEndPoint, new AsyncCallback(ConnectCallback), listener);
            }
            catch (Exception ex)
            {
                listener.Close();

                m_MarineExchangeDB.LogError(ex.Message,
                                            ex.StackTrace,
                                            "AsynchronousSocketListener.StartListening",
                                            ex.InnerException.ToString(),
                                            ex.TargetSite.ToString(),
                                            DateTime.Now,
                                            FeedID,
                                            null,
                                            null);
            }

            connectDone.WaitOne();

            // receive data
            receiveDone.Reset();

            if (listener.Connected)
            {
                Receive(listener);
            }

            receiveDone.WaitOne();

            if (listener.Connected)
            {
                listener.Shutdown(SocketShutdown.Both);
                listener.Close();
            }
        }
        // main method
        // call this method to start the server
        public void StartListening()
        {
            m_MarineExchangeDB = new MarineExchangeEntities();

            IPHostEntry ipHostInfo = new IPHostEntry();
            ipHostInfo.AddressList = new IPAddress[] { _serverIP };
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, _serverPortNumber);
            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // client threads
            List<Thread> listOfThreads = new List<Thread>();
            _clients = new List<AsynchronousClient>();

            // connect to feeds configured in database
            SetupFeeds();

            if (_feeds.Count() > 0)
            {
                Thread transferBufferThread = new Thread(() => CheckFeedBuffer());
                transferBufferThread.Start();

                // start the listener socket
                try
                {
                    listener.Bind(localEndPoint);
                    listener.Listen(100);

                    // will continue to run as long as the thread which is transferring
                    while (transferBufferThread.IsAlive && _feeds.Count > 0)
                    {
                        // reset _addDone - will be set again in BeginAccept
                        _allDone.Reset();

                        Socket newSocket = null;

                        EntireConnection newConnection;
                        try
                        {
                            newConnection = (EntireConnection)listener.BeginAccept(new AsyncCallback(ServerAcceptCallback), new EntireConnection { serverSocket = listener, clientSocket = newSocket }).AsyncState;
                        }
                        catch (Exception ex)
                        {
                            m_MarineExchangeDB.LogError(ex.Message,
                                                        ex.StackTrace,
                                                        "AsynchronousSocketListener.StartListening",
                                                        ex.InnerException.ToString(),
                                                        ex.TargetSite.ToString(),
                                                        DateTime.Now,
                                                        null,
                                                        null,
                                                        null);

                            newConnection = null;
                        }

                        // wait till accept is complete
                        _allDone.WaitOne();

                        if (newConnection != null)
                        {
                            try
                            {
                                // create new client - client has own thread which starts processing
                                AsynchronousClient newClient = new AsynchronousClient(newConnection.clientSocket);
                                _clients.Add(newClient);
                            }
                            catch (Exception ex)
                            {
                                m_MarineExchangeDB.LogError(ex.Message,
                                                            ex.StackTrace,
                                                            "AsynchronousSocketListener.StartListening",
                                                            ex.InnerException.ToString(),
                                                            ex.TargetSite.ToString(),
                                                            DateTime.Now,
                                                            null,
                                                            null,
                                                            null);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    m_MarineExchangeDB.LogError(ex.Message,
                                                ex.StackTrace,
                                                "AsynchronousSocketListener.StartListening",
                                                ex.InnerException.ToString(),
                                                ex.TargetSite.ToString(),
                                                DateTime.Now,
                                                null,
                                                null,
                                                null);
                }
            }
        }
        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);
                }
            }
        }