예제 #1
0
        /// <summary>
        /// 发送回调函数
        /// </summary>
        /// <param name="iar"></param>
        private void SendCallback(IAsyncResult iar)
        {
            SocketObject obj = null;

            try
            {
                obj = iar.AsyncState as SocketObject;
                if (iar.IsCompleted)
                {
                    int    sendLen = obj.ClientSocket.EndSend(iar);
                    string log     = string.Format("已成功转发比分数据:字节数:{3}->{0}->{1}:{2}", obj.MessObj.UserCode, obj.UserCode, obj.SendMessage, sendLen);
                    if (IsShowLog)
                    {
                        DoLog(log);
                    }
                }
            }
            catch (Exception e)
            {
                //socketObjList.TryTake(out obj);
                ListOperator(obj, ListAction.Delete);
                if (obj != null)
                {
                    CloseSocket(obj.ClientSocket);
                }
                DoLog("发送失败->" + e.Source + "->" + e.TargetSite + "->" + e.Message, true);
            }
        }
예제 #2
0
        /// <summary>
        /// 接入客户端回调
        /// </summary>
        /// <param name="iar"></param>
        private void AcceptCallback(IAsyncResult iar)
        {
            SocketObject obj    = new SocketObject();
            Socket       server = iar.AsyncState as Socket;
            Socket       client = null;

            if (iar.IsCompleted)
            {
                try
                {
                    client = server.EndAccept(iar);
                    StartHertCheck(client);//开启心跳检测
                    obj.ClientSocket   = client;
                    obj.BufferSize     = 300000;
                    obj.Buffer         = new byte[obj.BufferSize];
                    obj.RemoteEndpoint = client.RemoteEndPoint;

                    client.BeginReceive(obj.Buffer, 0, obj.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), obj);//接收消息
                }
                catch (Exception e)
                {
                    DoLog(e.Source + "->" + e.TargetSite + "->" + e.Message, true);
                    if (client != null)
                    {
                        CloseSocket(client);
                    }
                }

                AcceptSocket(server);
            }
        }
예제 #3
0
        /// <summary>
        /// 比分数据具体操作
        /// </summary>
        /// <param name="message"></param>
        /// <param name="messObj"></param>
        /// <param name="obj"></param>
        private void TalkOperation(string message, SocketMessage messObj, SocketObject obj)
        {
            if (IsShowLog)
            {
                DoLog("当前客户数:" + socketObjList.Count + "->接收比分数据:" + message);
            }
            string trasmitJsonStr = JsonConvert.SerializeObject(JsonConvert.DeserializeObject <TransmitMessage>(message));

            socketObjList.ToList().AsParallel().ForAll((item) =>
            {
                string messLoopId = string.Empty;
                try
                {
                    messLoopId = messObj.Data.DetailList[0].LoopId;
                }
                catch (Exception e)
                {
                    DoLog("获取对阵ID->" + e.Source + "->" + e.TargetSite + "->" + e.Message + "->data:" + message, true);
                }
                if (item.LoopID == messLoopId || item.JudgeCode == messObj.UserCode)
                {
                    string message1  = trasmitJsonStr;
                    byte[] dataBytes = Encoding.UTF8.GetBytes(message1);
                    item.SendMessage = message1;
                    item.MessObj     = messObj;
                    try
                    {
                        if (IsSocketConnected(item.ClientSocket))//发送前检测SOCKET通断
                        {
                            if (IsShowLog)
                            {
                                DoLog("开始向" + item.ClientSocket.RemoteEndPoint + "【" + item.UserCode + "】发送数据");
                            }
                            item.ClientSocket.BeginSend(dataBytes, 0, dataBytes.Length, SocketFlags.None, new AsyncCallback(SendCallback), item);
                        }
                        else
                        {
                            //socketObjList.TryTake(out item);
                            ListOperator(item, ListAction.Delete);
                            CloseSocket(item.ClientSocket);
                        }
                    }
                    catch (Exception e)
                    {
                        DoLog(item.ClientSocket.RemoteEndPoint + "【" + item.UserCode + "】SOCKET链接未活动");
                    }
                }
            });
        }
