当前位置: 首页>>代码示例>>Golang>>正文


Golang net.ChanToWriter函数代码示例

本文整理汇总了Golang中github.com/v2ray/v2ray-core/common/net.ChanToWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang ChanToWriter函数的具体用法?Golang ChanToWriter怎么用?Golang ChanToWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ChanToWriter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handleRequest

func handleRequest(conn *net.TCPConn, request *protocol.VMessRequest, input <-chan []byte, finish *sync.Mutex) {
	defer finish.Unlock()
	encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn)
	if err != nil {
		log.Error("VMessOut: Failed to create encrypt writer: %v", err)
		return
	}

	buffer := make([]byte, 0, 2*1024)
	buffer, err = request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, buffer)
	if err != nil {
		log.Error("VMessOut: Failed to serialize VMess request: %v", err)
		return
	}

	// Send first packet of payload together with request, in favor of small requests.
	payload, open := <-input
	if open {
		encryptRequestWriter.Crypt(payload)
		buffer = append(buffer, payload...)

		_, err = conn.Write(buffer)
		if err != nil {
			log.Error("VMessOut: Failed to write VMess request: %v", err)
			return
		}

		v2net.ChanToWriter(encryptRequestWriter, input)
	}
	return
}
开发者ID:starsw001,项目名称:v2ray-core,代码行数:31,代码来源:vmessout.go

示例2: Dispatch

func (this *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.OutboundRay) error {
	input := ray.OutboundInput()
	output := ray.OutboundOutput()

	this.Destination = packet.Destination()
	if packet.Chunk() != nil {
		this.ConnOutput.Write(packet.Chunk().Value)
		packet.Chunk().Release()
	}

	if packet.MoreChunks() {
		writeFinish := &sync.Mutex{}

		writeFinish.Lock()

		go func() {
			v2net.ChanToWriter(this.ConnOutput, input)
			writeFinish.Unlock()
		}()

		writeFinish.Lock()
	}

	v2net.ReaderToChan(output, this.ConnInput)
	close(output)

	return nil
}
开发者ID:ducktsmt,项目名称:v2ray-core,代码行数:28,代码来源:outboundhandler.go

示例3: Communicate

func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
	ray := this.Dispatcher.DispatchToOutbound(packet)

	input := ray.InboundInput()
	output := ray.InboundOutput()

	readFinish := &sync.Mutex{}
	writeFinish := &sync.Mutex{}

	readFinish.Lock()
	writeFinish.Lock()

	go func() {
		v2net.ReaderToChan(input, this.ConnInput)
		close(input)
		readFinish.Unlock()
	}()

	go func() {
		v2net.ChanToWriter(this.ConnOutput, output)
		writeFinish.Unlock()
	}()

	readFinish.Lock()
	writeFinish.Lock()
	return nil
}
开发者ID:road0001,项目名称:v2ray-core,代码行数:27,代码来源:inboundhandler.go

示例4: Dispatch

func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
	conn, err := net.Dial(firstPacket.Destination().Network(), firstPacket.Destination().Address().String())
	log.Info("Freedom: Opening connection to %s", firstPacket.Destination().String())
	if err != nil {
		close(ray.OutboundOutput())
		log.Error("Freedom: Failed to open connection: %s : %v", firstPacket.Destination().String(), err)
		return err
	}

	input := ray.OutboundInput()
	output := ray.OutboundOutput()
	var readMutex, writeMutex sync.Mutex
	readMutex.Lock()
	writeMutex.Lock()

	if chunk := firstPacket.Chunk(); chunk != nil {
		conn.Write(chunk.Value)
		chunk.Release()
	}

	if !firstPacket.MoreChunks() {
		writeMutex.Unlock()
	} else {
		go func() {
			v2net.ChanToWriter(conn, input)
			writeMutex.Unlock()
		}()
	}

	go func() {
		defer readMutex.Unlock()
		defer close(output)

		response, err := v2net.ReadFrom(conn, nil)
		log.Info("Freedom receives %d bytes from %s", response.Len(), conn.RemoteAddr().String())
		if response.Len() > 0 {
			output <- response
		} else {
			response.Release()
		}
		if err != nil {
			return
		}
		if firstPacket.Destination().IsUDP() {
			return
		}

		v2net.ReaderToChan(output, conn)
	}()

	writeMutex.Lock()
	if tcpConn, ok := conn.(*net.TCPConn); ok {
		tcpConn.CloseWrite()
	}
	readMutex.Lock()
	conn.Close()

	return nil
}
开发者ID:sign4bill,项目名称:v2ray-core,代码行数:59,代码来源:freedom.go

