当前位置: 首页>>代码示例>>Golang>>正文


Golang byteio.StickyWriter类代码示例

本文整理汇总了Golang中github.com/MJKWoolnough/byteio.StickyWriter的典型用法代码示例。如果您正苦于以下问题:Golang StickyWriter类的具体用法?Golang StickyWriter怎么用?Golang StickyWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了StickyWriter类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
	r := byteio.StickyReader{Reader: &byteio.LittleEndianReader{os.Stdin}}
	w := byteio.StickyWriter{Writer: &byteio.LittleEndianWriter{os.Stdout}}
	if err := generate(&r, &w, os.NewFile(3, "data.ora")); err != nil {
		w.WriteUint8(0)
		data.WriteString(&w, err.Error())
		os.Exit(1)
	}
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:9,代码来源:main.go

示例2: generate

func generate(r *byteio.StickyReader, w *byteio.StickyWriter, of *os.File) error {
	memoryLimit := r.ReadUint64()
	size := r.ReadInt64()
	gPath := data.ReadString(r)
	levelName := data.ReadString(r)
	mapPath := data.ReadString(r)
	if r.Err != nil {
		return r.Err
	}
	o, err := ora.Open(of, size)
	if err != nil {
		return err
	}
	f, err := os.Open(path.Join(gPath, "data.gen"))
	if err != nil {
		return err
	}
	g, err := LoadGenerator(f)
	if e := f.Close(); e != nil {
		return e
	}
	if err != nil {
		return err
	}

	b := o.Bounds()
	w.WriteUint8(2)
	w.WriteInt32(int32(b.Max.X) >> 4)
	w.WriteInt32(int32(b.Max.Y) >> 4)

	c := make(chan paint, 1024)
	m := make(chan string, 4)
	e := make(chan struct{}, 0)
	go func() {
		defer close(e)
		defer close(m)
		defer close(c)
		for {
			select {
			case message := <-m:
				w.WriteUint8(3)
				data.WriteString(w, message)
			case p := <-c:
				w.WriteUint8(4)
				w.WriteInt32(p.X)
				w.WriteInt32(p.Y)
				r, g, b, a := p.RGBA()
				w.WriteUint8(uint8(r >> 8))
				w.WriteUint8(uint8(g >> 8))
				w.WriteUint8(uint8(b >> 8))
				w.WriteUint8(uint8(a >> 8))
			case <-e:
				return
			}
		}
	}()

	err = g.Generate(levelName, mapPath, o, c, m, memoryLimit)

	e <- struct{}{}
	<-e
	return err
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:63,代码来源:main.go

示例3: writeError

func writeError(w *byteio.StickyWriter, err error) {
	w.WriteUint8(0)
	data.WriteString(w, err.Error())
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:4,代码来源:utils.go

示例4: transferFile

func transferFile(typeName, method string, typeID uint8, o *overlay.Overlay) dom.Node {
	name := xform.InputText("name", "")
	url := xform.InputRadio("url", "switch", true)
	upload := xform.InputRadio("upload", "switch", false)
	fileI := xform.InputUpload("")
	urlI := xform.InputURL("", "")
	s := xform.InputSubmit(method)

	name.Required = true

	typeFunc := func(dom.Event) {
		if url.Checked {
			urlI.Style().RemoveProperty("display")
			fileI.Style().SetProperty("display", "none", "")
			urlI.Required = true
			fileI.Required = false
			fileI.SetID("")
			urlI.SetID("file")
		} else {
			fileI.Style().RemoveProperty("display")
			urlI.Style().SetProperty("display", "none", "")
			fileI.Required = true
			urlI.Required = false
			urlI.SetID("")
			fileI.SetID("file")
		}
	}

	typeFunc(nil)

	url.AddEventListener("change", false, typeFunc)
	upload.AddEventListener("change", false, typeFunc)

	f := xjs.AppendChildren(xdom.Form(), xjs.AppendChildren(xdom.Fieldset(),
		xjs.SetInnerText(xdom.Legend(), method+" "+typeName),
		xform.Label(typeName+" Name", "name"),
		name,
		xdom.Br(),

		xform.Label("URL", "url"),
		url,
		xdom.Br(),

		xform.Label("Upload", "upload"),
		upload,
		xdom.Br(),

		xform.Label("File", "file"),

		fileI,
		urlI,

		xdom.Br(),
		s,
	))

	s.AddEventListener("click", false, func(e dom.Event) {
		if name.Value == "" {
			return
		}
		if url.Checked {
			if urlI.Value == "" {
				return
			}
		} else if len(fileI.Files()) != 1 {
			return

		}
		s.Disabled = true
		name.Disabled = true
		url.Disabled = true
		upload.Disabled = true
		fileI.Disabled = true
		urlI.Disabled = true
		e.PreventDefault()
		go func() {
			d := xdom.Div()
			uo := overlay.New(d)
			uo.OnClose(func() {
				o.Close()
			})
			xjs.Body().AppendChild(uo)
			status := xdom.Div()
			d.AppendChild(xjs.SetInnerText(status, "Transferring..."))
			conn, err := websocket.Dial("ws://" + js.Global.Get("location").Get("host").String() + "/transfer")
			if err != nil {
				xjs.SetInnerText(status, err.Error())
				return
			}
			defer conn.Close()
			w := byteio.StickyWriter{Writer: byteio.LittleEndianWriter{Writer: conn}}
			r := byteio.StickyReader{Reader: byteio.LittleEndianReader{Reader: conn}}

			pb := progress.New(color.RGBA{255, 0, 0, 0}, color.RGBA{0, 0, 255, 0}, 400, 50)
			d.AppendChild(pb)

			if url.Checked {
				w.WriteUint8(typeID << 1)
				data.WriteString(&w, urlI.Value)
				length := int(r.ReadInt32())
//.........这里部分代码省略.........
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:101,代码来源:utils.go

示例5: handle

func (c Console) handle(r *byteio.StickyReader, w *byteio.StickyWriter) error {
	id := int(r.ReadInt32())
	if r.Err != nil {
		return r.Err
	}
	s := c.c.c.Server(id)
	if s == nil {
		return ErrUnknownServer
	}
	s.RLock() //Needed? Path never gets changed!
	p := s.Path
	s.RUnlock()

	var (
		f       *os.File
		err     error
		logPath string
	)

	for _, lp := range logPaths {
		logPath = path.Join(p, lp)
		f, err = os.Open(logPath)
		if err == nil {
			break
		}
	}
	if f == nil {
		return errors.New("unable to open log file")
	}
	defer func() {
		f.Close()
	}()

	fsw, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	logDir := path.Dir(logPath)
	fsw.Add(logDir)
	defer fsw.Remove(logDir)

	pw := partWriter{w}

	io.Copy(pw, f)

	if w.Err != nil {
		return w.Err
	}
	t := time.NewTimer(time.Second * 10)
	for {
		select {
		case ev := <-fsw.Events:
			switch ev.Op {
			case fsnotify.Create:
				if ev.Name == logPath {
					f, err = os.Open(logPath)
					if err != nil {
						return err
					}
				}
			case fsnotify.Write:
				if ev.Name == logPath {
					io.Copy(pw, f)
					if w.Err != nil {
						return w.Err
					}
				}
			case fsnotify.Remove:
				if ev.Name == logPath {
					f.Close()
				}
			}
		case err = <-fsw.Errors:
		case <-t.C:
			w.WriteUint8(2) //ping
			if w.Err != nil {
				return w.Err
			}
		}
		t.Reset(time.Second * 10)
	}
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:82,代码来源:console.go

示例6: server

func (t Transfer) server(name string, r *byteio.StickyReader, w *byteio.StickyWriter, f *os.File, size int64) error {
	zr, err := zip.NewReader(f, size)
	if err != nil {
		return err
	}
	jars := make([]*zip.File, 0, 16)
	for _, file := range zr.File {
		if file.Name == "server.jar" {
			jars = []*zip.File{file}
			break
		}
		if strings.HasSuffix(file.Name, ".jar") {
			jars = append(jars, file)
		}
	}
	s := t.c.NewServer()
	done := false
	defer func() {
		if !done {
			t.c.RemoveServer(s.ID)
		}
		go t.c.Save()
	}()
	if s == nil {
		return errors.New("error creating server")
	}
	s.Lock()
	s.Name = name
	d := s.Path
	s.Unlock()
	if len(jars) == 0 {
		err = moveFile(f.Name(), path.Join(d, "server.jar"))
	} else {
		if len(jars) > 1 {
			w.WriteUint8(1)
			w.WriteInt16(int16(len(jars)))
			for _, jar := range jars {
				data.WriteString(w, jar.Name)
			}
			if w.Err != nil {
				return w.Err
			}
			p := r.ReadInt16()
			if r.Err != nil {
				return r.Err
			}
			if int(p) >= len(jars) || p < 0 {
				return errors.New("error selecting server jar")
			}
			jars[0] = jars[p]
		}
		if err == nil {
			err = unzip(zr, d)
			if err == nil {
				err = os.Rename(path.Join(d, jars[0].Name), path.Join(d, "server.jar"))
			}
		}
	}
	if err != nil {
		return err
	}
	serverProperties := DefaultServerSettings()
	ps, err := os.OpenFile(path.Join(d, "properties.server"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
	defer ps.Close()
	if err != nil {
		if !os.IsExist(err) {
			return err
		}
	} else {
		err = serverProperties.WriteTo(ps)
		if err != nil {
			return err
		}
	}
	done = true
	return nil
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:77,代码来源:transfer_server.go

示例7: serverConsole

func serverConsole(s data.Server) func(dom.Element) {
	return func(c dom.Element) {
		log := xform.TextArea("log", "")
		log.ReadOnly = true
		command := xform.InputText("command", "")
		command.Required = true
		send := xform.InputSubmit("Send")
		c.AppendChild(xjs.AppendChildren(xdom.Form(), xjs.AppendChildren(xdom.Fieldset(),
			xjs.SetInnerText(xdom.Legend(), "Console"),
			xform.Label("Log", ""), log, xdom.Br(),
			xform.Label("Command", "command"), command, send,
		)))
		if s.State == data.StateStopped {
			send.Disabled = true
			command.Disabled = true
		} else {
			send.AddEventListener("click", false, func(e dom.Event) {
				if command.Value == "" {
					return
				}
				e.PreventDefault()
				send.Disabled = true
				cmd := command.Value
				log.Value += "\n>" + cmd + "\n"
				log.Set("scrollTop", log.Get("scrollHeight"))
				command.Value = ""
				go func() {
					err := RPC.WriteCommand(s.ID, cmd)
					if err != nil {
						xjs.Alert("Error sending command: %s", err)
						return
					}
					send.Disabled = false
				}()
			})
		}
		go func() {
			conn, err := websocket.Dial("ws://" + js.Global.Get("location").Get("host").String() + "/console")
			if err != nil {
				xjs.Alert("Failed to connect to console: %s", err)
				return
			}
			defer conn.Close()
			w := byteio.StickyWriter{Writer: byteio.LittleEndianWriter{Writer: conn}}
			r := byteio.StickyReader{Reader: byteio.LittleEndianReader{Reader: conn}}
			updateStop := make(chan struct{})
			registerUpdateStopper(c, updateStop)
			done := false
			go func() {
				<-updateStop
				done = true
				conn.Close()
			}()
			w.WriteInt32(int32(s.ID))
			for {
				state := r.ReadUint8()
				switch state {
				case 0:
					if !done {
						err := ReadError(&r)
						if r.Err != nil {
							err = r.Err
						}
						log.Value += "\n\nError reading from console: " + err.Error()
						log.Set("scrollTop", log.Get("scrollHeight"))
					}
					return
				case 1:
					log.Value += data.ReadString(&r)
					log.Set("scrollTop", log.Get("scrollHeight"))
				}
			}
		}()
	}
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:75,代码来源:servers.go

示例8: generator

func (t Transfer) generator(name string, r *byteio.StickyReader, w *byteio.StickyWriter, f *os.File, size int64) error {
	g := t.c.Generators.New(t.c.Settings().DirGenerators)
	if g == nil {
		return errors.New("error creating generator")
	}
	g.Name = name

	done := false
	defer func() {
		if !done {
			t.c.RemoveGenerator(g.ID)
		}
		go t.c.Save()
	}()

	zr, err := zip.NewReader(f, size)
	if err != nil {
		f.Seek(0, 0)
		e := json.NewDecoder(f).Decode(new(data.GeneratorData))
		if e != nil {
			return err
		}
		err = moveFile(f.Name(), path.Join(g.Path, "data.gen"))
		if err != nil {
			return err
		}

		done = true
		return nil
	}

	gens := make([]*zip.File, 0, 16)
	for _, file := range zr.File {
		if file.Name == "data.gen" {
			gens = []*zip.File{file}
			break
		}
		if strings.HasSuffix(file.Name, ".gen") || strings.HasSuffix(file.Name, ".json") {
			gens = append(gens, file)
		}
	}

	if len(gens) == 0 {
		return errors.New("cannot find generator data in zip")
	}
	if len(gens) > 1 {
		w.WriteUint8(1)
		w.WriteInt16(int16(len(gens)))
		for _, gen := range gens {
			data.WriteString(w, gen.Name)
		}
		if w.Err != nil {
			return w.Err
		}
		p := r.ReadInt16()
		if r.Err != nil {
			return r.Err
		}
		if int(p) >= len(gens) || p < 0 {
			return errors.New("error selecting generator data")
		}
		gens[0] = gens[p]
	}

	gd, err := gens[0].Open()
	if err != nil {
		return err
	}

	err = json.NewDecoder(gd).Decode(new(data.GeneratorData))
	if err != nil {
		return err
	}

	err = unzip(zr, g.Path)
	if err != nil {
		return err
	}

	err = os.Rename(path.Join(g.Path, gens[0].Name), path.Join(g.Path, "data.gen"))
	if err != nil {
		return err
	}

	done = true
	return nil
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:87,代码来源:transfer_generator.go

示例9: generate

func (t Transfer) generate(name string, r *byteio.StickyReader, w *byteio.StickyWriter, f *os.File, size int64) error {
	gp.Add(1)
	defer gp.Done()
	mp := t.c.NewMap()
	if mp == nil {
		return errors.New("failed to create map")
	}

	done := false
	defer func() {
		if !done {
			t.c.RemoveMap(mp.ID)
		}
		t.c.Save()
	}()

	mp.Lock()
	mp.Name = name
	mapPath := mp.Path
	mp.Server = -2
	mp.Unlock()

	t.c.Generators.mu.RLock()
	gs := make([]data.Generator, len(t.c.Generators.List))
	for n, g := range t.c.Generators.List {
		gs[n] = *g
	}
	t.c.Generators.mu.RUnlock()
	var g data.Generator
	if len(gs) == 0 {
		return errors.New("no generators installed")
	} else if len(gs) == 1 {
		g = gs[0]
	} else {
		w.WriteUint8(1)
		w.WriteInt16(int16(len(gs)))
		for _, tg := range gs {
			data.WriteString(w, tg.Name)
		}
		if w.Err != nil {
			return w.Err
		}
		gID := r.ReadInt16()
		if r.Err != nil {
			return r.Err
		}

		if gID < 0 || int(gID) >= len(gs) {
			return errors.New("unknown generator")
		}
		g = gs[gID]
	}

	ms := DefaultMapSettings()
	ms["level-type"] = minecraft.FlatGenerator
	ms["generator-settings"] = "0"
	ms["motd"] = name

	j, err := os.Open(path.Join(g.Path, "data.gen"))
	if err != nil {
		return err
	}
	var gj data.GeneratorData
	err = json.NewDecoder(j).Decode(&gj)
	j.Close()
	if err != nil {
		return err
	}

	for k, v := range gj.Options {
		ms[k] = v
	}

	pf, err := os.Create(path.Join(mapPath, "properties.map"))
	if err != nil {
		return err
	}

	if err = ms.WriteTo(pf); err != nil {
		return err
	}
	pf.Close()

	cmd := exec.Command(t.c.Settings().GeneratorExecutable)
	cmd.ExtraFiles = append(cmd.ExtraFiles, f)
	cmd.Dir, err = os.Getwd()
	if err != nil {
		return err
	}
	cmd.Stdout = w
	pw, err := cmd.StdinPipe()
	if err != nil {
		return err
	}

	err = gp.Start(cmd)
	if err != nil {
		return err
	}
	defer gp.Remove(cmd)
//.........这里部分代码省略.........
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:101,代码来源:transfer_generate.go

示例10: WriteString

func WriteString(w *byteio.StickyWriter, s string) {
	w.WriteUint16(uint16(len(s)))
	w.Write([]byte(s))
}
开发者ID:MJKWoolnough,项目名称:minewebgen,代码行数:4,代码来源:utils.go


注:本文中的github.com/MJKWoolnough/byteio.StickyWriter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。