Exemplo n.º 1
0
        protected void AcceptCallBack(IAsyncResult ar)
        {
            Socket client = Listener.EndAccept(ar);
            int    index  = ClientPool.Get();

            if (index >= 0)
            {
                ClientPool[index].SetActive();
                Connection con = ClientPool[index];
                con.clientId = index;
                StepMessage  = new byte[ClientPool.Count][];
                //for (int i = 0; i < StepMessage.Length; i++)
                //{
                //    StepMessage[i] = new byte[framSize];
                //}
                con.socket = client;
                SendIninInfo((byte)con.clientId);
                if (FrameList.Count > 0)
                {
                    SendToClientAllFrame(index);
                }
                con.socket.BeginReceive(con.readBuff, 0, Connection.buffer_size, SocketFlags.None, ReceiveCallBack, con);
            }
            else
            {
                ServerLog.LogServer("服务器人数达到上限", 0);
            }
            Listener.BeginAccept(AcceptCallBack, Listener);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        public void Start()
        {
            Listener.Bind(EndPoint);
            Listener.Listen(MaxQueueLength);

            Listener.BeginAccept(new AsyncCallback(_AcceptCallback), null);
        }
Exemplo n.º 3
0
        public void StartServer(string host, int port, int maxServerCount)
        {
            this.port        = port.ToString();
            this.ip          = host;
            _serverListener  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _clientPool      = new IndexObjectPool <Connection>(maxServerCount);
            _frameList       = new List <byte[]>();
            Listener.NoDelay = true;
            timer            = new Timer(100);
            timer.AutoReset  = true;
            timer.Elapsed   += SendStepAll;
            timer.Enabled    = true;
            Listener.Bind(new IPEndPoint(IPAddress.Parse(host), port));
            Listener.Listen(maxServerCount);
            Listener.BeginAccept(AcceptCallBack, Listener);
            _stepMessage = new byte[maxServerCount][];
            // framSize = 0;
            //for (int i = 0; i < _stepMessage.Length; i++)
            //{
            //    _stepMessage[i] = new byte[framSize];
            //}
            //ClientPool.Clear();

            ServerLog.LogServer("服务器启动成功", 0);
        }
Exemplo n.º 4
0
        private void acceptCallback(IAsyncResult result)
        {
            if (Listener == null)
            {
                return;
            }

            try
            {
                var socket     = Listener.EndAccept(result);             // Finish accepting the incoming connection.
                var connection = new Connection(this, socket);           // Add the new connection to the dictionary.

                lock (ConnectionLock) Connections.Add(connection);       // add the connection to list.

                OnClientConnection(new ConnectionEventArgs(connection)); // Raise the OnConnect event.

                connection.BeginReceive(readCallback, connection);       // Begin receiving on the new connection connection.
                Console.WriteLine("new connection: " + socket.RemoteEndPoint);
            }
            catch (NullReferenceException) { } // we recive this after issuing server-shutdown, just ignore it.
            catch (SocketException) { }        //We recieve this if client disconnects too quickly, ignore it
            catch (Exception e)
            {
                Console.WriteLine(string.Format("{0} {1}", e, "AcceptCallback"));
            }

            Listener.BeginAccept(acceptCallback, null); // Continue receiving other incoming connection asynchronously.
        }
Exemplo n.º 5
0
        public override void Start()
        {
            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                // Set the event to nonsignaled state.
                allDone.Reset();
                StateObject state = new StateObject
                {
                    workSocket = this
                };
                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for a connection...");
                Listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    state);

                // Wait until a connection is made before continuing.
                allDone.WaitOne();
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 6
0
        private void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            allDone.Set();


            var listener = (StateObject)ar.AsyncState;

            WorkSockets.Add(("Store", ((SocketServer)listener.workSocket).Listener.EndAccept(ar)));

            // Create the state object.
            StateObject state = new StateObject
            {
                SocketId   = SocketIds.LastOrDefault(),
                workSocket = this
            };


            WorkSockets[SocketIds.LastOrDefault()].WorkSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                                                           new AsyncCallback(ReadCallback), state);
            SocketIds.Add(SocketIds.LastOrDefault() + 1);
            Listener.BeginAccept(
                new AsyncCallback(AcceptCallback),
                state);
        }
