Beispiel #1
0
        void asyncListener_SocketConnectioned(object sender, SocketConnectionEventArgs e)
        {
            if (this.ConnectionCreated != null)
            {
                SHSocket sh = new SHSocket(e.Socket);
                SHConnectionCreatedEventArgs evenArgs=new SHConnectionCreatedEventArgs();
                evenArgs.Bind(sh);

                this.ConnectionCreated(sender, evenArgs);
            }
        }
        protected void BeginAccept()
        {
            if (this.listenSocket == null) { throw new NullReferenceException("socket is null"); }

            this.listenSocket.BeginAccept((result) =>
            {
                Socket s = result.AsyncState as Socket;
                try
                {
                    Socket sb= s.EndAccept(result);

                    if (sb == null)
                    {
                        Trace.WriteLine("sb is null");
                        return;
                    }

                    sb.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, bufferSize * 1024);
                    sb.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, bufferSize * 1024);
                    
                    SocketConnectionEventArgs scArgs = new SocketConnectionEventArgs();
                    scArgs.Bind(this.listenSocket);

                    if (this.SocketConnectioned != null)
                    {
                        this.SocketConnectioned(this.listenSocket, scArgs);
                    }
                }
                catch(Exception e)
                {
                    Trace.WriteLine(e.ToString());
                }

                try
                {
                    lock (syncRoot)
                    {
                        if (this.listenSocket != null)
                        {
                            this.BeginAccept();
                        }
                    }
                }
                catch
                {
                    this.Stop();
                    throw;
                }

            }, this.listenSocket);
        }