コード例 #1
0
ファイル: tcp_service.go プロジェクト: jueebushe/hprose-go
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)
		}
	}
}
コード例 #2
0
ファイル: handler.go プロジェクト: neutony/chatserver
// 发送字节流
func SendByteStream(conn *net.TCPConn, buf []byte) error {
	conn.SetWriteDeadline(time.Now().Add(time.Duration(config.WriteTimeout) * time.Second))
	n, err := conn.Write(buf)
	if n != len(buf) || err != nil {
		return fmt.Errorf("Write to %v failed, Error: %v", ConnMapUuid.Get(conn).(string), err)
	}
	return nil
}
コード例 #3
0
ファイル: handler.go プロジェクト: WaylandGod/gotcp
func SendByteStream(conn *net.TCPConn, buf []byte, writeTimeout time.Duration) error {
	conn.SetWriteDeadline(time.Now().Add(writeTimeout))
	n, err := conn.Write(buf)
	if n != len(buf) || err != nil {
		return fmt.Errorf("Write to %v failed, Error: %v", conn.RemoteAddr(), err)
	}
	return nil
}
コード例 #4
0
ファイル: connector.go プロジェクト: ninsen/siesta
func (dc *DefaultConnector) send(correlationID int32, conn *net.TCPConn, request Request) error {
	writer := NewRequestHeader(correlationID, dc.config.ClientID, request)
	bytes := make([]byte, writer.Size())
	encoder := NewBinaryEncoder(bytes)
	writer.Write(encoder)

	conn.SetWriteDeadline(time.Now().Add(dc.config.WriteTimeout))
	_, err := conn.Write(bytes)
	return err
}
コード例 #5
0
ファイル: selector.go プロジェクト: elodina/stack-deploy
func (s *Selector) send(correlationID int32, conn *net.TCPConn, request siesta.Request) error {
	writer := siesta.NewRequestHeader(correlationID, s.config.ClientID, request)
	bytes := make([]byte, writer.Size())
	encoder := siesta.NewBinaryEncoder(bytes)
	writer.Write(encoder)

	err := conn.SetWriteDeadline(time.Now().Add(s.config.WriteTimeout))
	if err != nil {
		return err
	}
	_, err = conn.Write(bytes)
	return err
}
コード例 #6
0
ファイル: server.go プロジェクト: edgarcai/xserver
func writeBytes(conn *net.TCPConn, buf []byte) error {
	off := 0
	for off != len(buf) {
		if err := conn.SetWriteDeadline(time.Now().Add(time.Second * 10)); err != nil {
			return err
		}
		if n, err := conn.Write(buf[off:]); err != nil {
			return err
		} else {
			off += n
		}
	}
	return nil
}
コード例 #7
0
ファイル: tcp_server.go プロジェクト: hechel/linbox
func sendAuthResponse(conn *net.TCPConn, msg []byte) error {
	now := time.Now()
	timeout := now.Add(time.Second * time.Duration(60))

	conn.SetWriteDeadline(timeout)

	_, err := conn.Write(msg)
	if err != nil {
		logger.Errorf("Send AuthResponse Fail. AuthResponseBuf: %+v. Error: %s", msg, err)
		return err
	}

	return nil
}
コード例 #8
0
ファイル: channel.go プロジェクト: ZhangTingkuo/im_service
func (channel *Channel) RunOnce(conn *net.TCPConn) {
	defer conn.Close()

	closed_ch := make(chan bool)
	seq := 0
	seq = channel.ReSubscribe(conn, seq)
	seq = channel.ReSubscribeRoom(conn, seq)

	go func() {
		for {
			msg := ReceiveMessage(conn)
			if msg == nil {
				close(closed_ch)
				return
			}
			log.Info("channel recv message:", Command(msg.cmd))
			if msg.cmd == MSG_PUBLISH {
				amsg := msg.body.(*AppMessage)
				if channel.dispatch != nil {
					channel.dispatch(amsg)
				}
			} else if msg.cmd == MSG_PUBLISH_ROOM {
				amsg := msg.body.(*AppMessage)
				if channel.dispatch_room != nil {
					channel.dispatch_room(amsg)
				}
			} else {
				log.Error("unknown message cmd:", msg.cmd)
			}
		}
	}()

	for {
		select {
		case _ = <-closed_ch:
			log.Info("channel closed")
			return
		case msg := <-channel.wt:
			seq = seq + 1
			msg.seq = seq
			conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
			err := SendMessage(conn, msg)
			if err != nil {
				log.Info("channel send message:", err)
			}
		}
	}
}
コード例 #9
0
ファイル: tcpudp.go プロジェクト: xiaobagou/echo
func handleTCPConn(tcpconn *net.TCPConn, encryptKey []byte, buffer *bytes.Buffer, bpool *bpool.SizedBufferPool) {
	defer tcpconn.Close()
	defer bpool.Put(buffer)
	var receiveData []byte
	//tcpconn need read all data in 20 second ,otherwise Timeout() will be true
	tcpconn.SetReadDeadline(time.Now().Add(time.Duration(20) * time.Second))
	bufReader := bufio.NewReader(tcpconn)
	for {
		rData, err := bufReader.ReadBytes(',')
		if err != nil {
			if err == io.EOF {
				log.Printf("TCPConn Read error\n")
				return
			}
			buffer.Write(rData)
			continue
		}

		buffer.Write(rData)

		receiveData, err = netstring.Unmarshall(buffer.Bytes())
		if err != nil {
			if err == netstring.ErrNsLenNotEqaulOrgLen {
				continue
			} else {
				log.Printf("netstring unmarshall error : %v\n", err)
				return
			}
		}

		break
	}

	_, err := libsodium.DecryptData(encryptKey, receiveData)
	if err != nil {
		log.Printf("tcp DecryptData error : %v\n", err)
		return
	}

	tcpconn.SetWriteDeadline(time.Now().Add(time.Duration(20) * time.Second))
	_, err = tcpconn.Write(netstring.Marshall([]byte(homeip)))
	if err != nil {
		log.Printf("tcpconn error : %v\n", err)
	}
}
コード例 #10
0
ファイル: rawstreamer.go プロジェクト: xthexder/rawstreamer
func writeAligned(conn *net.TCPConn, buf []byte, timeout time.Time) error {
	conn.SetWriteDeadline(timeout)
	n, err := conn.Write(buf)
	if err != nil {
		if err2, ok := err.(*net.OpError); ok && err2.Timeout() {
			// Add extra padding to make sure we're still aligned
			align := n % (bits / 4)
			if align > 0 {
				fmt.Printf("Realigning: %d + %d\n", n, (bits/4)-align)
				conn.SetWriteDeadline(time.Time{})
				_, err = conn.Write(make([]byte, (bits/4)-align))
				if err != nil {
					return err
				}
			}
			return ErrWriteTimeout
		}
	}
	return err
}
コード例 #11
0
ファイル: server.go プロジェクト: temoto/dlock
func (server *Server) addConnection(tcpConn *net.TCPConn) *Connection {
	clientId := tcpConn.RemoteAddr().String()
	var err error
	if server.ConfigDebug {
		log.Printf("Server.addConnection: %s", clientId)
	}

	// TODO: set expected clientLocks to average+1
	if err = server.initClientLocks(clientId, 1); err != nil {
		log.Printf("Server.addConnection: %s %s", clientId, err.Error())
		return nil
	}

	if err := server.setupSocket(tcpConn); err != nil {
		log.Printf("Server.addConnection: %s setupSocket error: %s", clientId, err.Error())
		return nil
	}

	conn := NewConnection(server, clientId)
	conn.handlers = map[dlock.RequestType]HandlerFunc{
		dlock.RequestType_Ping: handlePing,
		dlock.RequestType_Lock: handleLock,
	}
	conn.funClose = tcpConn.Close
	conn.funResetIdleTimeout = func() error { return tcpConn.SetReadDeadline(time.Now().Add(server.ConfigIdleTimeout)) }
	conn.funResetReadTimeout = func() error { return tcpConn.SetReadDeadline(time.Now().Add(server.ConfigReadTimeout)) }
	conn.funResetWriteTimeout = func() error { return tcpConn.SetWriteDeadline(time.Now().Add(server.ConfigWriteTimeout)) }

	if server.ConfigReadBuffer == 0 {
		conn.r = bufio.NewReader(tcpConn)
	} else {
		conn.r = bufio.NewReaderSize(tcpConn, int(server.ConfigReadBuffer))
	}
	conn.w = bufio.NewWriter(tcpConn)

	server.wg.Add(1)
	go conn.loop()

	return conn
}
コード例 #12
0
ファイル: tcp_server.go プロジェクト: hechel/linbox
func handleSendingMsg(conn *net.TCPConn, userid uint64, shouldQuit chan bool) {
	defer conn.Close()

	channel := make(chan []byte, sendingChannelSize)
	quit := make(chan bool, 1)

	defer func() {
		quit <- true
		close(quit)

		logger.Info("Close sending connection for user %d", userid)
	}()

	connService.HandleSendingMsg(userid, channel, quit)

loop:
	for {
		select {
		case message, more := <-channel:
			if !more {
				logger.Errorf("The sending channel is closed by connector service. Close connection")
				break loop
			}

			now := time.Now()
			timeout := now.Add(time.Second * time.Duration(writeTimeoutInSecond))
			conn.SetWriteDeadline(timeout)

			_, err := conn.Write(message)

			if err != nil {
				logger.Errorf("Sending messages for user %d Error. Close the connection. Error: %s", userid, err)
				break loop
			}
		case <-shouldQuit:
			break loop
		}
	}
}
コード例 #13
0
ファイル: tcp_service.go プロジェクト: kennylixi/hprose-go
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
}
コード例 #14
0
ファイル: tcp_service.go プロジェクト: JREAMLU/hprose-go
func (server *TcpServer) handle() (err error) {
	defer func() {
		if e := recover(); e != nil && err == nil {
			err = fmt.Errorf("%v", e)
		}
	}()
	if server.listener == nil {
		return nil
	}
	var conn *net.TCPConn
	if conn, err = server.listener.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)
	}
	return nil
}