示例#1
0
文件: handler.go 项目: emersion/miko
// Handle a message of the specified type
func (h *Handler) Handle(t message.Type, conn *message.Conn) error {
	if h.ctx.IsServer() {
		if t != message.Types["version"] && conn.Version == 0 {
			// Client didn't send his version number
			err := conn.Write(func(w io.Writer) error {
				return builder.SendExit(w, message.ExitCodes["client_outdated"])
			})
			if err != nil {
				return err
			}
			return conn.Close()
		}
	}

	if val, ok := h.handlers[t]; ok {
		err := val(h.ctx, conn)

		if err != nil {
			return err
		}
	} else {
		return fmt.Errorf("Unknown message type: %d", t)
	}

	return nil
}
示例#2
0
func TestExit(t *testing.T) {
	code := message.ExitCodes["client_banned"]

	testMessage(t, message.Types["exit"], func(w io.Writer) error {
		return builder.SendExit(w, code)
	}, func(conn *message.Conn, t *testing.T) {
		c := handler.ReadExit(conn)
		if c != code {
			t.Fatal("Sent code", code, "but received", c)
		}
	})
}
示例#3
0
文件: engine.go 项目: emersion/miko
func (e *Engine) Stop() {
	e.running = false

	if e.srv != nil {
		e.brdStop <- true
		e.listenStop <- true
	}

	for _, client := range e.clients {
		client.Write(func(w io.Writer) error {
			return builder.SendExit(w, message.ExitCodes["server_closed"])
		})
		client.Close()
	}
}
示例#4
0
文件: server.go 项目: emersion/miko
}

var serverHandlers = &map[message.Type]TypeHandler{
	message.Types["version"]: func(ctx *message.Context, conn *message.Conn) error {
		conn.Version = ReadVersion(conn)

		if conn.Version != message.CurrentVersion {
			log.Println("Protocol version mismatch")

			code := message.ExitCodes["client_outdated"]
			if conn.Version > message.CurrentVersion {
				code = message.ExitCodes["server_outdated"]
			}

			err := conn.Write(func(w io.Writer) error {
				return builder.SendExit(w, code)
			})
			if err != nil {
				return err
			}

			if err := conn.Close(); err != nil {
				return err
			}
		} else {
			log.Println("Sending config to client")
			conn.State = message.Accepted

			err := conn.Write(func(w io.Writer) error {
				return builder.SendConfig(w, ctx.Config)
			})