Let's say we have the following abstraction for a server (XMPP, but here it doesn't matter a lot):
type Server struct {
Addr string
Conn net.Conn
tlsCon *tls.Conn
R *bufio.Reader
SSL bool
reader chan string
}
And a helper function to init it:
func createServer(domain string) (s *Server, err error) {
conn, err := net.Dial("tcp", domain+":5222")
if err != nil {
return nil, err
}
s = &Server{domain, conn, nil, bufio.NewReader(conn), false, make(chan string, 8)}
go func(t *Server) {
for {
buf := bufsPool.Get().([]byte)
n, err := s.R.Read(buf)
if err == nil {
s.reader <- string(buf[:n])
} else {
fmt.Println(err)
}
}
}(s)
return s, err
}
The idea is simple: create a connection and, if everything went well, get a buffered reader for it (I don't use the conn.read function just because, if server requires, I start TLS connection and reassign R to reader created based of it, but now this isn't the case).
Now we have the two functions, write and read:
func (s *Server) read() (t string) {
t = ""
Inn:
for {
select {
case u := <-s.reader:
fmt.Println("debug", u)
t += u
default:
break Inn
}
}
return t
}
So I want the read function to receive data sent by the goroutine which reads from socket (the one started in createServer()) from chan . The idea is to call write and than read the response. I created all of this because the server some times sends response as two parts, e.g. I have to do traditional read() 2 times. But this didn't work, my read function (see above) simply returns nothing. Most probably, that's because the server doesn't manage to send back the data and my function exits because there's nothing in the chan. But one concern is that although I call write and read multiple times, read always returns nothing.
So I guess I have some general design error and the question is if the community can help me find it. Thanks.