项目作者: hslam

项目描述 :
Package splice wraps the splice system call.
高级语言: Go
项目地址: git://github.com/hslam/splice.git
创建时间: 2020-12-10T01:05:08Z
项目社区:https://github.com/hslam/splice

开源协议:MIT License

下载


splice

PkgGoDev
Build Status
codecov
Go Report Card
LICENSE

Package splice wraps the splice system call.

Get started

Install

  1. go get github.com/hslam/splice

Import

  1. import "github.com/hslam/splice"

Usage

Example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/hslam/splice"
  5. "io"
  6. "net"
  7. "time"
  8. )
  9. func main() {
  10. contents := "Hello world"
  11. lis, err := net.Listen("tcp", ":9999")
  12. if err != nil {
  13. panic(err)
  14. }
  15. defer lis.Close()
  16. done := make(chan bool)
  17. go func() {
  18. conn, _ := lis.Accept()
  19. defer conn.Close()
  20. time.Sleep(time.Millisecond * 100)
  21. if _, err := splice.Splice(conn, conn, 1024); err != nil && err != io.EOF {
  22. panic(err)
  23. }
  24. close(done)
  25. }()
  26. conn, _ := net.Dial("tcp", "127.0.0.1:9999")
  27. conn.Write([]byte(contents))
  28. buf := make([]byte, 64)
  29. n, _ := conn.Read(buf)
  30. fmt.Println(string(buf[:n]))
  31. conn.Close()
  32. <-done
  33. }

Output

  1. Hello world

License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

splice was written by Meng Huang.