Ejemplo n.º 1
0
        /// <summary>
        /// 事件方法-消息显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tcpClient_MsgSendHandler(object sender, MessageEventArgs e)
        {
            CommuTcpClient client = sender as CommuTcpClient;

            Log.MessageInfo($"{client.Identity}-{client.Nodeid}-Send:{e.Msg}");
            MsgHandler_Show?.Invoke(sender, e);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProLogin(CommuTcpClient client, byte[] buffer)
        {
            Login login = new Login(buffer);

            OnRaiseMsgRecvEvent(client, login.ToString());
            LoginResp loginResp = new LoginResp(login.Header.Sequence_Id);
            //验证登陆
            ResultMsg msg      = LoginValidate(login.Body, out int nodeId, out decimal pDian, out decimal vDian);
            var       chatRate = pXinDb.GetTchatRate(0, nodeId, 3);//查询自己的倍率

            if (!msg.Success)
            {
                //验证失败
                loginResp.Body.Status    = 0;
                loginResp.Body.LoginDesc = msg.Message;
            }
            else
            {
                //登陆成功
                loginResp.Body.Status       = 1;
                loginResp.Body.LoginDesc    = "登录成功";
                loginResp.Body.VDianBalance = vDian;
                loginResp.Body.PDianBalance = pDian;
                loginResp.Body.Rate         = chatRate.Id == 0 ? 1 : chatRate.Rate;
                client.Nodeid = nodeId;
                string[] identitys = CommuClient.Values.Where(c => c.Nodeid == nodeId && c.Identity != client.Identity).Select(c => c.Identity).ToArray();
                foreach (var item in identitys)
                {
                    CommuClient[item].Close("抛弃旧连接");
                }
            }
            client.SendData(loginResp.ToBytes());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 链路测试
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProActive(CommuTcpClient client, byte[] buffer)
        {
            Active active = new Active(buffer);
            //OnRaiseMsgRecvEvent(client, active.ToString());
            ActiveResp activeResp = new ActiveResp(active.Header.Sequence_Id);

            client.SendData(activeResp.ToBytes());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 事件方法-接收客户端消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tcpClient_MsgRecvHandler(object sender, MessageEventArgs e)
        {
            //读取数据
            CommuTcpClient client = sender as CommuTcpClient;
            MessageHeader  header = new MessageHeader(e.Buffer);

            switch (header.Command_Id)
            {
            case PXin_COMMAND_TYPE.Login:
                ProLogin(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.LoginResp:
                break;

            case PXin_COMMAND_TYPE.Logout:
                ProLogout(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.LogoutResp:
                ProWt_LogoutResp(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.Active:
                ProActive(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.ActiveResp:
                ProActiveResp(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.ChatFee:
                ProChatFee(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.ChatFeePushResp:
                ProChatFeePushResp(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.ChatFeeRateSet:
                ProChatFeeRateSet(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.ChatFeeRateQuery:
                ProChatFeeRateQuery(client, e.Buffer);
                break;

            case PXin_COMMAND_TYPE.ChatFeeRateSetPushResp:
                ProChatFeeRateSetPushResp(client, e.Buffer);
                break;

            default:
                Log.MessageInfo("非法包,Msg=" + header.ToString());
                break;
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 事件方法-断开客户端连接
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void tcpClient_ConnectHandler(object sender, CommuEventArgs e)
 {
     if (e.State == 1)
     {
         //断开连接
         CommuTcpClient commuClient = sender as CommuTcpClient;
         RemoveCommuClient(commuClient.Identity, e.Reason);
     }
     ConnectHandler_Show?.Invoke(sender, e);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 客户端主动断开连接(登出)请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProLogout(CommuTcpClient client, byte[] buffer)
        {
            Wt_Logout logout = new Wt_Logout(buffer);

            OnRaiseMsgRecvEvent(client, logout.ToString());
            LogoutResp logoutResp = new LogoutResp(logout.Header.Sequence_Id);

            client.SendData(logoutResp.ToBytes());
            //关闭连接
            client.Close("客户端主动发起断开连接");
        }
Ejemplo n.º 7
0
        private void RateSetPush(PushRate pushRate)
        {
            CommuTcpClient receiveClient = CommuClient.Values.FirstOrDefault(c => c.Nodeid == pushRate.NodeId);

            if (receiveClient != null && pushRate != null)
            {
                //给聊天中的用户推送倍率
                ChatFeeRateSetPush chatFeeRateSetPush = new ChatFeeRateSetPush();
                chatFeeRateSetPush.Body.Rate    = pushRate.Rate;
                chatFeeRateSetPush.Body.SNodeId = pushRate.SNodeId;
                receiveClient.SendData(chatFeeRateSetPush.ToBytes());
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 聊天计费
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProChatFee(CommuTcpClient client, byte[] buffer)
        {
            ChatFee chatFee = new ChatFee(buffer);

            OnRaiseMsgRecvEvent(client, chatFee.ToString());
            ChatFeeResp chatFeeResp = new ChatFeeResp(chatFee.Header.Sequence_Id);
            //处理计费业务
            List <PushPDian> pushPList = new List <PushPDian>();
            var result     = (true, "");
            var key        = client.Nodeid.ToString() + chatFee.Header.Sequence_Id;
            var chatFeeHis = pXinDb.GetChatFeeHisBySeq(client.Nodeid.ToString() + chatFee.Header.Sequence_Id);

            if (chatFeeHis.Hisid == 0)//判断是否是丢包情况重发
            {
                if (!string.IsNullOrEmpty(chatFee.Body.Receiver))
                {
                    result = HandChatFeeBusiness(chatFee, client.Nodeid, pushPList);
                }
                else
                {
                    result = (false, "无效参数:Receiver");
                }
            }
            var tpxinUserinfo = pXinDb.GetUserInfoByNodeId(client.Nodeid);

            chatFeeResp.Body.FeeType = chatFee.Body.FeeType;
            chatFeeResp.Body.Status  = result.Item1 == true?Convert.ToUInt32(1) : Convert.ToUInt32(0);

            chatFeeResp.Body.StatusDesc   = result.Item1 == true ? "成功" : result.Item2;
            chatFeeResp.Body.VDianBalance = tpxinUserinfo.V;
            chatFeeResp.Body.PDianBalance = tpxinUserinfo.P;
            client.SendData(chatFeeResp.ToBytes());
            if (result.Item1)
            {
                if (chatFee.Body.ReceiveType == 1)//接收者类型为用户
                {
                    int receiverid = 0;
                    Int32.TryParse(chatFee.Body.Receiver, out receiverid);
                    var pushPDian = pushPList.FirstOrDefault(p => p.NodeId == receiverid);
                    PushReceive(pushPDian);
                }
                else if (chatFee.Body.ReceiveType == 2 || chatFee.Body.ReceiveType == 3) //接收者类型为群时
                {
                    foreach (var item in pushPList)                                      //遍历群成员进行推送
                    {
                        PushReceive(item);
                    }
                }
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 给接收者推送
 /// </summary>
 /// <param name="nodeId"></param>
 /// <param name="pushPDian"></param>
 private void PushReceive(PushPDian pushPDian)
 {
     if (pushPDian != null)
     {
         CommuTcpClient receiveClient = CommuClient.Values.FirstOrDefault(c => c.Nodeid == pushPDian.NodeId);
         if (receiveClient != null)
         {
             //给接收用户推送P点余额
             ChatFeePush chatFeePush = new ChatFeePush();
             chatFeePush.Body.PDianPush    = pushPDian.PDianPush;
             chatFeePush.Body.PDianBalance = pushPDian.PDianBalance;
             receiveClient.SendData(chatFeePush.ToBytes());
         }
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// 新增客户端连接
        /// </summary>
        /// <param name="socket">客户端Socket</param>
        private CommuTcpClient AddCommuClient(Socket socket)
        {
            lock (commuClientSyncObj)
            {
                CommuTcpClient tcpClient = new CommuTcpClient(socket);
                if (CommuClient.ContainsKey(tcpClient.Identity))
                {
                    CommuClient[tcpClient.Identity].Close("客户端用同一个端口连接");
                }
                Log.RunInfo("新增Socket:" + tcpClient.Identity);
                tcpClient.MsgSendEventHandler += new MessageEventHander(tcpClient_MsgSendHandler);
                tcpClient.ConnectEventHandler += new CommuEventHander(tcpClient_ConnectHandler);
                tcpClient.MsgRecvEventHandler += new MessageEventHander(tcpClient_MsgRecvHandler);

                CommuClient.Add(tcpClient.Identity, tcpClient);
                //CommuClient[tcpClient.Identity] = tcpClient;
                tcpClient.Start();
                return(tcpClient);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 聊天计费倍率查询
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProChatFeeRateQuery(CommuTcpClient client, byte[] buffer)
        {
            ChatFeeRateQuery chatFeeRateQuery = new ChatFeeRateQuery(buffer);

            OnRaiseMsgRecvEvent(client, chatFeeRateQuery.ToString());
            var tchatReceiverRate = pXinDb.GetTchatRate(chatFeeRateQuery.Body.Sender, chatFeeRateQuery.Body.Receiver, chatFeeRateQuery.Body.Type);
            var tchatSenderRate   = new TchatRate();

            if (chatFeeRateQuery.Body.Type == 1)
            {
                if (tchatReceiverRate.Id == 0)//没有设置私聊倍率
                {
                    tchatReceiverRate = pXinDb.GetTchatRate(0, chatFeeRateQuery.Body.Receiver, 3);
                }
                tchatSenderRate = pXinDb.GetTchatRate(chatFeeRateQuery.Body.Receiver, chatFeeRateQuery.Body.Sender, chatFeeRateQuery.Body.Type);
                if (tchatSenderRate.Id == 0)//没有设置私聊倍率
                {
                    tchatSenderRate = pXinDb.GetTchatRate(0, chatFeeRateQuery.Body.Sender, 3);
                }
                var pxinUser = pXinDb.GetReginfoByNodeid(chatFeeRateQuery.Body.Receiver);
                tchatSenderRate.Rate   = pxinUser.Isenterprise == 3 ? 1 : tchatSenderRate.Rate;   //当用户是达人时
                pxinUser               = pXinDb.GetReginfoByNodeid(chatFeeRateQuery.Body.Sender);
                tchatReceiverRate.Rate = pxinUser.Isenterprise == 3 ? 1 : tchatReceiverRate.Rate; //当对方用户是达人时
            }
            else
            {
                tchatSenderRate.Rate = tchatReceiverRate.Rate;
                tchatSenderRate.Id   = tchatReceiverRate.Id;
            }
            ChatFeeRateQueryResp chatFeeRateQueryResp = new ChatFeeRateQueryResp(chatFeeRateQuery.Header.Sequence_Id);

            chatFeeRateQueryResp.Body.Type         = chatFeeRateQuery.Body.Type;
            chatFeeRateQueryResp.Body.Sender       = chatFeeRateQuery.Body.Type == 1 ? chatFeeRateQuery.Body.Sender : chatFeeRateQuery.Body.Receiver;
            chatFeeRateQueryResp.Body.ReceiverRate = tchatReceiverRate.Id == 0 ? 1 : tchatReceiverRate.Rate;
            chatFeeRateQueryResp.Body.SenderRate   = tchatSenderRate.Id == 0 ? 1 : tchatSenderRate.Rate;
            var userInfo = pXinDb.GetUserInfoByNodeId(client.Nodeid);

            chatFeeRateQueryResp.Body.VDianBalance = userInfo.V;
            chatFeeRateQueryResp.Body.PDianBalance = userInfo.P;
            client.SendData(chatFeeRateQueryResp.ToBytes());
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 聊天计费倍率设置
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProChatFeeRateSet(CommuTcpClient client, byte[] buffer)
        {
            ChatFeeRateSet chatFeeRateSet = new ChatFeeRateSet(buffer);

            OnRaiseMsgRecvEvent(client, chatFeeRateSet.ToString());
            TchatRate tchatRate = new TchatRate();

            tchatRate.Typeid     = chatFeeRateSet.Body.Type;
            tchatRate.Sender     = chatFeeRateSet.Body.Sender;//0;
            tchatRate.Receiver   = chatFeeRateSet.Body.Receiver;
            tchatRate.Rate       = chatFeeRateSet.Body.Rate;
            tchatRate.Createtime = DateTime.Now;
            tchatRate.Remarks    = "聊天计费倍率设置";
            var flag   = true;
            var result = pXinDb.GetTchatRate(tchatRate.Sender, tchatRate.Receiver, tchatRate.Typeid);

            if (result.Id == 0)
            {
                if (tchatRate.Rate > 1)
                {
                    flag = pXinDb.ChatFeeRateSet(tchatRate);
                }
            }
            else
            {
                flag = pXinDb.UpdateChatFeeRate(tchatRate);
            }
            tchatRate.Rate = flag == false ? result.Rate : tchatRate.Rate;
            ChatFeeRateSetResp chatFeeRateSetResp = new ChatFeeRateSetResp(chatFeeRateSet.Header.Sequence_Id);

            chatFeeRateSetResp.Body.Status = flag == true ? (uint)1 : 0;
            chatFeeRateSetResp.Body.Rate   = tchatRate.Rate;
            client.SendData(chatFeeRateSetResp.ToBytes());
            //推送
            RateSetPush(client.Nodeid, chatFeeRateSet, tchatRate);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 聊天计费倍率设置推送响应
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProChatFeeRateSetPushResp(CommuTcpClient client, byte[] buffer)
        {
            ChatFeeRateSetPushResp chatFeeRateSetPushResp = new ChatFeeRateSetPushResp(buffer);

            OnRaiseMsgRecvEvent(client, chatFeeRateSetPushResp.ToString());
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 客户端主动断开连接(登出)响应
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProWt_LogoutResp(CommuTcpClient client, byte[] buffer)
        {
            LogoutResp logout = new LogoutResp(buffer);

            OnRaiseMsgRecvEvent(client, logout.ToString());
        }
Ejemplo n.º 15
0
 /// <summary>
 /// 链路测试响应
 /// </summary>
 /// <param name="client"></param>
 /// <param name="buffer"></param>
 private void ProActiveResp(CommuTcpClient client, byte[] buffer)
 {
     ActiveResp activeResp = new ActiveResp(buffer);
     //OnRaiseMsgRecvEvent(client, activeResp.ToString());
 }
Ejemplo n.º 16
0
 private void OnRaiseMsgRecvEvent(CommuTcpClient client, string msg)
 {
     Log.MessageInfo($"{client.Identity}-{client.Nodeid}-Recv:{msg}");
     MsgHandler_Show?.Invoke(client, new MessageEventArgs(client.Identity, string.Empty, msg));
 }