1 // created on 1/6/2002 at 22:27
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 using System.Net.Sockets;
32 using System.Resources;
38 internal struct ProtocolVersion
40 public const Int32 Version2 = 131072;
41 public const Int32 Version3 = 196608;
44 internal enum FormatCode:
53 /// This class provides many util methods to handle
54 /// reading and writing of PostgreSQL protocol messages.
56 /// [FIXME] Does this name fully represent the class responsability?
57 /// Should it be abstract or with a private constructor to prevent
58 /// creating instances?
61 internal sealed class PGUtil
64 // Logging related values
65 private static readonly String CLASSNAME = "PGUtil";
66 private static ResourceManager resman = new ResourceManager(typeof(PGUtil));
69 /// This method gets a C NULL terminated string from the network stream.
70 /// It keeps reading a byte in each time until a NULL byte is returned.
71 /// It returns the resultant string of bytes read.
72 /// This string is sent from backend.
75 public static String ReadString(Stream network_stream, Encoding encoding)
77 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadString");
79 // [FIXME] Is 512 enough?
80 Byte[] buffer = new Byte[512];
85 // [FIXME] Is this cast always safe?
86 b = (Byte)network_stream.ReadByte();
91 b = (Byte)network_stream.ReadByte();
93 String string_read = encoding.GetString(buffer, 0, counter);
94 NpgsqlEventLog.LogMsg(resman, "Log_StringRead", LogLevel.Debug, string_read);
99 /// This method writes a C NULL terminated string to the network stream.
100 /// It appends a NULL terminator to the end of the String.
103 public static void WriteString(String the_string, Stream network_stream, Encoding encoding)
105 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteString");
107 network_stream.Write(encoding.GetBytes(the_string + '\x00') , 0, encoding.GetByteCount(the_string) + 1);
111 /// This method writes a C NULL terminated string limited in length to the
113 /// It pads the string with null bytes to the size specified.
116 public static void WriteLimString(String the_string, Int32 n, Stream network_stream, Encoding encoding)
118 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteLimString");
120 // [FIXME] Parameters should be validated. And what about strings
121 // larger than or equal to n?
123 // Pad the string to the specified value.
124 String string_padded = the_string.PadRight(n, '\x00');
126 network_stream.Write(encoding.GetBytes(string_padded), 0, n);
129 public static void CheckedStreamRead(Stream stream, Byte[] buffer, Int32 offset, Int32 size)
131 Int32 bytes_from_stream = 0;
132 Int32 total_bytes_read = 0;
135 bytes_from_stream = stream.Read(buffer, offset + total_bytes_read, size);
136 total_bytes_read += bytes_from_stream;
137 size -= bytes_from_stream;
144 public static void WriteInt32(Stream stream, Int32 value)
146 stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)), 0, 4);
149 public static Int32 ReadInt32(Stream stream, Byte[] buffer)
151 CheckedStreamRead(stream, buffer, 0, 4);
152 return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));
156 public static void WriteInt16(Stream stream, Int16 value)
158 stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)), 0, 2);
161 public static Int16 ReadInt16(Stream stream, Byte[] buffer)
163 CheckedStreamRead(stream, buffer, 0, 2);
164 return IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 0));
169 /*public static void WriteQueryToStream( String query, Stream stream, Encoding encoding )
171 NpgsqlEventLog.LogMsg( CLASSNAME + query, LogLevel.Debug );
172 // Send the query to server.
173 // Write the byte 'Q' to identify a query message.
174 stream.WriteByte((Byte)'Q');
176 // Write the query. In this case it is the CommandText text.
177 // It is a string terminated by a C NULL character.
178 stream.Write(encoding.GetBytes(query + '\x00') , 0, query.Length + 1);
185 public static Int32 ProtocolVersionMajor(Int32 protocolVersion)
187 return (protocolVersion >> 16) & 0xffff;
190 public static Int32 ProtocolVersionMinor(Int32 protocolVersion)
192 return protocolVersion & 0xffff;