Exemplo n.º 7
0
        private static void OnAcceptProc(IAsyncResult ar)
        {
            if (Listener == null)
            {
                throw new Exception("Not Exists Server object");
            }

            if (Listener != null)
            {
                try
                {
                    Socket handler = Listener.EndAccept(ar);

                    byte[] buf = new byte[256];

                    var wrapObject = new WrapObject
                    {
                        Handler = handler,
                        Buffer  = buf
                    };

                    if (handler != null)
                    {
                        handler.BeginReceive(buf, 0, buf.Length, SocketFlags.None, asyncReceive, wrapObject);
                    }

                    Listener.BeginAccept(asyncAccept, null);
                }
                catch (SocketException e) { }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        void _AcceptCallback(IAsyncResult result)
        {
            Socket _connection = Listener.EndAccept(result);

            try
            {
                WebSocketClient      _client     = WebSocketClient.Create <T>(_connection);
                IWebSocketHandshaker _handshaker = WebSocketHandshakerFactory.CreateWebSocketHandshaker(_client);

                _handshaker.Handshake();

                Clients.Add(_client);
            }
            catch (Exception ex)
            {
                ex = ex = null;

                _connection.Close();
                _connection.Dispose();
            }
            finally
            {
                Listener.BeginAccept(new AsyncCallback(_AcceptCallback), null);
            }
        }
Exemplo n.º 9
0
        private void AcceptClient(IAsyncResult ar)
        {
            var socket = Listener.EndAccept(ar);

            Console.WriteLine("Accepted new socket: {0}", socket.RemoteEndPoint);
            Clients.Add(new CoCProxyClient(socket));
            Listener.BeginAccept(AcceptClient, Listener);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Accepts an incoming connection
 /// </summary>
 void AcceptConnection(IAsyncResult result)
 {
     if (OnSocketConnect == null)
     {
         throw new Exception("Listener on port '" + Port + "' lacks a socket connect event handler.");
     }
     OnSocketConnect(new SocketConnectEventArgs(Listener.EndAccept(result)));
     Listener.BeginAccept(AcceptConnection, null);
 }
Exemplo n.º 11
0
        private void AcceptClient(IAsyncResult ar)
        {
            var socket = Listener.EndAccept(ar);
            var client = new CoCProxyClient(socket);

            Console.WriteLine("Accepted new socket: {0}", socket.RemoteEndPoint);
            client.Client.NetworkManager.ExceptionLog = ExceptionLog;
            Clients.Add(client);
            Listener.BeginAccept(AcceptClient, Listener);
        }
Exemplo n.º 12
0
        private void AcceptClient(IAsyncResult ar)
        {
            var socket = Listener.EndAccept(ar);
            var client = new CoCRemoteClient(this, socket);

            Clients.Add(client);

            LoginPacketHandlers.RegisterLoginPacketHandlers(client);
            InGamePacketHandler.RegisterInGamePacketHandlers(client);
            Listener.BeginAccept(AcceptClient, Listener);
        }
Exemplo n.º 13
0
        private void AcceptClient(IAsyncResult ar)
        {
            var socket = Listener.EndAccept(ar);
            var client = new CoCRemoteClient(this, socket);

            Console.WriteLine("Accepted new connection: {0}", socket.RemoteEndPoint);

            // should use a single dictionary of handlers instead.
            LoginPacketHandlers.RegisterLoginPacketHandlers(client);
            InGamePacketHandler.RegisterInGamePacketHandlers(client);
            Clients.Add(client);
            Listener.BeginAccept(AcceptClient, Listener);
        }
Exemplo n.º 14
0
 public void AcceptOne()
 {
     try
     {
         ThreadNotifier.Reset();
         Listener.BeginAccept(AcceptCallback, Listener);
         ThreadNotifier.WaitOne();
     }
     catch (Exception e)
     {
         OnError?.Invoke(this, e);
     }
 }
Exemplo n.º 15
0
        private void AcceptCallback(IAsyncResult ar)
        {
            var newSocket = Listener.EndAccept(ar);
            var node      = NodeBuilder.FromEndpoint((IPEndPoint)newSocket.RemoteEndPoint);

            var receiveState    = CreateNetworkState(newSocket, node);
            var responseChannel = new TcpReactorResponseChannel(this, newSocket, EventLoop.Clone(ProxiesShareFiber));

            SocketMap.Add(node, responseChannel);
            NodeConnected(node, responseChannel);
            newSocket.BeginReceive(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length, SocketFlags.None, ReceiveCallback, receiveState);
            Listener.BeginAccept(AcceptCallback, null); //accept more connections
        }
Exemplo n.º 16
0
        private void AcceptCallback(IAsyncResult ar)
        {
            var handler = Listener.EndAccept(ar);

            Listener.BeginAccept(new AsyncCallback(AcceptCallback), null);

            var clientHandler = new SocketServerConnection(handler, this);

            clientHandler.OnClose += OnClientHandler_Close;
            Connections.Add(clientHandler);

            clientHandler.Start();
        }
Exemplo n.º 17
0
        public void StartServer()
        {
            _serverListener  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Listener.NoDelay = true;
            timer            = new Timer(100);
            timer.AutoReset  = true;
            timer.Elapsed   += SendStepAll;
            timer.Enabled    = true;
            Listener.Bind(new IPEndPoint(IPAddress.Parse(this.IP), this.Port));
            Listener.Listen(this._maxConnectNum);
            Listener.BeginAccept(AcceptCallBack, Listener);
            _frameList   = new List <byte[]>();
            _stepMessage = new byte[this._maxConnectNum][];

            ServerLog.LogServer("服务器启动成功", 0);
        }
Exemplo n.º 18
0
        private static void OnAcceptProc(IAsyncResult ar)
        {
            if (Listener == null)
            {
                throw new Exception("Not Exists Server object");
            }

            if (Listener != null)
            {
                try
                {
                    Socket handler = Listener.EndAccept(ar);

                    OnReceive(handler);

                    Listener.BeginAccept(asyncAccept, null);
                }
                catch (SocketException e) { }
            }
        }
Exemplo n.º 19
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                var newSocket = Listener.EndAccept(ar);
                var node      = NodeBuilder.FromEndpoint((IPEndPoint)newSocket.RemoteEndPoint);

                HeliosTrace.Instance.TcpInboundAcceptSuccess();

                var receiveState    = CreateNetworkState(newSocket, node);
                var responseChannel = new TcpReactorResponseChannel(this, newSocket, EventLoop.Clone(ProxiesShareFiber));
                SocketMap.TryAdd(node, responseChannel);
                NodeConnected(node, responseChannel);
                newSocket.BeginReceive(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length, SocketFlags.None,
                                       ReceiveCallback, receiveState);
                Listener.BeginAccept(AcceptCallback, null); //accept more connections
            }
            catch (Exception ex)
            {
                HeliosTrace.Instance.TcpInboundAcceptFailure(ex.Message);
            }
        }
Exemplo n.º 20
0
 protected override void StartInternal()
 {
     IsActive = true;
     Listener.Listen(Backlog);
     Listener.BeginAccept(AcceptCallback, null);
 }
Exemplo n.º 21
0
 private void WaitForConnection()
 {
     Listener.BeginAccept(ConnectionHandler, null);
 }