Beispiel #1
0
 /// <summary>
 /// 设置基本配置
 /// </summary>
 /// <param name="numConnections">同时处理的最大连接数</param>
 /// <param name="receiveBufferSize">用于每个套接字I/O操作的缓冲区大小(接收端)</param>
 /// <param name="overtime">超时时长,单位秒.(每10秒检查一次),当值为0时,不设置超时</param>
 ///<param name = "port" > 端口 </ param >
 public Pull(int numConnections, int receiveBufferSize, int overtime, int port)
 {
     server            = new TcpPullServer(numConnections, receiveBufferSize, overtime);
     server.OnAccept  += Server_OnAccept;
     server.OnReceive += Server_OnReceive;
     server.OnSend    += Server_OnSend;
     server.OnClose   += Server_OnClose;
     server.Start(port);
 }
Beispiel #2
0
 /// <summary>
 /// 停止所有服务
 /// </summary>
 public void Stop()
 {
     if (DicTcpServer == null)
     {
         return;
     }
     foreach (KeyValuePair <string, TcpPullServer <ClientInfo> > kvp in DicTcpServer)
     {
         TcpPullServer <ClientInfo> sk = kvp.Value;
         sk.Destroy();
     }
 }
        static void Main(string[] args)
        {
            tcpPullServer            = new TcpPullServer(1000, 10240, 5);
            tcpPullServer.OnAccept  += Server_OnAccept;
            tcpPullServer.OnReceive += Server_OnReceive;
            tcpPullServer.OnSend    += Server_OnSend;
            tcpPullServer.OnClose   += Server_OnClose;
            tcpPullServer.Start(10899);
            udpServer = new UdpServer(10240);
            udpServer.Start(10899, true);
            udpServer.OnReceive += UdpServer_OnReceive;
            udpServer.OnSend    += UdpServer_OnSend;
            //Console.WriteLine("Hello World!");
            Console.Read();

            Console.WriteLine("Hello World!");
        }
Beispiel #4
0
        public bool Start(bool isStart, ref string errorMsg)
        {
            try
            {
                InitConfig(DbType);//初始化配置
                Obj      = ObjectReflection.CreateObject(_commProgramName.Substring(0, _commProgramName.IndexOf(".dll")));
                IResolve = Obj as IDataResolve;
                if (!Dic.ContainsKey(StrDeviceId))
                {
                    Dic.TryAdd(StrDeviceId, IResolve);
                }
                else
                {
                    Dic[StrDeviceId] = IResolve;
                }
                string param1 = string.Empty;
                IResolve.GetRules(StrDeviceId, DbType, ref param1);

                _server = new TcpPullServer <ClientInfo>();
                // 设置服务器事件
                _server.OnPrepareListen -= new TcpServerEvent.OnPrepareListenEventHandler(OnPrepareListen);
                _server.OnAccept        -= new TcpServerEvent.OnAcceptEventHandler(OnAccept);
                _server.OnSend          -= new TcpServerEvent.OnSendEventHandler(OnSend);
                _server.OnReceive       -= new TcpPullServerEvent.OnReceiveEventHandler(OnReceive);
                _server.OnClose         -= new TcpServerEvent.OnCloseEventHandler(OnClose);
                _server.OnShutdown      -= new TcpServerEvent.OnShutdownEventHandler(OnShutdown);

                _server.OnPrepareListen += new TcpServerEvent.OnPrepareListenEventHandler(OnPrepareListen);
                _server.OnAccept        += new TcpServerEvent.OnAcceptEventHandler(OnAccept);
                _server.OnSend          += new TcpServerEvent.OnSendEventHandler(OnSend);
                _server.OnReceive       += new TcpPullServerEvent.OnReceiveEventHandler(OnReceive);
                _server.OnClose         += new TcpServerEvent.OnCloseEventHandler(OnClose);
                _server.OnShutdown      += new TcpServerEvent.OnShutdownEventHandler(OnShutdown);
                _server.IpAddress        = _hostIp;
                _server.Port             = ushort.Parse(_port);
                if (isStart)
                {
                    // 启动服务
                    if (_server.Start())
                    {
                        InitServiceInfo(true);
                        //Log4Helper.WriteInfoLog(this.GetType(),
                        //    $@"设备id:{StrDeviceId}  设备名称:{_name}  $Server Start OK -> ({_hostIp}:{_port})");
                        if (!DicTcpServer.ContainsKey(StrDeviceId))
                        {
                            DicTcpServer.TryAdd(StrDeviceId, _server);
                        }
                        else
                        {
                            DicTcpServer[StrDeviceId] = _server;
                        }
                        return(true);
                    }
                    else
                    {
                        InitServiceInfo(false);
                        string msg = $@"加载【{_name}】失败!×原因:{
                                string.Format("$Server Start Error -> {0}({1}) Port:{2}", _server.ErrorMessage,
                                    _server.ErrorCode, _port)
                            }";
                        //EmailHelper.SendQQEmail(InitDeviceScheduler.发件人邮箱,
                        //    InitDeviceScheduler.收件人邮箱列表.Split(','),
                        //    InitDeviceScheduler.ServiceName, InitDeviceScheduler.ServiceName + $@"加载【{_name}】失败!",
                        //    $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:" + InitDeviceScheduler.ServiceName +
                        //    msg,
                        //    InitDeviceScheduler.发件人邮箱授权码);
                        errorMsg = msg;
                        return(false);
                    }
                }
                else
                {
                    InitServiceInfo(false);
                    return(false);
                }
            }
            catch (Exception e)
            {
                //Log4Helper.WriteErrorLog(this.GetType(), $@"设备id:{StrDeviceId}  设备名称:{_name}  " + e.ToString());
                errorMsg = e.ToString();
                return(false);
            }
            finally
            {
                //InitMonitorService.AutoResetEvent.Set();
            }
        }