public void OnConnect(IWebSession session)
        {
            //Console.WriteLine(m_LocalNode.GetName() + " - OnConnect: " + session.GetRemoteAddress());
            //m_LocalNode.GetLogger().Info("OnClientConnect: " + session.GetRequestPath());

            if (m_Clients == null)
            {
                session.CloseConnection();
                return;
            }

            var count        = 0;
            var playerId     = "";
            var merchantCode = "";
            var currencyCode = "";
            var sessionId    = "";
            var parts        = session.GetRequestPath().Split('/');

            foreach (var part in parts)
            {
                if (part.Length <= 0)
                {
                    continue;
                }
                count++;
                if (count == 1)
                {
                    merchantCode = part;
                }
                if (count == 2)
                {
                    currencyCode = part;
                }
                if (count == 3)
                {
                    playerId = part;
                }
                if (count == 4)
                {
                    sessionId = part;
                }
                if (count > 4)
                {
                    break;
                }
            }

            var okay        = false;
            var clientToken = "";

            if (!String.IsNullOrEmpty(merchantCode) &&
                !String.IsNullOrEmpty(currencyCode) &&
                !String.IsNullOrEmpty(playerId) &&
                !String.IsNullOrEmpty(sessionId))
            {
                var dbhelper = m_LocalNode.GetDataHelper();
                using (var cnn = dbhelper.OpenDatabase(m_MainCache))
                {
                    using (var cmd = cnn.CreateCommand())
                    {
                        dbhelper.AddParam(cmd, "@session_id", sessionId);
                        dbhelper.AddParam(cmd, "@merchant_code", merchantCode);
                        dbhelper.AddParam(cmd, "@currency_code", currencyCode);
                        dbhelper.AddParam(cmd, "@player_id", playerId);

                        cmd.CommandText = " select * from tbl_player_session "
                                          + " where merchant_code = @merchant_code "
                                          + " and currency_code = @currency_code "
                                          + " and player_id = @player_id "
                                          + " and session_id = @session_id "
                        ;

                        using (var reader = cmd.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                okay = true;
                            }
                        }
                    }

                    if (okay)
                    {
                        clientToken = Guid.NewGuid().ToString();
                        using (var cmd = cnn.CreateCommand())
                        {
                            dbhelper.AddParam(cmd, "@client_token", clientToken);
                            dbhelper.AddParam(cmd, "@session_id", sessionId);
                            dbhelper.AddParam(cmd, "@merchant_code", merchantCode);
                            dbhelper.AddParam(cmd, "@currency_code", currencyCode);
                            dbhelper.AddParam(cmd, "@player_id", playerId);

                            cmd.CommandText = " update tbl_player_session "
                                              + " set client_token = @client_token, update_time = NOW() "
                                              + " where merchant_code = @merchant_code "
                                              + " and currency_code = @currency_code "
                                              + " and player_id = @player_id "
                                              + " and session_id = @session_id "
                            ;

                            okay = cmd.ExecuteNonQuery() > 0;
                        }
                    }
                }
            }

            //if (okay) m_LocalNode.GetLogger().Info("Client session is ok: " + sessionId);
            //else m_LocalNode.GetLogger().Info("Invalid session: " + sessionId);

            if (okay && m_Clients != null)
            {
                m_Clients.AddClient(session.GetRemoteAddress(), merchantCode, currencyCode, playerId, session);

                var clientMsg = new
                {
                    msg = "client_info",

                    client_id    = session.GetRemoteAddress(),
                    front_end    = m_LocalNode.GetName(),
                    client_token = clientToken,
                    action       = "connect"
                };

                var server = m_LocalNode.GetPublicServer();
                if (server != null && server.IsWorking())
                {
                    session.Send(m_LocalNode.GetJsonHelper().ToJsonString(clientMsg));
                }
            }
            else
            {
                session.CloseConnection();
            }
        }
Exemplo n.º 2
0
        public void OnConnect(IWebSession session)
        {
            //Console.WriteLine(m_LocalNode.GetName() + " - OnConnect: " + session.GetRemoteAddress());

            //System.Diagnostics.Debugger.Break();

            m_LocalNode.GetLogger().Info("OnClientConnect: " + session.GetRequestPath());

            var count        = 0;
            var playerId     = "";
            var merchantCode = "";
            var sessionId    = "";
            var parts        = session.GetRequestPath().Split('/');

            foreach (var part in parts)
            {
                if (part.Length <= 0)
                {
                    continue;
                }
                count++;
                if (count == 1)
                {
                    merchantCode = part;
                }
                if (count == 2)
                {
                    playerId = part;
                }
                if (count == 3)
                {
                    sessionId = part;
                }
                if (count > 3)
                {
                    break;
                }
            }

            var okay = false;

            if (!String.IsNullOrEmpty(merchantCode) &&
                !String.IsNullOrEmpty(playerId) &&
                !String.IsNullOrEmpty(sessionId))
            {
                var dbhelper = m_LocalNode.GetDataHelper();
                using (var cnn = dbhelper.OpenDatabase(m_MainCache))
                {
                    using (var cmd = cnn.CreateCommand())
                    {
                        dbhelper.AddParam(cmd, "@session_id", sessionId);
                        dbhelper.AddParam(cmd, "@merchant_code", merchantCode);
                        dbhelper.AddParam(cmd, "@player_id", playerId);

                        cmd.CommandText = " select * from tbl_player_session "
                                          + " where merchant_code = @merchant_code "
                                          + " and player_id = @player_id "
                                          + " and session_id = @session_id "
                        ;

                        using (var reader = cmd.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                okay = true;
                            }
                        }
                    }
                }
            }

            if (okay)
            {
                m_LocalNode.GetLogger().Info("Client session is ok: " + sessionId);
            }
            else
            {
                m_LocalNode.GetLogger().Info("Invalid session: " + sessionId);
            }

            if (okay && m_Deliverer != null)
            {
                m_Deliverer.AddClient(session.GetRemoteAddress(), session);
            }
            else
            {
                session.CloseConnection();
            }
        }