Esempio n. 1
0
        private void CycleSend()
        {
            while (true)
            {
                if (PrimConnStat == PrimConnState.UnConnected)
                {
                    Thread.Sleep(10);
                    continue;
                }

                if (!_sendFlag)
                {
                    Thread.Sleep(2);
                    continue;
                }

                if (_sendQueue.Count < 1)
                {
                    Thread.Sleep(2);
                    continue;
                }

                ServerMsgInfo sendMsgInfo = null;

                lock (_sendQueueMutex)
                {
                    sendMsgInfo = _sendQueue.Dequeue();
                }

                if (sendMsgInfo == null)
                {
                    Thread.Sleep(2);
                    continue;
                }

                try
                {
                    if (_dicPointSocket.ContainsKey(sendMsgInfo.Point))
                    {
                        var sendSocket = _dicPointSocket[sendMsgInfo.Point];
                        var buffer     = Encoding.UTF8.GetBytes((string)sendMsgInfo.Context);
                        sendSocket.Send(buffer);
                    }
                }
                catch (Exception)
                {
                    PrimConnStat = PrimConnState.UnConnected;
                }

                Thread.Sleep(2);
            }
        }
Esempio n. 2
0
        public int SendInfoByPoint(string revPoint, object context)
        {
            var iRet = 0;

            lock (_sendQueueMutex)
            {
                var msg = new ServerMsgInfo();
                msg.Point   = revPoint;
                msg.Context = context;
                _sendQueue.Enqueue(msg);
            }

            return(iRet);
        }
Esempio n. 3
0
        public int SendInfoByNetName(string revNetName, object context)
        {
            var iRet = 0;

            lock (_sendQueueMutex)
            {
                var msg = new ServerMsgInfo();
                msg.NetName = revNetName;
                msg.Context = context;
                _sendQueue.Enqueue(msg);
            }

            return(iRet);
        }
Esempio n. 4
0
        private void CycleRev(object socket)
        {
            while (true)
            {
                var revSocket = socket as Socket;
                try
                {
                    //定义byte数组存放从客户端接收过来的数据
                    var buffer = new byte[1024];

                    //将接收过来的数据放到buffer中,并返回实际接受数据的长度
                    var n = revSocket.Receive(buffer);

                    //将字节转换成字符串
                    var words = Encoding.UTF8.GetString(buffer, 0, n);
                    var point = revSocket.RemoteEndPoint.ToString();

                    ServerMsgInfo msg = null;
                    if (_dicPointName.ContainsKey(point))
                    {
                        msg         = new ServerMsgInfo();
                        msg.Point   = point;
                        msg.NetName = _dicPointName[point];
                        msg.Context = words;
                    }
                    else if (_dicPointSocket.ContainsKey(point))
                    {
                        msg         = new ServerMsgInfo();
                        msg.Point   = point;
                        msg.Context = words;
                    }

                    if (msg == null)
                    {
                        continue;
                    }

                    if (PrimDebug)
                    {
                        ((PrimConfigControl)PrimConfigUI).ShowRevMsg(msg.Point + " - " + msg.NetName + ":" + msg.Context);
                    }

                    if (NotifyMode == SktNotifyMode.DataEvent && OnMsgReved != null)
                    {
                        OnMsgReved(msg.Point, msg.Context);
                    }

                    if (NotifyMode == SktNotifyMode.DataQueue)
                    {
                        lock (_revQueueMutex)
                        {
                            _revQueue.Enqueue(msg);
                        }
                    }
                }
                catch (Exception)
                {
                    //ShowMsg(ex.Message);
                    break;
                }

                Thread.Sleep(2);
            }
        }