本文整理汇总了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
}
}
}
示例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()
}
示例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
}
示例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
}
}
}
}
示例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
}
示例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
}
}
示例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
}
示例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()
}
示例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])
}
}
示例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()
}
示例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()
}
示例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()
}
示例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()
}
示例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()
}
示例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()
}