當前位置: 首頁>>代碼示例>>Golang>>正文


Golang TCPConn.CloseRead方法代碼示例

本文整理匯總了Golang中net.TCPConn.CloseRead方法的典型用法代碼示例。如果您正苦於以下問題:Golang TCPConn.CloseRead方法的具體用法?Golang TCPConn.CloseRead怎麽用?Golang TCPConn.CloseRead使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.TCPConn的用法示例。


在下文中一共展示了TCPConn.CloseRead方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: pipeThenClose

func pipeThenClose(src, dst *net.TCPConn, finishChannel chan bool) {
	defer func() {
		src.CloseRead()
		dst.CloseWrite()
		finishChannel <- true
	}()

	buf := asocks.GetBuffer()
	defer asocks.GiveBuffer(buf)

	for {
		src.SetReadDeadline(time.Now().Add(60 * time.Second))
		n, err := src.Read(buf)
		if n > 0 {
			data := buf[0:n]
			encodeData(data)
			if _, err := dst.Write(data); err != nil {
				break
			}
		}
		if err != nil {
			break
		}
	}
}
開發者ID:momaer,項目名稱:asocks-go,代碼行數:25,代碼來源:local.go

示例2: proxy

// Copy from WebSocket to socket and vice versa.
func proxy(local *net.TCPConn, conn *websocketConn) {
	var wg sync.WaitGroup

	wg.Add(2)

	go func() {
		_, err := io.Copy(conn, local)
		if err != nil {
			Log("error copying ORPort to WebSocket")
		}
		local.CloseRead()
		conn.Close()
		wg.Done()
	}()

	go func() {
		_, err := io.Copy(local, conn)
		if err != nil {
			Log("error copying WebSocket to ORPort")
		}
		local.CloseWrite()
		conn.Close()
		wg.Done()
	}()

	wg.Wait()
}
開發者ID:aallai,項目名稱:flashproxy,代碼行數:28,代碼來源:websocket-server.go

示例3: proxy

func proxy(cliConn *net.TCPConn, rAddr *net.TCPAddr) error {
	srvConn, err := net.DialTCP("tcp", nil, rAddr)
	if err != nil {
		cliConn.Close()
		return err
	}
	defer srvConn.Close()

	// channels to wait on the close event for each connection
	serverClosed := make(chan struct{}, 1)
	clientClosed := make(chan struct{}, 1)

	go broker(srvConn, cliConn, clientClosed)
	go broker(cliConn, srvConn, serverClosed)

	var waitFor chan struct{}
	select {
	case <-clientClosed:
		// the client closed first
		srvConn.SetLinger(0)
		srvConn.CloseRead()
		waitFor = serverClosed
	case <-serverClosed:
		cliConn.CloseRead()
		waitFor = clientClosed
	}

	<-waitFor
	return nil
}
開發者ID:thraxil,項目名稱:cbp,代碼行數:30,代碼來源:cbp.go

示例4: Pipe

// Pipe starts bridging with two tcp connection
func Pipe(dst *net.TCPConn, src *net.TCPConn, f *func([]byte) []byte) error {
	defer src.CloseRead()
	defer dst.CloseWrite()

	rb := make([]byte, 4096)

	for {
		rsize, err := src.Read(rb)
		if err != nil {
			if isRecoverable(err) {
				continue
			}
			return err
		}

		var wb []byte
		if f != nil {
			wb = (*f)(rb[:rsize])
		} else {
			wb = rb[:rsize]
		}
		wWrote := 0
		wTotal := len(wb)
		for wWrote != wTotal {
			wSize, err := dst.Write(wb[wWrote:])
			wWrote += wSize
			if err != nil {
				if isRecoverable(err) {
					continue
				}
				return err
			}
		}
	}
}
開發者ID:nyushi,項目名稱:traproxy,代碼行數:36,代碼來源:util.go

示例5: TCProxy

