Пример #1
0
        public void SendEncryptUrl(string url, SockClientTcpSendCallback method)
        {
            url = EncryptSym.AESEncrypt(url);
            byte[] buffer = Encoding.UTF8.GetBytes(url);

            this.Send(buffer, method);
        }
Пример #2
0
        // Private Tools ===========================================================================

        public void SessCloseRequest(string sessid, SockClientTcpSendCallback method = null)
        {
            object req = new {
                id     = "service.sessclose",
                sessid = sessid,
            };

            tcp.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req)), method);
        }
Пример #3
0
        public void Send(byte[] buffer, SockClientTcpSendCallback method)
        {
            try {
                if (!IsSocketConnected(sock)) {
                    if (sock != null) sock.Close();
                    sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    sock.Connect(toep);
                    sock.SendTimeout = 1000; // important
                }
            } catch (Exception ex) {
                log4net.ILog log = log4net.LogManager.GetLogger(typeof(SockClientTcp));
                log.Error(String.Format("Connect to {0} failed.", toep.ToString()), ex);
            }

            if (method == null) {
                try {
                    sock.Send(buffer);
                } catch (Exception) { }
            } else {
                ThreadPool.QueueUserWorkItem((s) =>
                {
                    if (disposing) return;
                    string retval = "";
                    try {
                        mutex.WaitOne();
                        byte[] tmp = new byte[1024];
                        if (sock.Poll(0, SelectMode.SelectRead)) {
                            Thread.Sleep(200);
                            sock.Receive(tmp);
                        }
                        sock.Send(buffer);
                        if (sock.Poll(3000, SelectMode.SelectRead)) {
                            Thread.Sleep(200);
                            int nread = sock.Receive(tmp);
                            if (nread > 0)
                                retval = Encoding.UTF8.GetString(tmp.Take(nread).ToArray());
                        }
                    } catch (Exception ex) {
                        log4net.ILog log = log4net.LogManager.GetLogger(typeof(SockClientTcp));
                        log.Error("Exception in critical section.", ex);
                        return;
                    } finally {
                        mutex.ReleaseMutex();
                    }

                    try {
                        string[] str = retval.Replace("\r\n", "\n")
                            .Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        method.Method.Invoke(method.Target, new object[] { str.Last() });
                    } catch (Exception ex) {
                        log4net.ILog log = log4net.LogManager.GetLogger(typeof(SockClientTcp));
                        log.Error("Exception of invoking anonymous method.", ex);
                    }
                });
            }
        }
Пример #4
0
        protected void SendClientMsg(string ip, int port, string msg, SockClientTcpSendCallback method = null)
        {
            string url = "/center/clientsend" + "?ip=" + ip + "&port=" + port + "&data=" + msg;

            try {
                tcp.SendEncryptUrl(url, method);
            } catch (Exception) {
                //log4net.ILog log = log4net.LogManager.GetLogger(typeof(EnvModule));
                //log.Error("Exception of sending msg to client.", ex);
            }
        }
Пример #5
0
        public void Send(byte[] buffer, SockClientTcpSendCallback method)
        {
            try {
                if (!IsSocketConnected(sock))
                {
                    if (sock != null)
                    {
                        sock.Close();
                    }
                    sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    sock.Connect(toep);
                    sock.SendTimeout = 1000; // important
                }
            } catch (Exception ex) {
                log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
                .Error(String.Format("Connect to {0} failed.", toep.ToString()), ex);
            }

            if (method == null)
            {
                try {
                    sock.Send(buffer);
                } catch (Exception) { }
            }
            else
            {
                ThreadPool.QueueUserWorkItem((s) =>
                {
                    if (disposing)
                    {
                        return;
                    }
                    string retval = "";
                    try {
                        mutex.WaitOne();
                        byte[] tmp = new byte[1024];
                        if (sock.Poll(0, SelectMode.SelectRead))
                        {
                            Thread.Sleep(200);
                            sock.Receive(tmp);
                        }
                        sock.Send(buffer);
                        if (sock.Poll(3000, SelectMode.SelectRead))
                        {
                            Thread.Sleep(200);
                            int nread = sock.Receive(tmp);
                            if (nread > 0)
                            {
                                retval = Encoding.UTF8.GetString(tmp.Take(nread).ToArray());
                            }
                        }
                    } catch (Exception ex) {
                        log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
                        .Error("Exception in critical section.", ex);
                        return;
                    } finally {
                        mutex.ReleaseMutex();
                    }

                    try {
                        string[] str = retval.Replace("\r\n", "\n")
                                       .Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        method.Method.Invoke(method.Target, new object[] { str.Last() });
                    } catch (Exception ex) {
                        log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
                        .Error("Exception of invoking anonymous method.", ex);
                    }
                });
            }
        }
Пример #6
0
 public void SessDetailRequest(SockClientTcpSendCallback method = null)
 {
     tcp.Send(Encoding.UTF8.GetBytes("{'id':'service.sessdetail'}"), method);
 }
Пример #7
0
        public void SendEncryptUrl(string url, SockClientTcpSendCallback method)
        {
            url = EncryptSym.AESEncrypt(url);
            byte[] buffer = Encoding.UTF8.GetBytes(url);
            SockRequest.InsertHeader(SockRequestContentMode.url, ref buffer);

            this.Send(buffer, method);
        }