示例#1
0
    public NetworkPeer( )
    {
        _meta = SharedNet.Instance.MsgMeta;

        _peerConnectedMsgID = _meta.GetMessageID <gamedef.PeerConnected>();
        _peerClosedMsgID    = _meta.GetMessageID <gamedef.PeerClosed>();
    }
示例#2
0
    public NetworkPeer( )
    {
        DebugMessage = true;

        _meta = PeerManager.Instance.MsgMeta;

        MsgID_Connected    = _meta.GetMessageID <gamedef.PeerConnected>();
        MsgID_Disconnected = _meta.GetMessageID <gamedef.PeerDisconnected>();
        MsgID_ConnectError = _meta.GetMessageID <gamedef.PeerConnectError>();
        MsgID_SendError    = _meta.GetMessageID <gamedef.PeerSendError>();
        MsgID_RecvError    = _meta.GetMessageID <gamedef.PeerRecvError>();
    }
示例#3
0
    /// <summary>
    /// 发一个消息
    /// </summary>
    /// <typeparam name="T">消息类型</typeparam>
    /// <param name="msg">消息内容</param>
    public void SendMessage <T>(T msg)
    {
        if (_socket == null)
        {
            return;
        }

        if (!_socket.IsConnected)
        {
            if (OnConnectionNotReady != null)
            {
                OnConnectionNotReady.Invoke(this);
            }
        }

        uint msgID = _meta.GetMessageID <T>();

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

        LogMessage(msgID, msg);

        if (OnSend != null)
        {
            OnSend(this, msgID, msg);
        }

        MemoryStream data = new MemoryStream();

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

        _socket.SendPacket(msgID, data);
    }