Ejemplo n.º 1
0
        public bool SendMessage(int type, ProtoBuf.IExtensible msg)
        {
            if (!_isConnected)
            {
                return(false);
            }

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            ProtoBuf.Serializer.Serialize(stream, msg);
            byte[] bMsg = stream.ToArray();

            int len  = 4 + 4 + bMsg.Length;
            int rLen = IPAddress.HostToNetworkOrder(len);

            byte[] bLen = BitConverter.GetBytes(rLen);

            int rIndex = IPAddress.HostToNetworkOrder(_index);

            byte[] bIndex = BitConverter.GetBytes(rIndex);

            int rType = IPAddress.HostToNetworkOrder(type);

            byte[] bType = BitConverter.GetBytes(rType);

            byte[] bSend = new byte[4 + len];
            Array.Copy(bLen, 0, bSend, 0, 4);
            Array.Copy(bIndex, 0, bSend, 4, 4);
            Array.Copy(bType, 0, bSend, 8, 4);
            Array.Copy(bMsg, 0, bSend, 12, len - 8);
            _socketClient.Send(bSend);

            ++_index;

            return(true);
        }
Ejemplo n.º 2
0
        public bool SendMessage(int type, global::ProtoBuf.IExtensible msg)
        {
            if (!_isConnected)
            {
                return(false);
            }

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            ProtoBuf.Serializer.Serialize(stream, msg);
            byte[] bMsg = stream.ToArray();

            int len = 4 + bMsg.Length;

            byte[] bLen = BitConverter.GetBytes(len);
            System.Array.Reverse(bLen);
            byte[] bType = BitConverter.GetBytes(type);
            System.Array.Reverse(bType);

            byte[] bSend = new byte[4 + len];
            Array.Copy(bLen, 0, bSend, 0, 4);
            Array.Copy(bType, 0, bSend, 4, 4);
            Array.Copy(bMsg, 0, bSend, 8, len - 4);
            _socketClient.Send(bSend);

            return(true);
        }