func (s *Session) handleBind(stream conn.Conn, bind *proto.Bind) (err error) { stream.Debug("Binding new tunnel: %v", bind) respond := func(resp *proto.BindResp) { if err = proto.WriteMsg(stream, resp); err != nil { err = stream.Error("Failed to send bind response: %v", err) } } if err = s.hooks.OnBind(s, bind); err != nil { return } t, err := newTunnel(bind, s, s.binders, s.tunnelHooks) if err != nil { respond(&proto.BindResp{Error: err.Error()}) return } t.Info("Registered new tunnel on session %s", s.id) // add it to the list of tunnels s.addTunnel(t) // acknowledge success respond(&proto.BindResp{Url: t.url}) return }
func WriteMsg(c conn.Conn, msg interface{}) (err error) { buffer, err := Pack(msg) if err != nil { return } c.Debug("Writing message: %s", string(buffer)) if err = binary.Write(c, binary.LittleEndian, int64(len(buffer))); err != nil { return } if _, err = c.Write(buffer); err != nil { return } return }
func readMsgShared(c conn.Conn) (buffer []byte, err error) { c.Debug("Waiting to read message") var sz int64 err = binary.Read(c, binary.LittleEndian, &sz) if err != nil { return } c.Debug("Reading message with length: %d", sz) buffer = make([]byte, sz) if _, err = io.ReadFull(c, buffer); err != nil { return } c.Debug("Read message %s", buffer) return }