예제 #4
0
        /// <summary>
        /// SOCKET集合同步操作
        /// </summary>
        /// <param name="item"></param>
        /// <param name="action"></param>
        private void ListOperator(SocketObject item, ListAction action)
        {
            lock (obj)
            {
                switch (action)
                {
                case ListAction.Add:
                    socketObjList.Add(item);
                    break;

                case ListAction.Delete:
                    socketObjList.Remove(item);
                    break;
                }
            }
        }
예제 #5
0
        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="message"></param>
        /// <param name="messObj"></param>
        /// <param name="obj"></param>
        private void AnalyzeMessage(string message, SocketMessage messObj, SocketObject obj)
        {
            switch (messObj.ServerAction)
            {
            case "Login":    //移动端直播登录
                LoginOperation(messObj, obj);
                break;

            case "Talk":    //直播比分数据传入
                TalkOperation(message, messObj, obj);
                break;

            case "HeartBeat":    //心跳维持
                break;
            }
        }
예제 #6
0
        /// <summary>
        /// 接收消息回调
        /// </summary>
        /// <param name="iar"></param>
        private void ReceiveCallback(IAsyncResult iar)
        {
            SocketObject obj     = iar.AsyncState as SocketObject;
            Socket       client  = obj.ClientSocket;
            int          dataLen = 0;

            if (iar.IsCompleted)
            {
                try
                {
                    dataLen = client.EndReceive(iar);
                }
                catch (Exception e)
                {
                    CloseSocket(obj.ClientSocket);
                    DoLog(e.Source + "->" + e.TargetSite + "->" + e.Message, true);
                }
                string dataStr = string.Empty;
                if (dataLen > 0)
                {
                    try
                    {
                        dataStr = Encoding.UTF8.GetString(obj.Buffer, 0, dataLen).Trim();
                        dataStr = Regex.Replace(dataStr, "\\s", "");
                        SocketMessage messObj = JsonConvert.DeserializeObject <SocketMessage>(dataStr);
                        AnalyzeMessage(dataStr, messObj, obj);
                    }
                    catch (Exception e)
                    {
                        DoLog("数据接收处错误:" + e.Source + "->" + e.TargetSite + "->" + e.Message + "->" + dataStr, true);
                    }
                }
            }

            try
            {
                client.BeginReceive(obj.Buffer, 0, obj.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), obj);
            }
            catch (Exception e)
            {
                DoLog(e.Source + "->" + e.TargetSite + "->" + e.Message, true);
                CloseSocket(client);
            }
        }
예제 #7
0
        /// <summary>
        /// 登录具体操作
        /// </summary>
        /// <param name="messObj"></param>
        /// <param name="obj"></param>
        private void LoginOperation(SocketMessage messObj, SocketObject obj)
        {
            //查询所选对阵赛事对应的裁判登录账号
            if (!loopJudgeDic.ContainsKey(messObj.LoopId))
            {
                string judgeCode = DBHelper.GetLoopJudge(messObj.LoopId);
                loopJudgeDic.TryAdd(messObj.LoopId, judgeCode);
            }

            SocketObject item = null;

            item = socketObjList.ToList().Where(e => e.RemoteEndpoint.ToString().Equals(obj.RemoteEndpoint.ToString())).FirstOrDefault();
            if (item == null)
            {
                obj.UserCode  = messObj.UserCode;
                obj.LoopID    = messObj.LoopId;
                obj.JudgeCode = loopJudgeDic[messObj.LoopId];
                socketObjList.Add(obj);
            }
            else
            {
                item.UserCode  = messObj.UserCode;
                item.LoopID    = messObj.LoopId;
                item.JudgeCode = loopJudgeDic[messObj.LoopId];
            }
            DoLog(obj.UserCode + "【" + obj.RemoteEndpoint + "】连接成功,赛事对阵ID:" + obj.LoopID);


            //登录成功返回ServerMessage为"1"
            //SocketMessage sm = new SocketMessage { ServerMessage = "1" };
            //string smJson = JsonConvert.SerializeObject(sm);
            //try
            //{
            //    obj.ClientSocket.Send(Encoding.UTF8.GetBytes(smJson));
            //}
            //catch(Exception e)
            //{
            //    DoLog(e.Message);
            //    CloseSocket(obj.ClientSocket);
            //    socketObjList.TryTake(out obj);
            //}
        }