| 1 |
package controlproto |
| 2 |
|
| 3 |
import "github.com/centrifugal/centrifuge/internal/controlpb" |
| 4 |
|
| 5 |
// Decoder ... |
| 6 |
type Decoder interface { |
| 7 |
DecodeCommand([]byte) (*controlpb.Command, error) |
| 8 |
DecodeNode([]byte) (*controlpb.Node, error) |
| 9 |
DecodeUnsubscribe([]byte) (*controlpb.Unsubscribe, error) |
| 10 |
DecodeDisconnect([]byte) (*controlpb.Disconnect, error) |
| 11 |
DecodeSurveyRequest([]byte) (*controlpb.SurveyRequest, error) |
| 12 |
DecodeSurveyResponse([]byte) (*controlpb.SurveyResponse, error) |
| 13 |
} |
| 14 |
|
| 15 |
var _ Decoder = (*ProtobufDecoder)(nil) |
| 16 |
|
| 17 |
// ProtobufDecoder ... |
| 18 |
type ProtobufDecoder struct{} |
| 19 |
|
| 20 |
// NewProtobufDecoder ... |
| 21 |
func NewProtobufDecoder() *ProtobufDecoder { |
| 22 |
return &ProtobufDecoder{} |
| 23 |
} |
| 24 |
|
| 25 |
// DecodeCommand ... |
| 26 |
func (e *ProtobufDecoder) DecodeCommand(data []byte) (*controlpb.Command, error) { |
| 27 |
var cmd controlpb.Command |
| 28 |
err := cmd.Unmarshal(data) |
| 29 |
if err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
return &cmd, nil |
| 33 |
} |
| 34 |
|
| 35 |
// DecodeNode ... |
| 36 |
func (e *ProtobufDecoder) DecodeNode(data []byte) (*controlpb.Node, error) { |
| 37 |
var cmd controlpb.Node |
| 38 |
err := cmd.Unmarshal(data) |
| 39 |
if err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
return &cmd, nil |
| 43 |
} |
| 44 |
|
| 45 |
// DecodeUnsubscribe ... |
| 46 |
func (e *ProtobufDecoder) DecodeUnsubscribe(data []byte) (*controlpb.Unsubscribe, error) { |
| 47 |
var cmd controlpb.Unsubscribe |
| 48 |
err := cmd.Unmarshal(data) |
| 49 |
if err != nil { |
| 50 |
return nil, err |
| 51 |
} |
| 52 |
return &cmd, nil |
| 53 |
} |
| 54 |
|
| 55 |
// DecodeDisconnect ... |
| 56 |
func (e *ProtobufDecoder) DecodeDisconnect(data []byte) (*controlpb.Disconnect, error) { |
| 57 |
var cmd controlpb.Disconnect |
| 58 |
err := cmd.Unmarshal(data) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
return &cmd, nil |
| 63 |
} |
| 64 |
|
| 65 |
// DecodeSurveyRequest ... |
| 66 |
func (e *ProtobufDecoder) DecodeSurveyRequest(data []byte) (*controlpb.SurveyRequest, error) { |
| 67 |
var cmd controlpb.SurveyRequest |
| 68 |
err := cmd.Unmarshal(data) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
return &cmd, nil |
| 73 |
} |
| 74 |
|
| 75 |
// DecodeSurveyResponse ... |
| 76 |
func (e *ProtobufDecoder) DecodeSurveyResponse(data []byte) (*controlpb.SurveyResponse, error) { |
| 77 |
var cmd controlpb.SurveyResponse |
| 78 |
err := cmd.Unmarshal(data) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
return &cmd, nil |
| 83 |
} |