/// <summary>
 /// Default copy constructor
 /// </summary>
 /// <param name="b">the object to copy from</param>
 public IocpTcpServer(IocpTcpServer b)
     : base(b)
 {
     m_port      = b.m_port;
     m_serverOps = b.m_serverOps;
 }
        /// <summary>
        /// Accept callback function
        /// </summary>
        /// <param name="result">result</param>
        private static void onAccept(IAsyncResult result)
        {
            IocpTcpServer server = result.AsyncState as IocpTcpServer;
            TcpClient     client = null;

            try
            {
                if (server.m_listener != null)
                {
                    client = server.m_listener.EndAcceptTcpClient(result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                if (client != null)
                {
                    try
                    {
                        client.Client.Shutdown(SocketShutdown.Both);
                        //client.Client.Disconnect(true);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + " >" + e.StackTrace);
                    }
                    client.Close();
                    client = null;
                }
            }

            try
            {
                if (server.m_listener != null)
                {
                    server.m_listener.BeginAcceptTcpClient(new AsyncCallback(IocpTcpServer.onAccept), server);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                if (client != null)
                {
                    client.Close();
                }
                server.StopServer();
                return;
            }

            if (client != null)
            {
                IocpTcpSocket socket = new IocpTcpSocket(client, server);
                lock (server.m_listLock)
                {
                    if (server.MaxSocketCount != SocketCount.Infinite && server.m_socketList.Count > server.MaxSocketCount)
                    {
                        socket.Disconnect();
                        return;
                    }
                }
                if (server.CallBackObj == null)
                {
                    socket.Disconnect();
                    return;
                }

                if (!server.Acceptor.OnAccept(server, socket.IPInfo))
                {
                    socket.Disconnect();
                }
                else
                {
                    INetworkSocketCallback socketCallbackObj = server.Acceptor.GetSocketCallback();
                    socket.CallBackObj = socketCallbackObj;
                    socket.Start();
                    lock (server.m_listLock)
                    {
                        server.m_socketList.Add(socket);
                    }
                    server.OnServerAccepted(server, socket);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Default copy constructor
 /// </summary>
 /// <param name="b">the object to copy from</param>
 public IocpTcpServer(IocpTcpServer b)
     : base(b)
 {
     m_port = b.m_port;
     m_serverOps = b.m_serverOps;
 }
        /// <summary>
        /// Stop the server
        /// </summary>
        public void StopServer()
        {
            lock (m_generalLock)
            {
                if (!IsServerStarted)
                    return;

                m_listener.StopServer();
                m_listener = null;
            }
            ShutdownAllClient();

            if (CallBackObj != null)
                CallBackObj.OnServerStopped(this);
        }
        /// <summary>
        /// Start the server and start accepting the client
        /// </summary>
        protected override void execute()
        {
            StartStatus status = StartStatus.FAIL_SOCKET_ERROR;
            try
            {
                lock (m_generalLock)
                {
                    if (IsServerStarted)
                    {
                        status = StartStatus.FAIL_ALREADY_STARTED;
                        throw new CallbackException();
                    }

                    CallBackObj = m_serverOps.CallBackObj;
                    Port = m_serverOps.Port;
                    ReceiveType = m_serverOps.ReceiveType;

                    if (Port == null || Port.Length == 0)
                    {
                        Port = ServerConf.DEFAULT_PORT;
                    }
                    m_socketMap.Clear();
                    ServerOps listenerOps = new ServerOps(this, m_serverOps.Port,true);
                    m_listener.StartServer(listenerOps);
                }

            }
            catch (CallbackException)
            {
                CallBackObj.OnServerStarted(this, status);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                if (m_listener != null)
                    m_listener.StopServer();
                m_listener = null;
                CallBackObj.OnServerStarted(this, StartStatus.FAIL_SOCKET_ERROR);
                return;
            }
            CallBackObj.OnServerStarted(this, StartStatus.SUCCESS);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Stop the server
        /// </summary>
        public void StopServer()
        {
            lock (m_generalLock)
            {
                if (!IsServerStarted)
                    return;

                m_listener.StopServer();
                m_listener = null;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Start the server and start accepting the client
        /// </summary>
        protected override void execute()
        {
            StartStatus status = StartStatus.FAIL_SOCKET_ERROR;
            try
            {
                lock (m_generalLock)
                {
                    if (IsServerStarted)
                    {
                        status = StartStatus.FAIL_ALREADY_STARTED;
                        throw new CallbackException();
                    }
                    Acceptor = m_serverOps.Acceptor;
                    CallBackObj = m_serverOps.CallBackObj;
                    RoomCallBackObj = m_serverOps.RoomCallBackObj;
                    Port = m_serverOps.Port;
                    ReceiveType = m_serverOps.ReceiveType;
                    MaxSocketCount = m_serverOps.MaxSocketCount;
                    MaxStreamCountPerSocket = m_serverOps.MaxStreamCountPerSocket;

                    if (Port == null || Port.Length == 0)
                    {
                        Port = ServerConf.DEFAULT_PORT;
                    }
                    lock (m_listLock)
                    {
                        m_socketMap.Clear();
                    }
                    lock (m_roomLock)
                    {
                        m_roomMap.Clear();
                    }          

                    m_listener = new IocpTcpServer();
                    ServerOps listenerOps = new ServerOps(this, m_serverOps.Port, this,null, true, MaxSocketCount);
                    m_listener.StartServer(listenerOps);
                }

            }
            catch (CallbackException)
            {
                OnParallelServerStarted(this, status);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                if (m_listener != null)
                    m_listener.StopServer();
                m_listener = null;
                OnParallelServerStarted(this, StartStatus.FAIL_SOCKET_ERROR);
                return;
            }
        }