Exemplo n.º 1
0
        public bool Start(ServerConfig serverConfig)
        {
            IPAddress addr;
            IPAddress.TryParse(m_Config.IP, out addr);
            if (addr == null)
                addr = IPAddress.Any;
            m_Config.EndPoint = new IPEndPoint(addr, m_Config.Port);

            m_ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                SocketAsyncEventArgs socketEventArg;

                var socketArgsList = new List<SocketAsyncEventArgs>(serverConfig.MaxAcceptOps);

                for (int i = 0; i < serverConfig.MaxAcceptOps; i++)
                {
                    // Accept하는 SocketAsyncEventArgs는 버퍼가 없어야 한다
                    socketEventArg = new SocketAsyncEventArgs();
                    socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(CompletedAccept);
                    socketArgsList.Add(socketEventArg);
                }
                Server.m_AcceptSAEAPool = new ConcurrentStack<SocketAsyncEventArgs>(socketArgsList);

                m_ListenSocket.Bind(m_Config.EndPoint);
                m_ListenSocket.Listen(m_Config.BackLog);

                m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

                return StartAccept();
            }
            catch (Exception e)
            {
                OnError(e);
                return false;
            }
        }
Exemplo n.º 2
0
 public Listener(ServerConfig.ListenerConfig listenerConfig, Server server)
 {
     m_Config = listenerConfig;
     Server = server;
 }
Exemplo n.º 3
0
 public virtual bool LoadServerConfig()
 {
     try
     {
         using (StreamReader sr = File.OpenText(System.IO.Directory.GetCurrentDirectory() + @"\Config\Server.config"))
         {
             XmlSerializer serializer = new XmlSerializer(typeof(ServerConfig));
             m_ServerConfig = serializer.Deserialize(sr) as ServerConfig;
             return true;
         }
     }
     catch(Exception e)
     {
         Logger.Error(e);
         return false;
     }
 }