/// <summary>
        /// 发送SOCKET消息
        /// </summary>
        public void SendMessage(IMessage obj)
        {
            if (!ProtoDic.ContainProtoType(obj.GetType()))
            {
                Debug.LogError("不存协议类型");
                return;
            }
            ByteBuffer buff    = new ByteBuffer();
            int        protoId = ProtoDic.GetProtoIdByProtoType(obj.GetType());

            byte[] result;
            using (MemoryStream ms = new MemoryStream())
            {
                obj.WriteTo(ms);
                result = ms.ToArray();
            }

            UInt16 lengh = (UInt16)(result.Length + 2);

            Debug.Log("lengh" + lengh + ",protoId" + protoId);
            buff.WriteShort((UInt16)lengh);

            buff.WriteShort((UInt16)protoId);
            buff.WriteBytes(result);
            SendMessage(buff);
        }
Beispiel #2
0
        /// <summary>
        /// 소켓 메세지 보내기
        /// </summary>
        public void SendMessage(IMessage obj)
        {
            if (false == socketClient.IsConnected())
            {
                // block
                socketClient.SendSyncConnect();
            }

            if (!ProtoDic.ContainProtoType(obj.GetType()))
            {
                Debug.LogError("알 수 없는 프로토콜 유형");
                return;
            }
            ByteBuffer buff    = new ByteBuffer();
            int        protoId = ProtoDic.GetProtoIdByProtoType(obj.GetType());

            byte[] result;
            using (MemoryStream ms = new MemoryStream())
            {
                obj.WriteTo(ms);
                result = ms.ToArray();
            }

            UInt16 lengh = (UInt16)(result.Length + 2);

            Debug.Log("lengh" + lengh + ",protoId" + protoId);
            buff.WriteShort((UInt16)lengh);

            buff.WriteShort((UInt16)protoId);
            buff.WriteBytes(result);
            SendMessage(buff);
        }
Beispiel #3
0
        public void Send <T>(T message, TcpClient client)
            where T : IMessage
        {
            if (client == null)
            {
                return;
            }
            BaseMessage m    = new BaseMessage();
            var         type = typeof(T);

            Console.WriteLine("send data type=" + type);
            m.Id = ProtoDic.GetProtoIdByProtoType(type);
            byte[] bytes = message.ToByteArray();
            m.Data = ByteString.CopyFrom(bytes);
            try
            {
                bytes = m.ToByteArray();
                client.GetStream().Write(bytes, 0, bytes.Length);
            }
            catch (Exception ex)
            {
                Disconnect(client);
                Console.WriteLine("ex=" + ex.Message + LogUtil.GetStackTraceModelName());
            }
        }
Beispiel #4
0
        private static void SendMessage(object obj, Socket clientSocket)
        {
            MemoryStream ms = new MemoryStream();

            ProtoBuf.Serializer.Serialize(ms, obj);
            ByteBuffer buff    = new ByteBuffer();
            Type       type    = obj.GetType();
            int        protoId = ProtoDic.GetProtoIdByProtoType(type);

            buff.WriteShort((ushort)protoId);
            buff.WriteBytes(ms.ToArray());
            clientSocket.Send(WriteMessage(buff.ToBytes()));
        }
Beispiel #5
0
        /// <summary>
        /// 发送SOCKET消息
        /// </summary>
        public void SendMessage(object obj)
        {
            if (!ProtoDic.ContainProtoType(obj.GetType()))
            {
                Debug.LogError("不存协议类型");
                return;
            }
            ByteBuffer buff    = new ByteBuffer();
            int        protoId = ProtoDic.GetProtoIdByProtoType(obj.GetType());

            buff.WriteShort((ushort)protoId);
            MemoryStream ms = new MemoryStream();

            ProtoBuf.Serializer.Serialize(ms, obj);
            byte[] result = ms.ToArray();
            buff.WriteBytes(result);
            SendMessage(buff);
        }
    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="obj"></param>
    public void SendMessage(object obj)
    {
        if (!ProtoDic.isContainProtoType(obj.GetType()))
        {
            Debug.LogError("协议类型没有注册");
            return;
        }
        ByteBuffer buff    = new ByteBuffer();
        int        protoId = ProtoDic.GetProtoIdByProtoType(obj.GetType());

        using (MemoryStream ms = new MemoryStream())
        {
            ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(ms, obj);
            byte[] msg = ms.ToArray();
            buff.WriteShort((ushort)ms.Length); //消息长度
            buff.WriteShort((ushort)protoId);   //消息号
            buff.WriteBytes(msg);               //消息
            socketClient.SendMessage(buff);
        }
    }
Beispiel #7
0
    public override void Send <T>(T message)
    {
        if (_TcpClient == null)
        {
            return;
        }
        BaseMessage m = new BaseMessage();

        m.Id = ProtoDic.GetProtoIdByProtoType(typeof(T));
        byte[] bytes = message.ToByteArray();
        m.Data = ByteString.CopyFrom(bytes);
        try
        {
            bytes = m.ToByteArray();
            _TcpClient.GetStream().Write(bytes, 0, bytes.Length);
        }
        catch (Exception ex)
        {
            Debug.Log("ex=" + ex.Message);
        }
    }
Beispiel #8
0
        public void Send <T>(T message, int userId)
            where T : IMessage
        {
            if (_Server == null)
            {
                return;
            }
            BaseMessage m = new BaseMessage();

            m.Id = ProtoDic.GetProtoIdByProtoType(typeof(T));
            byte[] bytes = message.ToByteArray();
            m.Data = ByteString.CopyFrom(bytes);
            try
            {
                bytes = m.ToByteArray();
                var token = Facade.Instance.GetClient(userId);
                Console.WriteLine("send message to token._IP=" + token._GameIp);
                _Server.Send(bytes, bytes.Length, token._GameIp);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ex=" + ex.Message);
            }
        }