// auth for goim handshake with client, use rsa & aes. func (server *Server) authTCP(rr *bufio.Reader, wr *bufio.Writer, p *proto.Proto) (key string, rid int32, heartbeat time.Duration, err error) { if err = p.ReadTCP(rr); err != nil { return } if p.Operation != define.OP_AUTH { log.Warn("auth operation not valid: %d", p.Operation) err = ErrOperation return } if key, rid, heartbeat, err = server.operator.Connect(p); err != nil { return } p.Body = nil p.Operation = define.OP_AUTH_REPLY if err = p.WriteTCP(wr); err != nil { return } err = wr.Flush() return }
// TODO linger close? func (server *Server) serveTCP(conn *net.TCPConn, rp, wp *bytes.Pool, tr *itime.Timer) { var ( err error key string hb time.Duration // heartbeat p *proto.Proto b *Bucket trd *itime.TimerData rb = rp.Get() wb = wp.Get() ch = NewChannel(server.Options.CliProto, server.Options.SvrProto, define.NoRoom) rr = &ch.Reader wr = &ch.Writer ) ch.Reader.ResetBuffer(conn, rb.Bytes()) ch.Writer.ResetBuffer(conn, wb.Bytes()) // handshake trd = tr.Add(server.Options.HandshakeTimeout, func() { conn.Close() }) // must not setadv, only used in auth if p, err = ch.CliProto.Set(); err == nil { if key, ch.RoomId, hb, err = server.authTCP(rr, wr, p); err == nil { b = server.Bucket(key) err = b.Put(key, ch, tr) } } if err != nil { conn.Close() rp.Put(rb) wp.Put(wb) tr.Del(trd) log.Error("key: %s handshake failed error(%v)", key, err) return } trd.Key = key tr.Set(trd, hb) // hanshake ok start dispatch goroutine go server.dispatchTCP(key, conn, wr, wp, wb, ch) for { if p, err = ch.CliProto.Set(); err != nil { break } if err = p.ReadTCP(rr); err != nil { break } //p.Time = *globalNowTime if p.Operation == define.OP_HEARTBEAT { tr.Set(trd, hb) p.Body = nil p.Operation = define.OP_HEARTBEAT_REPLY if Debug { log.Debug("key: %s receive heartbeat", key) } } else { if err = server.operator.Operate(p); err != nil { break } } ch.CliProto.SetAdv() ch.Signal() } if err != nil && err != io.EOF { log.Error("key: %s server tcp failed error(%v)", key, err) } b.Del(key) tr.Del(trd) rp.Put(rb) conn.Close() ch.Close() if err = server.operator.Disconnect(key, ch.RoomId); err != nil { log.Error("key: %s operator do disconnect error(%v)", key, err) } if Debug { log.Debug("key: %s server tcp goroutine exit", key) } return }