New test.
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlStartupPacket.cs
1 // created on 9/6/2002 at 16:56
2
3
4 // Npgsql.NpgsqlStartupPacket.cs
5 //
6 // Author:
7 //      Francisco Jr. (fxjrlists@yahoo.com.br)
8 //
9 //      Copyright (C) 2002 The Npgsql Development Team
10 //      npgsql-general@gborg.postgresql.org
11 //      http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 //
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.
17 //
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.
22 //
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
26
27 using System;
28 using System.IO;
29 using System.Text;
30 using System.Net;
31
32 namespace Npgsql
33 {
34
35     /// <summary>
36     /// This class represents a StartupPacket message of PostgreSQL
37     /// protocol.
38     /// </summary>
39     ///
40     internal sealed class NpgsqlStartupPacket
41     {
42         // Logging related values
43         private static readonly String CLASSNAME = "NpgsqlStartupPacket";
44
45         // Private fields.
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;
53
54         public NpgsqlStartupPacket(Int32 packet_size,
55                                    ProtocolVersion protocol_version,
56                                    String database_name,
57                                    String user_name,
58                                    String arguments,
59                                    String unused,
60                                    String optional_tty)
61         {
62
63             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
64             // Just copy the values.
65
66             // [FIXME] Validate params? We are the only clients, so, hopefully, we
67             // know what to send.
68
69             this.packet_size = packet_size;
70             this.protocol_version = protocol_version;
71
72             this.database_name = database_name;
73             this.user_name = user_name;
74             this.arguments = arguments;
75             this.unused = unused;
76             this.optional_tty = optional_tty;
77
78         }
79
80
81         public void WriteToStream(Stream output_stream, Encoding encoding)
82         {
83             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteToStream");
84
85             switch (protocol_version) {
86             case ProtocolVersion.Version2 :
87                 WriteToStream_Ver_2(output_stream, encoding);
88                 break;
89
90             case ProtocolVersion.Version3 :
91                 WriteToStream_Ver_3(output_stream, encoding);
92                 break;
93
94             }
95         }
96
97
98         private void WriteToStream_Ver_2(Stream output_stream, Encoding encoding)
99                                 {
100             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteToStream_Ver_2");
101
102             // Packet length = 296
103             output_stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.packet_size)), 0, 4);
104
105             output_stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(PGUtil.ConvertProtocolVersion(this.protocol_version))), 0, 4);
106
107             // Database name.
108             PGUtil.WriteLimString(this.database_name, 64, output_stream, encoding);
109
110             // User name.
111             PGUtil.WriteLimString(this.user_name, 32, output_stream, encoding);
112
113             // Arguments.
114             PGUtil.WriteLimString(this.arguments, 64, output_stream, encoding);
115
116             // Unused.
117             PGUtil.WriteLimString(this.unused, 64, output_stream, encoding);
118
119             // Optional tty.
120             PGUtil.WriteLimString(this.optional_tty, 64, output_stream, encoding);
121             output_stream.Flush();
122         }
123
124
125         private void WriteToStream_Ver_3(Stream output_stream, Encoding encoding)
126         {
127             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteToStream_Ver_3");
128
129             PGUtil.WriteInt32(output_stream, 4 + 4 + 5 + (encoding.GetByteCount(user_name) + 1) + 9 + (encoding.GetByteCount(database_name) + 1) + 10 + 4 + 1);
130
131             PGUtil.WriteInt32(output_stream, Npgsql.PGUtil.ConvertProtocolVersion(this.protocol_version));
132
133             // User name.
134             PGUtil.WriteString("user", output_stream, encoding);
135
136             // User name.
137             PGUtil.WriteString(user_name, output_stream, encoding);
138
139             // Database name.
140             PGUtil.WriteString("database", output_stream, encoding);
141
142             // Database name.
143             PGUtil.WriteString(database_name, output_stream, encoding);
144
145             // DateStyle.
146             PGUtil.WriteString("DateStyle", output_stream, encoding);
147
148             // DateStyle.
149             PGUtil.WriteString("ISO", output_stream, encoding);
150
151             output_stream.WriteByte(0);
152             output_stream.Flush();
153         }
154     }
155 }