Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ipaddress"></param>
        /// <param name="port"></param>
        /// <param name="sendData"></param>
        /// <param name="messageID">if occur error, use this messageID as returndata</param>
        /// <param name="handlerReturnData"></param>
        public void SendMsg(string ipaddress, int port, string sendData, string messageID, HandlerReturnData handlerReturnData)
        {
            IPEndPoint ipe          = new IPEndPoint(IPAddress.Parse(ipaddress), port);
            Socket     clientSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            m_maxNumberAcceptedClients.WaitOne();
            AsyncClientToken asyncClientToken = m_readWritePool.PopOrNew();

            asyncClientToken.HandlerReturnData = handlerReturnData;
            asyncClientToken.Socket            = clientSocket;
            asyncClientToken.MessageID         = messageID;
            int sendcount = Encoding.UTF8.GetBytes(sendData, 0, sendData.Length, asyncClientToken.Buffer, 0);

            asyncClientToken.Datasize = sendcount;


            try
            {
                clientSocket.BeginConnect(ipe, new AsyncCallback(ConnectCallback), asyncClientToken);
            }
            catch (Exception ex)
            {
                CloseOneInstance(asyncClientToken);
                if (LogTool != null)
                {
                    LogTool.Log(ex.Message + ex.StackTrace);
                }
            }
        }
Пример #2
0
 private void StartReceive()
 {
     m_maxNumberReceiveClients.WaitOne();
     if (mainServiceSocket == null)
     {
         return;
     }
     try
     {
         SocketAsyncEventArgs e = m_readWritePool.Pop();
         if (e == null || e.UserToken == null)
         {
             e            = new SocketAsyncEventArgs();
             e.UserToken  = new T();
             e.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
         }
         e.RemoteEndPoint = host;
         e.SetBuffer(m_bufferManager.TakeBuffer(m_maxSize), 0, m_maxSize);
         bool willRaiseEvent = mainServiceSocket.ReceiveFromAsync(e);
         if (!willRaiseEvent)
         {
             ProcessReceive(e);
         }
         StartReceive();
     }
     catch (Exception ex)
     {
         IsRunning = false;
         LogTool.Log(ex.Message + ex.StackTrace);
     }
 }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ipaddress"></param>
        /// <param name="port"></param>
        /// <param name="sendData"></param>
        /// <param name="messageID">if occur error, use this messageID as returndata</param>
        /// <param name="handlerReturnData"></param>
        public void SendMsg(string ipaddress, int port, string sendData, string messageID, HandlerReturnData handlerReturnData)
        {
            m_maxNumberAcceptedClients.WaitOne();
            AsyncClientToken asyncClientToken = m_readWritePool.PopOrNew();

            asyncClientToken.HandlerReturnData = handlerReturnData;
            asyncClientToken.IP        = ipaddress;
            asyncClientToken.Port      = port;
            asyncClientToken.MessageID = messageID;
            int sendcount = Encoding.UTF8.GetBytes(sendData, 0, sendData.Length, asyncClientToken.Buffer, 0);

            asyncClientToken.Datasize = sendcount;

            try
            {
                IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(asyncClientToken.IP), asyncClientToken.Port);
                asyncClientToken.Socket.BeginSendTo(asyncClientToken.Buffer, 0, asyncClientToken.Datasize, SocketFlags.None, ipe, new AsyncCallback(SendCallback), asyncClientToken);
            }
            catch (Exception ex)
            {
                CloseOneInstance(asyncClientToken);
                if (LogTool != null)
                {
                    LogTool.Log(ex.Message + ex.StackTrace);
                }
            }
        }
Пример #4
0
 public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
 {
     _Logger.Log(LogLevel.Info, String.Format("Executed action named {0} for request {1} by {2} from {3}.",
                                              actionContext.ActionDescriptor.ActionName,
                                              actionContext.Request.RequestUri,
                                              "User",
                                              actionContext.Request.GetClientIp()));
 }
