Esempio n. 1
0
func NewConn(c net.Conn) Conn {
	conn := &conn{
		sendMessageQueue:    make(chan protocol.Message, DEFAULT_SEND_BUFFER_SIZE),
		receiveMessageQueue: make(chan protocol.Message, DEFAULT_RECEIVE_BUFFER_SIZE),

		c:    c,
		br:   bufio.NewReader(c),
		bw:   bufio.NewWriter(c),
		addr: fmt.Sprintf("%s_%s", c.LocalAddr().String(), c.RemoteAddr().String()),

		latestActiveTime: time.Now(),

		closed: datatypes.NewSyncClose(),
	}
	go conn.sendLoop()
	go conn.readLoop()
	go conn.handleLoop()
	addConn(conn.addr, conn)
	return conn
}
Esempio n. 2
0
func NewConn(address string, connectTimeout, readTimeout, writeTimeout time.Duration) (c Conn, err error) {
	var (
		clientSocket *socket.TClientSocket
		clientConn   net.Conn
	)
	if clientSocket, err = socket.NewTClientSocket(address); err != nil {
		return
	}
	if clientConn, err = clientSocket.DialTimeout(connectTimeout); err != nil {
		return
	}
	c = &conn{
		c:            clientConn,
		br:           bufio.NewReader(clientConn),
		bw:           bufio.NewWriter(clientConn),
		readTimeout:  readTimeout,
		writeTimeout: writeTimeout,
		flag:         0,
		closed:       datatypes.NewSyncClose(),
	}
	return
}