예제 #1
0
        public void Write <T>(T message)
        {
            const int sizeSize   = sizeof(int);
            const int codeSize   = sizeof(byte);
            const int headerSize = sizeSize + codeSize;

            byte[] messageBody;
            long   messageLength = 0;

            using (var memStream = new MemoryStream())
            {
                // add a buffer to the start of the array to put the size and message code
                memStream.Position += headerSize;
                Serializer.Serialize(memStream, message);
                messageBody   = memStream.GetBuffer();
                messageLength = memStream.Position;
            }

            // check to make sure something was written, otherwise we'll have to create a new array
            if (messageLength == headerSize)
            {
                messageBody = new byte[headerSize];
            }

            var messageCode = TypeToMessageCodeMap[typeof(T)];
            var size        = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)messageLength - headerSize + 1));

            Array.Copy(size, messageBody, sizeSize);
            messageBody[sizeSize] = (byte)messageCode;

            if (PbcSocket.Send(messageBody, (int)messageLength, SocketFlags.None) == 0)
            {
                throw new RiakException("Failed to send data to server - Timed Out: {0}:{1}".Fmt(_server, _port));
            }
        }
예제 #2
0
        public void Write(MessageCode messageCode)
        {
            const int sizeSize   = sizeof(int);
            const int codeSize   = sizeof(byte);
            const int headerSize = sizeSize + codeSize;

            var messageBody = new byte[headerSize];

            var size = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(1));

            Array.Copy(size, messageBody, sizeSize);
            messageBody[sizeSize] = (byte)messageCode;

            if (PbcSocket.Send(messageBody, headerSize, SocketFlags.None) == 0)
            {
                throw new RiakException("Failed to send data to server - Timed Out: {0}:{1}".Fmt(_server, _port));
            }
        }