Example #1
0
    static public async Task <Response> Call <Response>(string connectionId, object msg)
    {
        var requestId  = ToMessageID(msg.GetType());
        var responseId = (int)Convert.ChangeType(ToMessageID(typeof(Response)), typeof(int));
        var task       = new TaskCompletionSource <SocketMessage>();

        tasks[responseId] = task;

        Moon.Buffer buffer = new Moon.Buffer();
        buffer.Write(Convert.ToUInt16(requestId));

        var sdata = Serializer.Encode(msg);

        if (null != sdata)
        {
            buffer.Write(sdata, 0, sdata.Length);
            Send(connectionId, buffer);
        }
        else
        {
            return(default(Response));
        }
        var res = await task.Task;

        return(Serializer.Decode <Response>(res.Data.Data, res.Data.Index, res.Data.Count));
    }
Example #2
0
    static public bool Send <Message>(string connectionId, Message msg)
    {
        var bytes = Serializer.Encode(msg);

        if (null == bytes)
        {
            OnError("", string.Format("Send Message {0}, Serialize error", msg.ToString()));
            return(false);
        }

        var id = Convert.ToUInt16(ToMessageID(msg.GetType()));

        Moon.Buffer buffer = new Moon.Buffer();
        buffer.Write(id);
        buffer.Write(bytes, 0, bytes.Length);
        Send(connectionId, buffer);
        return(true);
    }
Example #3
0
 static public void Send(string connectionId, Moon.Buffer data)
 {
     socket.Send(connectionId, data);
 }