1 // created on 9/6/2002 at 16:56
4 // Npgsql.NpgsqlStartupPacket.cs
7 // Francisco Jr. (fxjrlists@yahoo.com.br)
9 // Copyright (C) 2002 The Npgsql Development Team
10 // npgsql-general@gborg.postgresql.org
11 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 /// This class represents a StartupPacket message of PostgreSQL
40 internal sealed class NpgsqlStartupPacket
42 // Logging related values
43 private static readonly String CLASSNAME
= "NpgsqlStartupPacket";
46 private Int32 packet_size
;
47 private ProtocolVersion protocol_version
;
48 private String database_name
;
49 private String user_name
;
50 private String arguments
;
51 private String unused
;
52 private String optional_tty
;
54 public NpgsqlStartupPacket(Int32 packet_size
,
55 ProtocolVersion protocol_version
,
63 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, CLASSNAME
);
64 // Just copy the values.
66 // [FIXME] Validate params? We are the only clients, so, hopefully, we
69 this.packet_size
= packet_size
;
70 this.protocol_version
= protocol_version
;
72 this.database_name
= database_name
;
73 this.user_name
= user_name
;
74 this.arguments
= arguments
;
76 this.optional_tty
= optional_tty
;
81 public void WriteToStream(Stream output_stream
, Encoding encoding
)
83 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "WriteToStream");
85 switch (protocol_version
) {
86 case ProtocolVersion
.Version2
:
87 WriteToStream_Ver_2(output_stream
, encoding
);
90 case ProtocolVersion
.Version3
:
91 WriteToStream_Ver_3(output_stream
, encoding
);
98 private void WriteToStream_Ver_2(Stream output_stream
, Encoding encoding
)
100 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "WriteToStream_Ver_2");
102 // Packet length = 296
103 output_stream
.Write(BitConverter
.GetBytes(IPAddress
.HostToNetworkOrder(this.packet_size
)), 0, 4);
105 output_stream
.Write(BitConverter
.GetBytes(IPAddress
.HostToNetworkOrder(PGUtil
.ConvertProtocolVersion(this.protocol_version
))), 0, 4);
108 PGUtil
.WriteLimString(this.database_name
, 64, output_stream
, encoding
);
111 PGUtil
.WriteLimString(this.user_name
, 32, output_stream
, encoding
);
114 PGUtil
.WriteLimString(this.arguments
, 64, output_stream
, encoding
);
117 PGUtil
.WriteLimString(this.unused
, 64, output_stream
, encoding
);
120 PGUtil
.WriteLimString(this.optional_tty
, 64, output_stream
, encoding
);
121 output_stream
.Flush();
125 private void WriteToStream_Ver_3(Stream output_stream
, Encoding encoding
)
127 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "WriteToStream_Ver_3");
129 PGUtil
.WriteInt32(output_stream
, 4 + 4 + 5 + (encoding
.GetByteCount(user_name
) + 1) + 9 + (encoding
.GetByteCount(database_name
) + 1) + 10 + 4 + 1);
131 PGUtil
.WriteInt32(output_stream
, Npgsql
.PGUtil
.ConvertProtocolVersion(this.protocol_version
));
134 PGUtil
.WriteString("user", output_stream
, encoding
);
137 PGUtil
.WriteString(user_name
, output_stream
, encoding
);
140 PGUtil
.WriteString("database", output_stream
, encoding
);
143 PGUtil
.WriteString(database_name
, output_stream
, encoding
);
146 PGUtil
.WriteString("DateStyle", output_stream
, encoding
);
149 PGUtil
.WriteString("ISO", output_stream
, encoding
);
151 output_stream
.WriteByte(0);
152 output_stream
.Flush();