Ejemplo n.º 1
0
        public override Task <SaveCoetLogResult> SaveLog(SaveCoetLogParm request, ServerCallContext context)
        {
            if (Common.isAllowHost(context.Peer))
            {
                List <CoetLogInfoEntity> logInfoList = new List <CoetLogInfoEntity>();
                foreach (var item in request.CoetLogInfos)
                {
                    logInfoList.Add(new CoetLogInfoEntity
                    {
                        Type     = item.Type,
                        JsonInfo = item.JsonInfo,
                        SendIP   = item.SendIP,
                        SendName = item.SendName
                    });
                }

                LogMethod lm           = new LogMethod();
                int       executeCount = lm.SaveLog(logInfoList);

                return(Task.FromResult(new SaveCoetLogResult {
                    ExecuteCount = executeCount
                }));
            }
            else
            {
                return(Task.FromResult(new SaveCoetLogResult {
                    ExecuteCount = 0
                }));
            }
        }
Ejemplo n.º 2
0
        public static void Start(string coetServerUrl)
        {
            try
            {
                Channel channel = new Channel(coetServerUrl, ChannelCredentials.Insecure);
                var     client  = new CoetLog.CoetLogClient(channel);

                if (_sendTimer == null)
                {
                    _sendTimer = new Timer(new TimerCallback(async d =>
                    {
                        if (!_isSending)
                        {
                            _isSending = true;

                            SaveCoetLogParm sp = new SaveCoetLogParm();

                            while (_sendLogQueue.Count > 0 && sp.CoetLogInfos.Count < _sendMaxCount)
                            {
                                CoetLogInfo ci = _sendLogQueue.Dequeue();

                                if (ci != null)
                                {
                                    sp.CoetLogInfos.Add(ci);
                                }
                            }

                            if (sp.CoetLogInfos.Count > 0)
                            {
                                int executeCount = 0;
                                try
                                {
                                    var reply    = await client.SaveLogAsync(sp);
                                    executeCount = reply.ExecuteCount;
                                    Console.WriteLine(executeCount);
                                }
                                catch (Exception)
                                {
                                    channel.ShutdownAsync().Wait();
                                }

                                if (executeCount < sp.CoetLogInfos.Count)
                                {
                                    foreach (var item in sp.CoetLogInfos)
                                    {
                                        lock (_sendLogQueue)
                                        {
                                            _sendLogQueue.Enqueue(item);
                                        }
                                    }
                                }
                            }

                            _isSending = false;
                        }
                    }), new object(), 0, _timerinterval);
                }
            }
            catch (Exception)
            {
            }
        }