示例5: Dispatch

func (this *BlackHole) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
	if chunk := firstPacket.Chunk(); chunk != nil {
		chunk.Release()
	}

	close(ray.OutboundOutput())
	if firstPacket.MoreChunks() {
		v2net.ChanToWriter(ioutil.Discard, ray.OutboundInput())
	}
	return nil
}
开发者ID:airmao,项目名称:v2ray-core,代码行数:11,代码来源:blackhole.go

示例6: Communicate

func (handler *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
	ray := handler.Server.DispatchToOutbound(packet)

	input := ray.InboundInput()
	output := ray.InboundOutput()

	input <- handler.Data2Send
	close(input)

	v2net.ChanToWriter(handler.DataReturned, output)
	return nil
}
开发者ID:kkndyu,项目名称:v2ray-core,代码行数:12,代码来源:inboundhandler.go

示例7: Communicate

func (handler *InboundConnectionHandler) Communicate(dest v2net.Destination) error {
	ray := handler.Server.NewInboundConnectionAccepted(dest)

	input := ray.InboundInput()
	output := ray.InboundOutput()

	input <- handler.Data2Send
	close(input)

	v2net.ChanToWriter(handler.DataReturned, output)
	return nil
}
开发者ID:hxford,项目名称:v2ray-core,代码行数:12,代码来源:inboundhandler.go

示例8: runBenchmarkTransport

func runBenchmarkTransport(size int) {

	transportChanA := make(chan *alloc.Buffer, 16)
	transportChanB := make(chan *alloc.Buffer, 16)

	readerA := &StaticReader{size, 0}
	readerB := &StaticReader{size, 0}

	writerA := ioutil.Discard
	writerB := ioutil.Discard

	finishA := make(chan bool)
	finishB := make(chan bool)

	go func() {
		v2net.ChanToWriter(writerA, transportChanA)
		close(finishA)
	}()

	go func() {
		v2net.ReaderToChan(transportChanA, readerA)
		close(transportChanA)
	}()

	go func() {
		v2net.ChanToWriter(writerB, transportChanB)
		close(finishB)
	}()

	go func() {
		v2net.ReaderToChan(transportChanB, readerB)
		close(transportChanB)
	}()

	<-transportChanA
	<-transportChanB
}
开发者ID:ducktsmt,项目名称:v2ray-core,代码行数:37,代码来源:transport_test.go

示例9: Communicate

func (handler *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
	ray := handler.Server.DispatchToOutbound(packet)

	input := ray.InboundInput()
	output := ray.InboundOutput()

	buffer := alloc.NewBuffer()
	buffer.Clear()
	buffer.Append(handler.Data2Send)
	input <- buffer
	close(input)

	v2net.ChanToWriter(handler.DataReturned, output)
	return nil
}
开发者ID:NinjaOSX,项目名称:v2ray-core,代码行数:15,代码来源:inboundhandler.go

示例10: transport

func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
	var wg sync.WaitGroup
	wg.Add(2)
	defer wg.Wait()

	go func() {
		v2net.ReaderToChan(ray.InboundInput(), input)
		close(ray.InboundInput())
		wg.Done()
	}()

	go func() {
		v2net.ChanToWriter(output, ray.InboundOutput())
		wg.Done()
	}()
}
开发者ID:ibmendoza,项目名称:v2ray-core,代码行数:16,代码来源:http.go

示例11: handleRequest

