remove code duplication.

This commit is contained in:
Miroslav Šedivý 2020-11-16 16:56:03 +01:00
parent db706097f6
commit 234b0a2e06

View File

@ -77,101 +77,93 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
return err
}
var err error
switch header.Event {
// Signal Events
case event.SIGNAL_ANSWER:
payload := &message.SignalAnswer{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.signalAnswer(session, payload)
}), "%s failed", header.Event)
})
// Control Events
case event.CONTROL_RELEASE:
return errors.Wrapf(h.controlRelease(session), "%s failed", header.Event)
err = h.controlRelease(session)
case event.CONTROL_REQUEST:
return errors.Wrapf(h.controlRequest(session), "%s failed", header.Event)
err = h.controlRequest(session)
case event.CONTROL_GIVE:
payload := &message.Control{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.controlGive(session, payload)
}), "%s failed", header.Event)
})
case event.CONTROL_CLIPBOARD:
payload := &message.Clipboard{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.controlClipboard(session, payload)
}), "%s failed", header.Event)
})
case event.CONTROL_KEYBOARD:
payload := &message.Keyboard{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.controlKeyboard(session, payload)
}), "%s failed", header.Event)
})
// Screen Events
case event.SCREEN_RESOLUTION:
return errors.Wrapf(h.screenResolution(session), "%s failed", header.Event)
err = h.screenResolution(session)
case event.SCREEN_CONFIGURATIONS:
return errors.Wrapf(h.screenConfigurations(session), "%s failed", header.Event)
err = h.screenConfigurations(session)
case event.SCREEN_SET:
payload := &message.ScreenResolution{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.screenSet(session, payload)
}), "%s failed", header.Event)
})
// Boradcast Events
case event.BORADCAST_CREATE:
payload := &message.BroadcastCreate{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.boradcastCreate(session, payload)
}), "%s failed", header.Event)
})
case event.BORADCAST_DESTROY:
return errors.Wrapf(h.boradcastDestroy(session), "%s failed", header.Event)
err = h.boradcastDestroy(session)
// Admin Events
case event.ADMIN_LOCK:
return errors.Wrapf(h.adminLock(session), "%s failed", header.Event)
err = h.adminLock(session)
case event.ADMIN_UNLOCK:
return errors.Wrapf(h.adminUnlock(session), "%s failed", header.Event)
err = h.adminUnlock(session)
case event.ADMIN_CONTROL:
return errors.Wrapf(h.adminControl(session), "%s failed", header.Event)
err = h.adminControl(session)
case event.ADMIN_RELEASE:
return errors.Wrapf(h.adminRelease(session), "%s failed", header.Event)
err = h.adminRelease(session)
case event.ADMIN_GIVE:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.adminGive(session, payload)
}), "%s failed", header.Event)
})
case event.ADMIN_BAN:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.adminBan(session, payload)
}), "%s failed", header.Event)
})
case event.ADMIN_KICK:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.adminKick(session, payload)
}), "%s failed", header.Event)
})
case event.ADMIN_MUTE:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.adminMute(session, payload)
}), "%s failed", header.Event)
})
case event.ADMIN_UNMUTE:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
err = utils.Unmarshal(payload, raw, func() error {
return h.adminUnmute(session, payload)
}), "%s failed", header.Event)
})
default:
return errors.Errorf("unknown message event %s", header.Event)
}
return errors.Wrapf(err, "%s failed", header.Event)
}