New test.
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Gds / GdsServiceManager.cs
1 /*
2  *      Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *         The contents of this file are subject to the Initial 
5  *         Developer's Public License Version 1.0 (the "License"); 
6  *         you may not use this file except in compliance with the 
7  *         License. You may obtain a copy of the License at 
8  *         http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *         Software distributed under the License is distributed on 
11  *         an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *         express or implied. See the License for the specific 
13  *         language governing rights and limitations under the License.
14  * 
15  *      Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16  *      All Rights Reserved.
17  */
18
19 using System;
20 using System.IO;
21 using FirebirdSql.Data.Common;
22
23 namespace FirebirdSql.Data.Gds
24 {
25         internal sealed class GdsServiceManager : IServiceManager
26         {
27                 #region Fields
28
29                 private int handle;
30                 private GdsConnection connection;
31
32                 #endregion
33
34                 #region Properties
35
36                 public int Handle
37                 {
38                         get { return this.handle; }
39                 }
40
41                 public bool IsLittleEndian
42                 {
43                         get { return false; }
44                 }
45
46                 #endregion
47
48                 #region Constructors
49
50                 public GdsServiceManager()
51                 {
52                 }
53
54                 #endregion
55
56                 #region Methods
57
58                 public void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service)
59                 {
60                         lock (this)
61                         {
62                                 try
63                                 {
64                                         if (this.connection == null)
65                                         {
66                                                 this.connection = new GdsConnection();
67                                         }
68
69                                         this.connection.Connect(dataSource, port, 8192, Charset.DefaultCharset);
70
71                                         this.connection.Send.Write(IscCodes.op_service_attach);
72                                         this.connection.Send.Write(0);
73                                         this.connection.Send.Write(service);
74                                         this.connection.Send.WriteBuffer(spb.ToArray());
75                                         this.connection.Send.Flush();
76
77                                         try
78                                         {
79                                                 this.handle = this.connection.ReadGenericResponse().ObjectHandle;
80                                         }
81                                         catch (IscException)
82                                         {
83                                                 try
84                                                 {
85                                                         this.Detach();
86                                                 }
87                                                 catch
88                                                 {
89                                                 }
90
91                                                 throw;
92                                         }
93                                 }
94                                 catch (IOException)
95                                 {
96                                         this.connection.Disconnect();
97
98                                         throw new IscException(IscCodes.isc_net_write_err);
99                                 }
100                         }
101                 }
102
103                 public void Detach()
104                 {
105                         lock (this)
106                         {
107                                 try
108                                 {
109                                         this.connection.Send.Write(IscCodes.op_service_detach);
110                                         this.connection.Send.Write(this.Handle);
111                                         this.connection.Send.Flush();
112
113                                         this.connection.ReadGenericResponse();
114
115                                         this.handle = 0;
116                                 }
117                                 catch (IOException)
118                                 {
119                                         throw new IscException(IscCodes.isc_network_error);
120                                 }
121                                 finally
122                                 {
123                                         try
124                                         {
125                                                 this.connection.Disconnect();
126                                         }
127                                         catch (IOException)
128                                         {
129                                                 throw new IscException(IscCodes.isc_network_error);
130                                         }
131                                 }
132                         }
133                 }
134
135                 public void Start(ServiceParameterBuffer spb)
136                 {
137                         lock (this)
138                         {
139                                 try
140                                 {
141                                         this.connection.Send.Write(IscCodes.op_service_start);
142                                         this.connection.Send.Write(this.Handle);
143                                         this.connection.Send.Write(0);
144                                         this.connection.Send.WriteBuffer(spb.ToArray(), spb.Length);
145                                         this.connection.Send.Flush();
146
147                                         try
148                                         {
149                                                 this.connection.ReadGenericResponse();
150                                         }
151                                         catch (IscException)
152                                         {
153                                                 throw;
154                                         }
155                                 }
156                                 catch (IOException)
157                                 {
158                                         throw new IscException(IscCodes.isc_net_write_err);
159                                 }
160                         }
161                 }
162
163                 public void Query(
164                         ServiceParameterBuffer  spb,
165                         int                                             requestLength,
166                         byte[]                                  requestBuffer,
167                         int                                             bufferLength,
168                         byte[]                                  buffer)
169                 {
170                         lock (this)
171                         {
172                                 try
173                                 {
174                                         this.connection.Send.Write(IscCodes.op_service_info);   //      operation
175                                         this.connection.Send.Write(this.Handle);                                //      db_handle
176                                         this.connection.Send.Write((int)0);                                             //      incarnation                                     
177                                         this.connection.Send.WriteTyped(
178                                                 IscCodes.isc_spb_version, spb.ToArray());                       //      Service parameter buffer
179                                         this.connection.Send.WriteBuffer(
180                                                 requestBuffer, requestLength);                                          //      request buffer
181                                         this.connection.Send.Write(bufferLength);                               //      result buffer length
182
183                                         this.connection.Send.Flush();
184
185                                         GdsResponse r = this.connection.ReadGenericResponse();
186
187                                         Buffer.BlockCopy(r.Data, 0, buffer, 0, bufferLength);
188                                 }
189                                 catch (IOException)
190                                 {
191                                         throw new IscException(IscCodes.isc_network_error);
192                                 }
193                         }
194                 }
195
196                 #endregion
197
198                 #region Buffer creation methods
199
200                 public ServiceParameterBuffer CreateParameterBuffer()
201                 {
202                         return new ServiceParameterBuffer();
203                 }
204
205                 #endregion
206         }
207 }