mirror of https://github.com/v2ray/v2ray-core
				
				
				
			simplify done api
							parent
							
								
									9d8922f523
								
							
						
					
					
						commit
						d6dc88860b
					
				|  | @ -19,7 +19,7 @@ type OutboundListener struct { | ||||||
| func (l *OutboundListener) add(conn net.Conn) { | func (l *OutboundListener) add(conn net.Conn) { | ||||||
| 	select { | 	select { | ||||||
| 	case l.buffer <- conn: | 	case l.buffer <- conn: | ||||||
| 	case <-l.done.C(): | 	case <-l.done.Wait(): | ||||||
| 		common.Ignore(conn.Close(), "We can do nothing if Close() returns error.") | 		common.Ignore(conn.Close(), "We can do nothing if Close() returns error.") | ||||||
| 	default: | 	default: | ||||||
| 		common.Ignore(conn.Close(), "We can do nothing if Close() returns error.") | 		common.Ignore(conn.Close(), "We can do nothing if Close() returns error.") | ||||||
|  | @ -29,7 +29,7 @@ func (l *OutboundListener) add(conn net.Conn) { | ||||||
| // Accept implements net.Listener.
 | // Accept implements net.Listener.
 | ||||||
| func (l *OutboundListener) Accept() (net.Conn, error) { | func (l *OutboundListener) Accept() (net.Conn, error) { | ||||||
| 	select { | 	select { | ||||||
| 	case <-l.done.C(): | 	case <-l.done.Wait(): | ||||||
| 		return nil, newError("listen closed") | 		return nil, newError("listen closed") | ||||||
| 	case c := <-l.buffer: | 	case c := <-l.buffer: | ||||||
| 		return c, nil | 		return c, nil | ||||||
|  |  | ||||||
|  | @ -134,7 +134,7 @@ func (c *udpConn) Read(buf []byte) (int, error) { | ||||||
| 			c.uplink.Add(int64(nBytes)) | 			c.uplink.Add(int64(nBytes)) | ||||||
| 		} | 		} | ||||||
| 		return nBytes, nil | 		return nBytes, nil | ||||||
| 	case <-c.done.C(): | 	case <-c.done.Wait(): | ||||||
| 		return 0, io.EOF | 		return 0, io.EOF | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -239,7 +239,7 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest | ||||||
| 	conn, existing := w.getConnection(id) | 	conn, existing := w.getConnection(id) | ||||||
| 	select { | 	select { | ||||||
| 	case conn.input <- b: | 	case conn.input <- b: | ||||||
| 	case <-conn.done.C(): | 	case <-conn.done.Wait(): | ||||||
| 		b.Release() | 		b.Release() | ||||||
| 	default: | 	default: | ||||||
| 		b.Release() | 		b.Release() | ||||||
|  | @ -308,7 +308,7 @@ func (w *udpWorker) monitor() { | ||||||
| 
 | 
 | ||||||
| 	for { | 	for { | ||||||
| 		select { | 		select { | ||||||
| 		case <-w.done.C(): | 		case <-w.done.Wait(): | ||||||
| 			return | 			return | ||||||
| 		case <-timer.C: | 		case <-timer.C: | ||||||
| 			nowSec := time.Now().Unix() | 			nowSec := time.Now().Unix() | ||||||
|  |  | ||||||
|  | @ -125,7 +125,7 @@ func (m *Client) monitor() { | ||||||
| 
 | 
 | ||||||
| 	for { | 	for { | ||||||
| 		select { | 		select { | ||||||
| 		case <-m.done.C(): | 		case <-m.done.Wait(): | ||||||
| 			m.sessionManager.Close() | 			m.sessionManager.Close() | ||||||
| 			m.inboundRay.InboundInput().Close() | 			m.inboundRay.InboundInput().Close() | ||||||
| 			m.inboundRay.InboundOutput().CloseError() | 			m.inboundRay.InboundOutput().CloseError() | ||||||
|  |  | ||||||
|  | @ -51,7 +51,7 @@ func (l *generalLogger) run() { | ||||||
| 
 | 
 | ||||||
| 	for { | 	for { | ||||||
| 		select { | 		select { | ||||||
| 		case <-l.done.C(): | 		case <-l.done.Wait(): | ||||||
| 			return | 			return | ||||||
| 		case msg := <-l.buffer: | 		case msg := <-l.buffer: | ||||||
| 			logger.Write(msg.String() + platform.LineSeparator()) | 			logger.Write(msg.String() + platform.LineSeparator()) | ||||||
|  |  | ||||||
|  | @ -21,23 +21,18 @@ func NewDone() *Done { | ||||||
| // Done returns true if Close() is called.
 | // Done returns true if Close() is called.
 | ||||||
| func (d *Done) Done() bool { | func (d *Done) Done() bool { | ||||||
| 	select { | 	select { | ||||||
| 	case <-d.c: | 	case <-d.Wait(): | ||||||
| 		return true | 		return true | ||||||
| 	default: | 	default: | ||||||
| 		return false | 		return false | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // C returns a channel for waiting for done.
 | // Wait returns a channel for waiting for done.
 | ||||||
| func (d *Done) C() chan struct{} { | func (d *Done) Wait() <-chan struct{} { | ||||||
| 	return d.c | 	return d.c | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Wait blocks until Close() is called.
 |  | ||||||
| func (d *Done) Wait() { |  | ||||||
| 	<-d.c |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status.
 | // Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status.
 | ||||||
| func (d *Done) Close() error { | func (d *Done) Close() error { | ||||||
| 	d.access.Lock() | 	d.access.Lock() | ||||||
|  |  | ||||||
|  | @ -71,7 +71,7 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) | ||||||
| 		Local:  l.Addr(), | 		Local:  l.Addr(), | ||||||
| 		Remote: l.Addr(), | 		Remote: l.Addr(), | ||||||
| 	}) | 	}) | ||||||
| 	<-done.C() | 	<-done.Wait() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) { | func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Darien Raymond
						Darien Raymond