sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/server/state/normal.go

50 lines
1021 B
Go
Raw Normal View History

2017-12-17 17:50:51 +01:00
package state
import (
2018-02-07 15:34:18 +01:00
"dev.sum7.eu/genofire/yaja/server/extension"
"dev.sum7.eu/genofire/yaja/server/utils"
2017-12-17 17:50:51 +01:00
)
// SendingClient state
type SendingClient struct {
Next State
Client *utils.Client
}
// Process messages
func (state *SendingClient) Process() State {
state.Client.Log = state.Client.Log.WithField("state", "normal")
state.Client.Log.Debug("sending")
// sending
go func() {
select {
case msg := <-state.Client.Messages:
err := state.Client.Out.Encode(msg)
if err != nil {
state.Client.Log.Warn(err)
}
case <-state.Client.OnClose():
return
}
}()
state.Client.Log.Debug("receiving")
return state.Next
}
// ReceivingClient state
type ReceivingClient struct {
Extensions extension.Extensions
Client *utils.Client
}
// Process messages
func (state *ReceivingClient) Process() State {
element, err := state.Client.Read()
if err != nil {
state.Client.Log.Warn("unable to read: ", err)
return nil
}
state.Extensions.Process(element, state.Client)
return state
}