func TCProxy(srvConn, cliConn *net.TCPConn) {
	// channels to wait on the close event for each connection
	serverClosed := make(chan struct{}, 1)
	clientClosed := make(chan struct{}, 1)

	go broker(srvConn, cliConn, clientClosed)
	go broker(cliConn, srvConn, serverClosed)

	// wait for one half of the proxy to exit, then trigger a shutdown of the
	// other half by calling CloseRead(). This will break the read loop in the
	// broker and allow us to fully close the connection cleanly without a
	// "use of closed network connection" error.
	var waitFor chan struct{}
	select {
	case <-clientClosed:
		// the client closed first and any more packets from the server aren't
		// useful, so we can optionally SetLinger(0) here to recycle the port
		// faster.
		srvConn.SetLinger(0)
		srvConn.CloseRead()
		waitFor = serverClosed
	case <-serverClosed:
		cliConn.CloseRead()
		waitFor = clientClosed
	}

	// Wait for the other connection to close.
	// This "waitFor" pattern isn't required, but gives us a way to track the
	// connection and ensure all copies terminate correctly; we can trigger
	// stats on entry and deferred exit of this function.
	<-waitFor
}
開發者ID:marcinwyszynski,項目名稱:tcproxy,代碼行數:32,代碼來源:tcproxy.go

示例6: tcpShim

func tcpShim(inbound, outbound *net.TCPConn, connEvent *events.Connection, eh events.Handler) error {
	eh.Connection(connEvent)
	ch := make(chan error, 1)
	go func() {
		var err error
		defer func() { ch <- err }()
		_, err = io.Copy(inbound, outbound)
		outbound.CloseRead()
		inbound.CloseWrite()
	}()

	_, err1 := io.Copy(outbound, inbound)
	inbound.CloseRead()
	outbound.CloseWrite()

	err2 := <-ch
	inbound.Close()
	outbound.Close()

	if err1 != nil {
		return err1
	} else {
		return err2
	}
}
開發者ID:errordeveloper,項目名稱:flux,代碼行數:25,代碼來源:forward.go

示例7: handleConnection

func (proxy *Proxy) handleConnection(in *net.TCPConn) error {
	defer in.Close()

	plainOut, err := proxy.connectionFactory()
	if err != nil {
		log.Print("could no create outgoing connection", err)
		return err
	}
	out := plainOut.(*net.TCPConn)
	defer out.Close()

	serverClosed := make(chan struct{}, 1)
	clientClosed := make(chan struct{}, 1)

	go broker(out, in, clientClosed)
	go broker(in, out, serverClosed)

	var waitFor chan struct{}

	select {
	case <-clientClosed:
		// the client closed first and any more packets from the server aren't
		// useful, so we can optionally SetLinger(0) here to recycle the port
		// faster.
		out.SetLinger(0)
		out.CloseRead()
		waitFor = serverClosed
	case <-serverClosed:
		in.CloseRead()
		waitFor = clientClosed
	}

	<-waitFor
	return nil
}
開發者ID:mikroio,項目名稱:tcp-forward-proxy,代碼行數:35,代碼來源:proxy.go

示例8: copyAndClose

func copyAndClose(ctx *ProxyCtx, dst, src *net.TCPConn) {
	if _, err := io.Copy(dst, src); err != nil {
		ctx.Warnf("Error copying to client: %s", err)
	}

	dst.CloseWrite()
	src.CloseRead()
}
開發者ID:elazarl,項目名稱:goproxy,代碼行數:8,代碼來源:https.go

示例9: ProxyTCP

func (self *Session) ProxyTCP(conn *net.TCPConn, bufferSize int) {
	writeClosed := make(chan struct{})
	readClosed := make(chan struct{})
	go func() {
		<-writeClosed
		<-readClosed
		conn.Close()
		self.log("proxy closed")
	}()
	// to conn
	go func() {
		var once sync.Once
		for {
			select {
			case msg := <-self.Message:
				switch msg.Tag {
				case DATA:
					_, err := conn.Write(msg.Data)
					if err != nil {
						self.log("proxy write error %v", err)
						go once.Do(func() {
							<-time.After(time.Second * 5)
							conn.CloseWrite()
							close(writeClosed)
						})
						self.AbortRead()
					}
				case STATE:
					switch msg.State {
					case STATE_FINISH_SEND, STATE_ABORT_SEND:
						go once.Do(func() {
							<-time.After(time.Second * 5)
							conn.CloseWrite()
							close(writeClosed)
						})
					case STATE_ABORT_READ:
					case STATE_FINISH_READ:
					}
				}
			case <-self.Stopped:
				return
			}
		}
	}()
	// from conn
	for {
		buf := make([]byte, bufferSize)
		n, err := conn.Read(buf)
		if err != nil {
			conn.CloseRead()
			close(readClosed)
			self.FinishSend()
			return
		}
		self.Send(buf[:n])
	}
}
開發者ID:ZhangJun-GitHub,項目名稱:gotunnel,代碼行數:57,代碼來源:proxy.go