func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan *alloc.Buffer, finish *sync.Mutex) {
	defer finish.Unlock()
	aesStream, err := v2crypto.NewAesEncryptionStream(request.RequestKey[:], request.RequestIV[:])
	if err != nil {
		log.Error("VMessOut: Failed to create AES encryption stream: ", err)
		return
	}
	encryptRequestWriter := v2crypto.NewCryptionWriter(aesStream, conn)

	buffer := alloc.NewBuffer().Clear()
	defer buffer.Release()
	buffer, err = request.ToBytes(protocol.NewRandomTimestampGenerator(protocol.Timestamp(time.Now().Unix()), 30), buffer)
	if err != nil {
		log.Error("VMessOut: Failed to serialize VMess request: ", err)
		return
	}

	// Send first packet of payload together with request, in favor of small requests.
	firstChunk := firstPacket.Chunk()
	moreChunks := firstPacket.MoreChunks()

	for firstChunk == nil && moreChunks {
		firstChunk, moreChunks = <-input
	}

	if firstChunk == nil && !moreChunks {
		log.Warning("VMessOut: Nothing to send. Existing...")
		return
	}

	aesStream.XORKeyStream(firstChunk.Value, firstChunk.Value)
	buffer.Append(firstChunk.Value)
	firstChunk.Release()

	_, err = conn.Write(buffer.Value)
	if err != nil {
		log.Error("VMessOut: Failed to write VMess request: ", err)
		return
	}

	if moreChunks {
		v2net.ChanToWriter(encryptRequestWriter, input)
	}
	return
}
开发者ID:ibmendoza,项目名称:v2ray-core,代码行数:45,代码来源:outbound.go

示例12: handleRequest

func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan *alloc.Buffer, finish *sync.Mutex) {
	defer finish.Unlock()
	encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn)
	if err != nil {
		log.Error("VMessOut: Failed to create encrypt writer: %v", err)
		return
	}

	buffer := alloc.NewBuffer()
	buffer.Clear()
	requestBytes, err := request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, buffer.Value)
	if err != nil {
		log.Error("VMessOut: Failed to serialize VMess request: %v", err)
		return
	}

	// Send first packet of payload together with request, in favor of small requests.
	firstChunk := firstPacket.Chunk()
	moreChunks := firstPacket.MoreChunks()

	if firstChunk == nil && moreChunks {
		firstChunk, moreChunks = <-input
	}

	if firstChunk != nil {
		encryptRequestWriter.Crypt(firstChunk.Value)
		requestBytes = append(requestBytes, firstChunk.Value...)
		firstChunk.Release()

		_, err = conn.Write(requestBytes)
		buffer.Release()
		if err != nil {
			log.Error("VMessOut: Failed to write VMess request: %v", err)
			return
		}
	}

	if moreChunks {
		v2net.ChanToWriter(encryptRequestWriter, input)
	}
	return
}
开发者ID:xiatiansong,项目名称:v2ray-core,代码行数:42,代码来源:vmessout.go

示例13: transport

func (this *SocksServer) transport(reader io.Reader, writer io.Writer, firstPacket v2net.Packet) {
	ray := this.space.PacketDispatcher().DispatchToOutbound(firstPacket)
	input := ray.InboundInput()
	output := ray.InboundOutput()

	var inputFinish, outputFinish sync.Mutex
	inputFinish.Lock()
	outputFinish.Lock()

	go func() {
		v2net.ReaderToChan(input, reader)
		inputFinish.Unlock()
		close(input)
	}()

	go func() {
		v2net.ChanToWriter(writer, output)
		outputFinish.Unlock()
	}()
	outputFinish.Lock()
}
开发者ID:adoot,项目名称:v2ray-core,代码行数:21,代码来源:socks.go

示例14: TestReaderAndWrite

func TestReaderAndWrite(t *testing.T) {
	v2testing.Current(t)

	size := 1024 * 1024
	buffer := make([]byte, size)
	nBytes, err := rand.Read(buffer)
	assert.Int(nBytes).Equals(len(buffer))
	assert.Error(err).IsNil()

	readerBuffer := bytes.NewReader(buffer)
	writerBuffer := bytes.NewBuffer(make([]byte, 0, size))

	transportChan := make(chan *alloc.Buffer, 1024)

	err = v2net.ReaderToChan(transportChan, readerBuffer)
	assert.Error(err).Equals(io.EOF)
	close(transportChan)

	err = v2net.ChanToWriter(writerBuffer, transportChan)
	assert.Error(err).IsNil()

	assert.Bytes(buffer).Equals(writerBuffer.Bytes())
}
开发者ID:ducktsmt,项目名称:v2ray-core,代码行数:23,代码来源:transport_test.go

示例15: DumpInput

func (vconn *FreedomConnection) DumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {
	v2net.ChanToWriter(conn, input)
	finish <- true
}
开发者ID:ramigg,项目名称:v2ray-core,代码行数:4,代码来源:freedom.go


注:本文中的github.com/v2ray/v2ray-core/common/net.ChanToWriter函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。