WriteString() private method

private WriteString ( string s, int len ) : void
s string
len int
return void
Example #1
0
        internal async Task WriteSASLInitialResponse(string mechanism, byte[] initialResponse, bool async)
        {
            var len = sizeof(byte) +                                                // Message code
                      sizeof(int) +                                                 // Length
                      PGUtil.UTF8Encoding.GetByteCount(mechanism) + sizeof(byte) +  // Mechanism plus null terminator
                      sizeof(int) +                                                 // Initial response length
                      (initialResponse?.Length ?? 0);                               // Initial response payload

            if (WriteBuffer.WriteSpaceLeft < len)
            {
                await WriteBuffer.Flush(async);
            }

            WriteBuffer.WriteByte(FrontendMessageCode.Password);
            WriteBuffer.WriteInt32(len - 1);

            WriteBuffer.WriteString(mechanism);
            WriteBuffer.WriteByte(0);   // null terminator
            if (initialResponse == null)
            {
                WriteBuffer.WriteInt32(-1);
            }
            else
            {
                WriteBuffer.WriteInt32(initialResponse.Length);
                WriteBuffer.WriteBytes(initialResponse);
            }
        }
Example #2
0
        internal void WriteStartup(Dictionary <string, string> parameters)
        {
            const int protocolVersion3 = 3 << 16; // 196608

            var len = sizeof(int) +               // Length
                      sizeof(int) +               // Protocol version
                      sizeof(byte);               // Trailing zero byte

            foreach (var kvp in parameters)
            {
                len += PGUtil.UTF8Encoding.GetByteCount(kvp.Key) + 1 +
                       PGUtil.UTF8Encoding.GetByteCount(kvp.Value) + 1;
            }

            // Should really never happen, just in case
            if (len > WriteBuffer.Size)
            {
                throw new Exception("Startup message bigger than buffer");
            }

            WriteBuffer.WriteInt32(len);
            WriteBuffer.WriteInt32(protocolVersion3);

            foreach (var kv in parameters)
            {
                WriteBuffer.WriteString(kv.Key);
                WriteBuffer.WriteByte(0);
                WriteBuffer.WriteString(kv.Value);
                WriteBuffer.WriteByte(0);
            }

            WriteBuffer.WriteByte(0);
        }
Example #3
0
        internal async Task WriteParse(string sql, string statementName, List <NpgsqlParameter> inputParameters, bool async)
        {
            Debug.Assert(statementName.All(c => c < 128));

            int queryByteLen;

            try
            {
                queryByteLen = TextEncoding.GetByteCount(sql);
            }
            catch (Exception e)
            {
                Break(e);
                throw;
            }

            if (WriteBuffer.WriteSpaceLeft < 1 + 4 + statementName.Length + 1)
            {
                await Flush(async);
            }

            var messageLength =
                sizeof(byte) +                        // Message code
                sizeof(int) +                         // Length
                statementName.Length +                // Statement name
                sizeof(byte) +                        // Null terminator for the statement name
                queryByteLen + sizeof(byte) +         // SQL query length plus null terminator
                sizeof(ushort) +                      // Number of parameters
                inputParameters.Count * sizeof(int);  // Parameter OIDs

            WriteBuffer.WriteByte(FrontendMessageCode.Parse);
            WriteBuffer.WriteInt32(messageLength - 1);
            WriteBuffer.WriteNullTerminatedString(statementName);

            await WriteBuffer.WriteString(sql, queryByteLen, async);

            if (WriteBuffer.WriteSpaceLeft < 1 + 2)
            {
                await Flush(async);
            }
            WriteBuffer.WriteByte(0); // Null terminator for the query
            WriteBuffer.WriteUInt16((ushort)inputParameters.Count);

            foreach (var p in inputParameters)
            {
                if (WriteBuffer.WriteSpaceLeft < 4)
                {
                    await Flush(async);
                }

                WriteBuffer.WriteInt32((int)p.Handler !.PostgresType.OID);
            }
        }
Example #4
0
        internal async Task WriteQuery(string sql, bool async)
        {
            var queryByteLen = TextEncoding.GetByteCount(sql);

            if (WriteBuffer.WriteSpaceLeft < 1 + 4)
            {
                await Flush(async);
            }

            WriteBuffer.WriteByte(FrontendMessageCode.Query);
            WriteBuffer.WriteInt32(
                sizeof(int) +         // Message length (including self excluding code)
                queryByteLen +        // Query byte length
                sizeof(byte));        // Null terminator

            await WriteBuffer.WriteString(sql, queryByteLen, async);

            if (WriteBuffer.WriteSpaceLeft < 1)
            {
                await Flush(async);
            }
            WriteBuffer.WriteByte(0);  // Null terminator
        }