示例10: copyBytes

func copyBytes(in, out *net.TCPConn) {
	glog.Infof("Copying from %v <-> %v <-> %v <-> %v",
		in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
	if _, err := io.Copy(in, out); err != nil {
		glog.Errorf("I/O error: %v", err)
	}
	in.CloseRead()
	out.CloseWrite()
}
開發者ID:vishvananda,項目名稱:wormhole,代碼行數:9,代碼來源:proxier.go

示例11: sendDest

func sendDest(source *net.TCPConn, dest []*DestList) {
	buffer := make([]byte, MIN_BUF_SIZE)
	n, _ := source.Read(buffer)
	// log.Println("recv data len:=>", n)
	for _, d := range dest {
		d.Conn.Write(buffer[:n])
		d.Conn.CloseWrite()
	}
	source.CloseRead()
}
開發者ID:sunvim,項目名稱:forward,代碼行數:10,代碼來源:forwarder.go

示例12: copy_half

// Copy one side of the socket, doing a half close when it has
// finished
func copy_half(backend *Backend, dst, src *net.TCPConn, wg *sync.WaitGroup) {
	defer wg.Done()
	transferred, err := io.Copy(dst, src)
	backend.transferred += transferred
	if err != nil {
		log.Printf("Error: %s", err)
	}
	dst.CloseWrite()
	src.CloseRead()
}
開發者ID:josegonzalez,項目名稱:tcp_fallback,代碼行數:12,代碼來源:tcp_fallback.go

示例13: copyBytes

func copyBytes(direction string, dest, src *net.TCPConn, wg *sync.WaitGroup) {
	defer wg.Done()
	glog.V(4).Infof("Copying %s: %s -> %s", direction, src.RemoteAddr(), dest.RemoteAddr())
	n, err := io.Copy(dest, src)
	if err != nil {
		glog.Errorf("I/O error: %v", err)
	}
	glog.V(4).Infof("Copied %d bytes %s: %s -> %s", n, direction, src.RemoteAddr(), dest.RemoteAddr())
	dest.CloseWrite()
	src.CloseRead()
}
開發者ID:ericcapricorn,項目名稱:kubernetes,代碼行數:11,代碼來源:proxier.go

示例14: CopyBytes

func CopyBytes(in, out *net.TCPConn) {
	log.Printf("Copying from %v <-> %v <-> %v <-> %v",
		in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
	_, err := io.Copy(in, out)
	if err != nil && err != io.EOF {
		log.Printf("I/O error: %v", err)
	}

	in.CloseRead()
	out.CloseWrite()
}
開發者ID:Jingzhe88,項目名稱:kubernetes,代碼行數:11,代碼來源:proxier.go

示例15: copyBytes

func copyBytes(logLevel int, direction string, dest, src *net.TCPConn, wg *sync.WaitGroup) {
	defer wg.Done()
	if logLevel > 0 {
		log.Printf("Copying %s: %s -> %s", direction, src.RemoteAddr(), dest.RemoteAddr())
	}
	n, err := io.Copy(dest, src)
	if err != nil {
		log.Printf("I/O error: %v", err)
	}
	if logLevel > 0 {
		log.Printf("Copied %d bytes %s: %s -> %s", n, direction, src.RemoteAddr(), dest.RemoteAddr())
	}
	dest.CloseWrite()
	src.CloseRead()
}
開發者ID:brandnetworks,項目名稱:tcpproxy,代碼行數:15,代碼來源:tcpproxy.go


注:本文中的net.TCPConn.CloseRead方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。