Exemplo n.º 1
0
    /// <summary>
    /// 发一个消息
    /// </summary>
    /// <typeparam name="T">消息类型</typeparam>
    /// <param name="msg">消息内容</param>

    public void SendMessage <T>(T msg, bool isShow = true)
    {
        if (_socket == null)
        {
            return;
        }

        uint msgID = _metaSet.GetByType <T>().id;

        if (msgID == 0)
        {
            throw new InvalidCastException("Error when getting msgID:" + typeof(T).FullName);
        }


        MemoryStream data = new MemoryStream();

        try
        {
            IMessage im = msg as IMessage;
            im.WriteTo(data);
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
            return;
        }

        _socket.SendPacket(msgID, data.ToArray());
    }
Exemplo n.º 2
0
    /// <summary>
    /// 发一个消息
    /// </summary>
    /// <typeparam name="T">消息类型</typeparam>
    /// <param name="msg">消息内容</param>

    public void SendMessage <T>(T msg) where T : Sproto.SprotoTypeBase
    {
        if (_socket == null)
        {
            return;
        }

        uint msgID = _metaSet.GetByType <T>().id;

        if (msgID == 0)
        {
            throw new InvalidCastException("Error when getting msgID:" + typeof(T).FullName);
        }

        if (DebugMessage)
        {
            ReflectMessage(msgID, msg);
        }


        try
        {
            var data = spack.pack(msg.encode());

            _socket.SendPacket(msgID, data);
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
            return;
        }
    }
Exemplo n.º 3
0
    /// <summary>
    /// 发一个消息
    /// </summary>
    /// <typeparam name="T">消息类型</typeparam>
    /// <param name="msg">消息内容</param>

    public void SendMessage <T>(T msg)
    {
        if (_socket == null)
        {
            return;
        }

        uint msgID = _metaSet.GetByType <T>().id;

        if (msgID == 0)
        {
            throw new InvalidCastException("Error when getting msgID:" + typeof(T).FullName);
        }

        if (DebugMessage)
        {
            ReflectMessage(msgID, msg);
        }


        MemoryStream data = new MemoryStream();

        try
        {
            ProtoBuf.Serializer.Serialize(data, msg);
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
            return;
        }

        _socket.SendPacket(msgID, data.ToArray());
    }
Exemplo n.º 4
0
    public NetworkPeer()
    {
        _metaSet = PeerManager.Instance.MsgMeta;

        MsgID_Connected    = _metaSet.GetByType <PeerConnected>().id;
        MsgID_Disconnected = _metaSet.GetByType <PeerDisconnected>().id;
        MsgID_ConnectError = _metaSet.GetByType <PeerConnectError>().id;
        MsgID_SendError    = _metaSet.GetByType <PeerSendError>().id;
        MsgID_RecvError    = _metaSet.GetByType <PeerRecvError>().id;
    }