Esempio n. 1
0
        private void process_receive(SocketAsyncEventArgs e)
        {
            CUserToken token = e.UserToken as CUserToken;

            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                //이후의 작업은 CUserToken에 맡긴다.
                token.on_receive(e.Buffer, e.Offset, e.BytesTransferred);

                //다음 메시지 수신을 위해서 다시 ReceiveAsync 메소드를 호출한다.
                bool pending = token.socket.ReceiveAsync(e);
                if (!pending)
                {
                    process_receive(e);
                }
            }
            else
            {
                Console.WriteLine(string.Format("error {0}, transferred {1}", e.SocketError, e.BytesTransferred));
                close_clientsocket(token);
            }
        }
Esempio n. 2
0
        public void listen(string host, int port, int backlog)
        {
            this.client_listener = new CListener();
            this.client_listener.callback_on_newclient += on_new_client;
            this.client_listener.Start(host, port, backlog);

            this.receive_event_args_pool = new SocketAsyncEventArgsPool(this.max_connections);
            this.send_event_args_pool    = new SocketAsyncEventArgsPool(this.max_connections);

            SocketAsyncEventArgs arg;

            for (int i = 0; i < this.max_connections; i++)
            {
                CUserToken token = new CUserToken();

                //receive pool
                {
                    arg            = new SocketAsyncEventArgs();
                    arg.Completed += new EventHandler <SocketAsyncEventArgs>(receive_completed);
                    arg.UserToken  = token;

                    this.buffer_manager.SetBuffer(arg);
                    this.receive_event_args_pool.Push(arg);
                }

                //send pool
                {
                    arg            = new SocketAsyncEventArgs();
                    arg.Completed += new EventHandler <SocketAsyncEventArgs>(send_completed);
                    arg.UserToken  = token;

                    this.buffer_manager.SetBuffer(arg);
                    this.send_event_args_pool.Push(arg);
                }
            }
        }