示例#1
0
        public void Start()
        {
            if (!Connected)
            {
                Socket sock = GetSocket(mServer, mPort);

                if (sock != null)
                {
                    ses = new NetSession(sock);
                    ses.DataReceived += new DataReceived(NetSession_DataReceived);
                    ses.Notified     += new Action <NotifyEventArgs>(NetSession_Notified);
                    ses.BeginReceive();
                }
            }
        }
示例#2
0
        //! Socket's BeginAccept() callback
        //  Handles new connection and create session
        private void AcceptCallback(IAsyncResult ar)
        {
            // our session
            NetSession ses = null;

            try
            {
                // Signal the main thread to continue.
                allDone.Set();

                // Get the socket that handles the client request.
                //Socket listener = sock;
                Socket     listener = (Socket)ar.AsyncState;
                Socket     handler  = listener.EndAccept(ar);
                IPEndPoint ep       = (IPEndPoint)handler.RemoteEndPoint;

                if (CanConnect(ep))
                {
                    // setup the session
                    ses = new NetSession(handler)
                    {
                        Id = NewSessionId()
                    };

                    // bind session event handler
                    ses.Notified         += new Action <NotifyEventArgs>(NetSession_Notified);
                    ses.ConnectionClosed += new ConnectionClosed(NetSession_Closed);
                    ses.DataReceived     += new DataReceived(NetSession_DataReceived);

                    ses.BeginReceive();

                    // add session to our table
                    lock (sessions)
                    {
                        sessions.Add(ses.Id, ses);
                    }

                    // Call ClientAccepted handler
                    ClientAccepted?.Invoke(ses);
                }
                else
                {
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }

                // start listen for connections again.
                listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
            }
            catch (SocketException e)
            {
                if (ses != null)
                {
                    ses.Close();
                }

                // Queue the next accept, think this should be here, stop attacks based on killing the waiting listeners
                if (sock != null)
                {
                    sock.BeginAccept(new AsyncCallback(AcceptCallback), sock);
                }
            }
            catch (Exception e)
            {
                if (ses != null)
                {
                    ses.Close();
                }

                // Queue the next accept, think this should be here, stop attacks based on killing the waiting listeners
                if (sock != null)
                {
                    sock.BeginAccept(new AsyncCallback(AcceptCallback), sock);
                }
            }
        }