Exemple #1
0
        public static IServer Create(SocketServerConfig config)
        {
            try
            {
                int    port = config.Port;
                string ip   = config.IPAddress;

                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                SocketKeepAliveHelper.SetKeepAliveValues(config, listener, config);
                if (ip == null || ip.Length < 1 || ip == "127.0.0.1")
                {
                    listener.Bind(new IPEndPoint(IPAddress.Any, port));
                }
                else
                {
                    listener.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
                }
                return(new SocketServer(listener, config));
            }
            catch (Exception err)
            {
                SocketLogMgt.SetLastError(err);
                return(null);
            }
        }
Exemple #2
0
        private SocketServer(Socket listener, SocketServerConfig config)
        {
            _listener = listener;
            _config   = config;

            _timer          = new Timer(_config.ConnectionCollectionInterval);
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
        }
Exemple #3
0
        public static IServer Create(int port)
        {
            try
            {
                SocketServerConfig cfg = new SocketServerConfig();
                cfg.Port = port;

                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                SocketKeepAliveHelper.SetKeepAliveValues(cfg, listener, cfg);
                listener.Bind(new IPEndPoint(IPAddress.Any, port));
                return(new SocketServer(listener, cfg));
            }
            catch (Exception err)
            {
                SocketLogMgt.SetLastError(err);
                return(null);
            }
        }
Exemple #4
0
 internal static void SetLog(Object sender, SocketServerConfig config)
 {
     if (config == null)
     {
         return;
     }
     SetLog(SocketLogType.Debug, sender, "---------------------------------");
     SetLog(SocketLogType.Debug, sender, "IP: " + config.IPAddress);
     SetLog(SocketLogType.Debug, sender, "Port: " + config.Port.ToString());
     SetLog(SocketLogType.Debug, sender, "BackLog: " + config.BackLog.ToString());
     SetLog(SocketLogType.Debug, sender, "SendTimeout: " + config.SendTimeout.ToString());
     SetLog(SocketLogType.Debug, sender, "ReceiveTimeout: " + config.ReceiveTimeout.ToString());
     SetLog(SocketLogType.Debug, sender, "ConnectionTimeoutSecond: " + config.ConnectionTimeoutSecond.ToString());
     SetLog(SocketLogType.Debug, sender, "ConnectionCollectionInterval: " + config.ConnectionCollectionInterval.ToString());
     SetLog(SocketLogType.Debug, sender, "EnableConnectionCollecting: " + config.EnableConnectionCollecting.ToString());
     SetLog(SocketLogType.Debug, sender, "SocketWorkerType: " + config.SocketWorkerType);
     SetLog(SocketLogType.Debug, sender, "CodePage: " + ((config.CodePageCode < 0) ? config.CodePageName : config.CodePageCode.ToString()));
     SetLog(SocketLogType.Debug, sender, "---------------------------------");
 }
Exemple #5
0
        public static IServer Create(string ip, int port)
        {
            if (ip == null || ip.Length < 1 || ip == "127.0.0.1")
            {
                return(Create(port));
            }
            try
            {
                SocketServerConfig cfg = new SocketServerConfig();
                cfg.IPAddress = ip;
                cfg.Port      = port;

                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                SocketKeepAliveHelper.SetKeepAliveValues(cfg, listener, cfg);
                listener.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
                return(new SocketServer(listener, cfg));
            }
            catch (Exception err)
            {
                SocketLogMgt.SetLastError(err);
                return(null);
            }
        }
Exemple #6
0
        /// <summary>
        /// 20130206
        /// To avoid the following exception when HL7GW is behind a NAT/Proxy/Firewall:
        /// System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine.
        /// https://groups.google.com/forum/?fromgroups#!topic/google-help-dataapi/5abZAyGUu3A
        /// http://code.google.com/p/google-gdata/wiki/KeepAliveAndUnderlyingConnectionIsClosed
        /// http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html#preventingdisconnection
        /// http://www.codeproject.com/Articles/117557/Set-Keep-Alive-Values
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="socket"></param>
        /// <param name="cfg"></param>
        public static void SetKeepAliveValues(object obj, Socket socket, SocketServerConfig cfg)
        {
            if (socket == null || cfg == null)
            {
                return;
            }

            int  res;
            uint time     = cfg.KeepAliveTime;
            uint interval = cfg.KeepAliveInterval;

            if (cfg.KeepAlive)
            {
                res = SocketKeepAliveHelper.SetKeepAliveValues(socket, true, time, interval);
                SocketLogMgt.SetLog(SocketLogType.Debug, obj,
                                    string.Format("SetSocketKeepAlive: Set {0}ms alive every {1}ms, result:{2}", time, interval, res));
            }
            else
            {
                res = SocketKeepAliveHelper.SetKeepAliveValues(socket, false, time, interval);
                SocketLogMgt.SetLog(SocketLogType.Debug, obj,
                                    string.Format("SetSocketKeepAlive: Disable, result:{0}", res));
            }
        }
Exemple #7
0
 internal static void SetLog(SocketServerConfig config)
 {
     SetLog(null, config);
 }