2003-01-28 Daniel Morgan <danmorg@sc.rr.com>
[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 //
11
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.
16 // 
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.
21 // 
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
25
26 using System;
27 using System.IO;
28 using System.Text;
29 using System.Net;
30
31 namespace Npgsql
32 {
33         
34         /// <summary>
35         /// This class represents a StartupPacket message of PostgreSQL
36         /// protocol.
37         /// </summary>
38         /// 
39         internal sealed class NpgsqlStartupPacket
40         {
41                 
42                 // Logging related values
43     private static readonly String CLASSNAME = "NpgsqlStartupPacket";
44                 
45                 // Private fields.
46                 private Int32 packet_size;
47                 private Int32 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                                            Int32 protocol_version_major,
56                                            Int32 protocol_version_minor,
57                                            String database_name, 
58                                            String user_name,
59                                            String arguments,
60                                            String unused,
61                                            String optional_tty)
62                 {
63                         
64                         NpgsqlEventLog.LogMsg("Entering " + CLASSNAME + ".NpgsqlStartupPacket()", LogLevel.Debug);
65                         // Just copy the values.
66                         
67                         // [FIXME] Validate params? We are the only clients, so, hopefully, we
68                         // know what to send.
69                         
70                         this.packet_size = packet_size;
71                   this.protocol_version = (protocol_version_major<<16) | protocol_version_minor; 
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                 public void WriteToStream(Stream output_stream, Encoding encoding)
81                 {
82                         
83                         NpgsqlEventLog.LogMsg("Entering " + CLASSNAME + ".WriteToStream()", LogLevel.Debug);
84                         
85                         // [FIXME] Need exception handling ?
86                         
87                         // Packet length = 296
88       output_stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.packet_size)), 0, 4);
89                 
90       // Protocol version = 2.0
91       output_stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.protocol_version)), 0, 4);
92                 
93       // Database name.
94                         PGUtil.WriteLimString(this.database_name, 64, output_stream, encoding);
95       
96       // User name.
97       PGUtil.WriteLimString(this.user_name, 32, output_stream, encoding);
98         
99         // Arguments.
100         PGUtil.WriteLimString(this.arguments, 64, output_stream, encoding);
101                         
102       // Unused.
103       PGUtil.WriteLimString(this.unused, 64, output_stream, encoding);
104       
105       // Optional tty.
106       PGUtil.WriteLimString(this.optional_tty, 64, output_stream, encoding);
107                         
108                         
109                 }
110         }
111 }