marshalAndSend takes a method and arguments, scrubs the arguments to create a dnode message, marshals the message to JSON and sends it over the wire.
(method interface{}, arguments []interface{})
| 836 | // marshalAndSend takes a method and arguments, scrubs the arguments to create |
| 837 | // a dnode message, marshals the message to JSON and sends it over the wire. |
| 838 | func (c *Client) marshalAndSend(method interface{}, arguments []interface{}) (callbacks map[string]dnode.Path, errC <-chan error, err error) { |
| 839 | // scrub trough the arguments and save any callbacks. |
| 840 | callbacks = c.scrubber.Scrub(arguments) |
| 841 | |
| 842 | defer func() { |
| 843 | if err != nil { |
| 844 | c.removeCallbacks(callbacks) |
| 845 | } |
| 846 | }() |
| 847 | |
| 848 | // Do not encode empty arguments as "null", make it "[]". |
| 849 | if arguments == nil { |
| 850 | arguments = make([]interface{}, 0) |
| 851 | } |
| 852 | |
| 853 | rawArgs, err := json.Marshal(arguments) |
| 854 | if err != nil { |
| 855 | return nil, nil, err |
| 856 | } |
| 857 | |
| 858 | msg := dnode.Message{ |
| 859 | Method: method, |
| 860 | Arguments: &dnode.Partial{Raw: rawArgs}, |
| 861 | Callbacks: callbacks, |
| 862 | } |
| 863 | |
| 864 | p, err := json.Marshal(msg) |
| 865 | if err != nil { |
| 866 | return nil, nil, err |
| 867 | } |
| 868 | |
| 869 | select { |
| 870 | case <-c.closeChan: |
| 871 | return nil, nil, errors.New("can't send, client is closed") |
| 872 | default: |
| 873 | if c.getSession() == nil { |
| 874 | return nil, nil, errors.New("can't send, session is not established yet") |
| 875 | } |
| 876 | |
| 877 | errC := make(chan error, 1) |
| 878 | |
| 879 | c.send <- &message{ |
| 880 | p: p, |
| 881 | errC: errC, |
| 882 | } |
| 883 | |
| 884 | return callbacks, errC, nil |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | func (c *Client) getSession() sockjs.Session { |
| 889 | c.m.RLock() |
no test coverage detected