本文整理汇总了Golang中net.TCPConn.SetNoDelay方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPConn.SetNoDelay方法的具体用法?Golang TCPConn.SetNoDelay怎么用?Golang TCPConn.SetNoDelay使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.TCPConn
的用法示例。
在下文中一共展示了TCPConn.SetNoDelay方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handlerConnection
//处理连接请求
func (this *server) handlerConnection(conn *net.TCPConn) {
defer logger.CatchException()
logger.Infof("New connection coming ... IP=%s ", conn.RemoteAddr())
conn.SetNoDelay(true) //无延迟
conn.SetKeepAlive(true) //保持激活
conn.SetReadBuffer(64 * 1024) //设置读缓冲区大小
conn.SetWriteBuffer(64 * 1024) //设置写缓冲区大小
conn.SetReadDeadline(time.Now().Add(30000000 * time.Second)) //设置读超时
session := network.NewSession(conn)
defer session.Close()
for {
msg, err := session.RecvMSG()
if err != nil {
logger.Infof("RecvMsgs IP=%s err=%v", conn.RemoteAddr(), err.Error())
return
}
ret := this.ProcessMessage(session, msg)
if ret == false {
return
}
}
}
示例2: newClientConn
func newClientConn(ls *LeaseServer, raw *net.TCPConn) (*clientConn, error) {
err := raw.SetKeepAlive(true)
if err != nil {
return nil, err
}
err = raw.SetNoDelay(true)
if err != nil {
return nil, err
}
ret := &clientConn{
id: incrementAndGet(&ls.clientIdCounter, 1),
c: raw,
d: gob.NewDecoder(raw),
e: gob.NewEncoder(raw),
req: ls.req,
resp: make(chan response, 20),
ackLock: new(sync.Mutex),
pendingAcks: make(map[uint64]chan bool),
}
// send client id
fmt.Printf("sending id %d\n", ret.id)
idBuff := make([]byte, 8, 8)
binary.LittleEndian.PutUint64(idBuff, ret.id)
ret.c.Write(idBuff)
fmt.Println("sent")
return ret, nil
}
示例3: configure
func (s *Server) configure(c *net.TCPConn) net.Conn {
c.SetNoDelay(true)
if s.cfg == nil {
return c
}
return tls.Server(c, s.cfg)
}
示例4: handleConnection
func handleConnection(conn *net.TCPConn, no_delay int, packet_bytes int) {
defer conn.Close()
fmt.Println("handle connection", conn)
if no_delay == 0 {
if err := conn.SetNoDelay(false); err != nil {
fmt.Println("set no delay to false failed.")
return
}
fmt.Println("set no delay to false ok.")
}
/*SO_SNDBUF := 16384
if err := conn.SetWriteBuffer(SO_SNDBUF); err != nil {
fmt.Println("set send SO_SNDBUF failed.")
return
}
fmt.Println("set send SO_SNDBUF to", SO_SNDBUF, "ok.")*/
b := make([]byte, packet_bytes)
fmt.Println("write", len(b), "bytes to conn")
for {
n, err := conn.Write(b)
if err != nil {
fmt.Println("write data error, n is", n, "and err is", err)
break
}
}
}
示例5: Start
func (server *TcpServer) Start() (err error) {
for {
var conn *net.TCPConn
if conn, err = server.TCPListener.AcceptTCP(); err != nil {
return err
}
if server.keepAlive != nil {
if err := conn.SetKeepAlive(server.keepAlive.(bool)); err != nil {
return err
}
}
if server.keepAlivePeriod != nil {
if kap, ok := (net.Conn(conn)).(iKeepAlivePeriod); ok {
if err := kap.SetKeepAlivePeriod(server.keepAlivePeriod.(time.Duration)); err != nil {
return err
}
}
}
if server.linger != nil {
if err := conn.SetLinger(server.linger.(int)); err != nil {
return err
}
}
if server.noDelay != nil {
if err := conn.SetNoDelay(server.noDelay.(bool)); err != nil {
return err
}
}
if server.readBuffer != nil {
if err := conn.SetReadBuffer(server.readBuffer.(int)); err != nil {
return err
}
}
if server.writerBuffer != nil {
if err := conn.SetWriteBuffer(server.writerBuffer.(int)); err != nil {
return err
}
}
if server.deadline != nil {
if err := conn.SetDeadline(server.deadline.(time.Time)); err != nil {
return err
}
}
if server.readDeadline != nil {
if err := conn.SetReadDeadline(server.readDeadline.(time.Time)); err != nil {
return err
}
}
if server.writerDeadline != nil {
if err := conn.SetWriteDeadline(server.writerDeadline.(time.Time)); err != nil {
return err
}
}
if server.config != nil {
server.ServeTCP(tls.Client(conn, server.config))
} else {
server.ServeTCP(conn)
}
}
}
示例6: SetDefaultOptions
func (this *GoServer) SetDefaultOptions(conn *net.TCPConn) {
conn.SetKeepAlive(true)
interval, err := time.ParseDuration("45s")
if err != nil {
conn.SetKeepAlivePeriod(interval)
}
conn.SetNoDelay(true)
}
示例7: configTCP
func (o options) configTCP(conn *net.TCPConn) error {
if v, ok := o[mangos.OptionNoDelay]; ok {
if err := conn.SetNoDelay(v.(bool)); err != nil {
return err
}
}
if v, ok := o[mangos.OptionKeepAlive]; ok {
if err := conn.SetKeepAlive(v.(bool)); err != nil {
return err
}
}
return nil
}
示例8: loop
func loop(conn *net.TCPConn) {
defer conn.Close()
conn.SetLinger(5)
conn.SetKeepAlive(false)
conn.SetNoDelay(true)
now := time.Now()
conn.SetReadDeadline(now.Add(readWriteLimit))
if _, err := io.ReadFull(conn, buf); err == nil {
conn.Write(response)
}
}
示例9: setTCPOptions
func setTCPOptions(conn *net.TCPConn) {
var err error
if err = conn.SetLinger(0); err != nil {
l.Infoln(err)
}
if err = conn.SetNoDelay(false); err != nil {
l.Infoln(err)
}
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
l.Infoln(err)
}
if err = conn.SetKeepAlive(true); err != nil {
l.Infoln(err)
}
}
示例10: SetTCPOptions
// SetTCPOptions sets syncthings default TCP options on a TCP connection
func SetTCPOptions(conn *net.TCPConn) error {
var err error
if err = conn.SetLinger(0); err != nil {
return err
}
if err = conn.SetNoDelay(false); err != nil {
return err
}
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
return err
}
if err = conn.SetKeepAlive(true); err != nil {
return err
}
return nil
}
示例11: NewSession
func NewSession(conn *net.TCPConn, rc *turbo.RemotingConfig) *Session {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(rc.IdleTime * 2)
//禁用nagle
conn.SetNoDelay(true)
conn.SetReadBuffer(rc.ReadBufferSize)
conn.SetWriteBuffer(rc.WriteBufferSize)
session := &Session{
conn: conn,
br: bufio.NewReaderSize(conn, rc.ReadBufferSize),
bw: bufio.NewWriterSize(conn, rc.WriteBufferSize),
ReadChannel: make(chan *packet.Packet, rc.ReadChannelSize),
WriteChannel: make(chan *packet.Packet, rc.WriteChannelSize),
isClose: false,
remoteAddr: conn.RemoteAddr().String(),
rc: rc}
return session
}
示例12: ServeTCP
// ServeTCP ...
func (service *TcpService) ServeTCP(conn *net.TCPConn) (err error) {
if service.keepAlive != nil {
if err = conn.SetKeepAlive(service.keepAlive.(bool)); err != nil {
return err
}
}
if service.keepAlivePeriod != nil {
if kap, ok := (net.Conn(conn)).(iKeepAlivePeriod); ok {
if err = kap.SetKeepAlivePeriod(service.keepAlivePeriod.(time.Duration)); err != nil {
return err
}
}
}
if service.linger != nil {
if err = conn.SetLinger(service.linger.(int)); err != nil {
return err
}
}
if service.noDelay != nil {
if err = conn.SetNoDelay(service.noDelay.(bool)); err != nil {
return err
}
}
if service.readBuffer != nil {
if err = conn.SetReadBuffer(service.readBuffer.(int)); err != nil {
return err
}
}
if service.writeBuffer != nil {
if err = conn.SetWriteBuffer(service.writeBuffer.(int)); err != nil {
return err
}
}
if service.config != nil {
tlsConn := tls.Server(conn, service.config)
tlsConn.Handshake()
return service.Serve(tlsConn)
}
return service.Serve(conn)
}
示例13: setupConn
// Setup a connection
func (t *TCPTransport) setupConn(c *net.TCPConn) {
c.SetNoDelay(true)
c.SetKeepAlive(true)
}
示例14: ServeTCP
func (service *TcpService) ServeTCP(conn *net.TCPConn) (err error) {
if service.keepAlive != nil {
if err = conn.SetKeepAlive(service.keepAlive.(bool)); err != nil {
return err
}
}
if service.keepAlivePeriod != nil {
if kap, ok := (net.Conn(conn)).(iKeepAlivePeriod); ok {
if err = kap.SetKeepAlivePeriod(service.keepAlivePeriod.(time.Duration)); err != nil {
return err
}
}
}
if service.linger != nil {
if err = conn.SetLinger(service.linger.(int)); err != nil {
return err
}
}
if service.noDelay != nil {
if err = conn.SetNoDelay(service.noDelay.(bool)); err != nil {
return err
}
}
if service.readBuffer != nil {
if err = conn.SetReadBuffer(service.readBuffer.(int)); err != nil {
return err
}
}
if service.writeBuffer != nil {
if err = conn.SetWriteBuffer(service.writeBuffer.(int)); err != nil {
return err
}
}
if service.timeout != nil {
if err = conn.SetDeadline(time.Now().Add(service.timeout.(time.Duration))); err != nil {
return err
}
}
go func(conn net.Conn) {
if service.config != nil {
tlsConn := tls.Server(conn, service.config)
tlsConn.Handshake()
conn = tlsConn
}
var data []byte
var err error
for {
if service.readTimeout != nil {
err = conn.SetReadDeadline(time.Now().Add(service.readTimeout.(time.Duration)))
}
if err == nil {
data, err = receiveDataOverTcp(conn)
}
if err == nil {
data = service.Handle(data, conn)
if service.writeTimeout != nil {
err = conn.SetWriteDeadline(time.Now().Add(service.writeTimeout.(time.Duration)))
}
if err == nil {
err = sendDataOverTcp(conn, data)
}
}
if err != nil {
conn.Close()
break
}
}
}(conn)
return nil
}
示例15: SetConnParam
func SetConnParam(conn *net.TCPConn) {
conn.SetNoDelay(false)
conn.SetKeepAlive(true)
conn.SetLinger(-1)
}