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.
2017-12-17 13:31:02 +01:00
|
|
|
package toclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/genofire/yaja/server/extension"
|
|
|
|
"github.com/genofire/yaja/server/state"
|
|
|
|
"github.com/genofire/yaja/server/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SendingClient state
|
|
|
|
type SendingClient struct {
|
|
|
|
Next state.State
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process messages
|
|
|
|
func (state *SendingClient) Process(client *utils.Client) (state.State, *utils.Client) {
|
|
|
|
client.Log = client.Log.WithField("state", "normal")
|
|
|
|
client.Log.Debug("sending")
|
|
|
|
// sending
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case msg := <-client.Messages:
|
|
|
|
err := client.Out.Encode(msg)
|
2017-12-17 15:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
client.Log.Warn(err)
|
|
|
|
}
|
2017-12-17 13:31:02 +01:00
|
|
|
case <-client.OnClose():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
client.Log.Debug("receiving")
|
|
|
|
return state.Next, client
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceivingClient state
|
|
|
|
type ReceivingClient struct {
|
|
|
|
Extensions extension.Extensions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process messages
|
|
|
|
func (state *ReceivingClient) Process(client *utils.Client) (state.State, *utils.Client) {
|
|
|
|
element, err := client.Read()
|
|
|
|
if err != nil {
|
|
|
|
client.Log.Warn("unable to read: ", err)
|
|
|
|
return nil, client
|
|
|
|
}
|
|
|
|
state.Extensions.Process(element, client)
|
|
|
|
return state, client
|
|
|
|
}
|