Пример #5
0
 public RisProContext(string connectionString, ICommonLog logger)
     : base(connectionString)
 {
     _logger = logger;
     _logger.Log(LogLevel.Debug, "RisProContext.ctor");
     Database.Log = log => Debug.WriteLine(log);
     this.Database.CommandTimeout = 0;
 }
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //Interlocked.Increment(ref m_numConnectedSockets);
            //Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
            //    m_numConnectedSockets);

            // Get the socket for the accepted client connection and put it into the
            //ReadEventArg object user token
            try
            {
                SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
                ((T)readEventArgs.UserToken).Socket = e.AcceptSocket;

                // As soon as the client is connected, post a receive to the connection
                readEventArgs.SetBuffer(0, m_maxSize);
                bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);
                if (!willRaiseEvent)
                {
                    ProcessReceive(readEventArgs);
                }
                // Accept the next connection request
                StartAccept(e);
            }
            catch (Exception ex)
            {
                LogTool.Log(ex.Message + ex.StackTrace);
                CloseClientSocket(e);
            }
        }
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            T token = (T)e.UserToken;

            try
            {
                if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
                {
                    LogTool.Log("Ip:" + ((IPEndPoint)e.RemoteEndPoint).Address + " port:" + ((IPEndPoint)e.RemoteEndPoint).Port);

                    string data       = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
                    string returndata = HandleRecivedMessage(data, token);
                    if (string.IsNullOrEmpty(returndata))
                    {
                        CloseClientSocket(e);
                        return;
                    }

                    int bytecount = Encoding.UTF8.GetBytes(returndata, 0, returndata.Length, e.Buffer, 0);

                    e.SetBuffer(0, bytecount);
                    bool willRaiseEvent = mainServiceSocket.SendToAsync(e);
                    if (!willRaiseEvent)
                    {
                        ProcessSend(e);
                    }
                }
                else
                {
                    HandleError(token);
                    CloseClientSocket(e);
                }
            }
            catch (Exception ex)
            {
                LogTool.Log(ex.Message + ex.StackTrace);
                HandleError(token);
                CloseClientSocket(e);
            }
        }
Пример #8
0
        public LicenseDataDto GetLicenseData()
        {
            try
            {
                var auditServer = ConfigurationManager.AppSettings["AuditServer"];
                var devID       = ConfigurationManager.AppSettings["DevID"];
                if (devID == "Test")
                {
                    return(new LicenseDataDto
                    {
                        IsSuccessed = true,
                        RisEnabled = true,
                        ConsultationEnabled = true,
                        MaxOnlineUserCount = 99999
                    });
                }
                using (var client = new ServiceSoapClient(new BasicHttpBinding(), new EndpointAddress(auditServer)))
                {
                    var result = client.GetRisLicense(devID);
                    if (result.isSuccessful)
                    {
                        var data                = new Cryptography("GCRIS2_Terrence").DeEncrypt(result.risData);
                        var maxOnlie            = data.Substring(4, 4);
                        var expiredDate         = data.Substring(8, 10);
                        var isExpired           = expiredDate != "1980-12-10" && DateTime.Now >= DateTime.Parse(data.Substring(8, 10));
                        var dataPanel           = data.Substring(18, 64);
                        var risEnabled          = dataPanel.Substring(61, 1);
                        var consultationEnabled = dataPanel.Substring(62, 1);
                        return(new LicenseDataDto
                        {
                            IsSuccessed = true,
                            IsExpired = isExpired,
                            MaxOnlineUserCount = int.Parse(maxOnlie),
                            ConsultationEnabled = consultationEnabled != "0",
                            RisEnabled = risEnabled != "0",
                        });
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.Log(LogLevel.Error, "connect to license server failed: \r\n" + exception);
            }

            return(new LicenseDataDto
            {
                IsSuccessed = false,
                ErrorMessage = "Get License failed."
            });
        }
Пример #9
0
 public new void SaveChanges()
 {
     try
     {
         base.SaveChanges();
     }
     catch (DbEntityValidationException e)
     {
         foreach (var eve in e.EntityValidationErrors)
         {
             var errorMessage = String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
             Debug.WriteLine(errorMessage);
             _logger.Log(LogLevel.Error, errorMessage);
             foreach (var ve in eve.ValidationErrors)
             {
                 var propertyMessage = String.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                 _logger.Log(LogLevel.Error, propertyMessage);
                 Debug.WriteLine(propertyMessage);
             }
         }
         throw;
     }
 }
Пример #10
0
 public override void Log(ExceptionLoggerContext context)
 {
     _Logger.Log(LogLevel.Error, context.ExceptionContext.Exception.ToString());
 }