DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
StreamDatabaseInterface.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using System;
17 using System.IO;
18 
19 namespace Deveel.Data.Client {
26  class StreamDatabaseInterface : RemoteDatabaseInterface {
27  internal StreamDatabaseInterface(string initial_database)
28  : base(initial_database) {
29  }
30 
34  protected BinaryWriter output;
38  protected BinaryReader input;
39 
40  private bool closed = false;
41 
47  internal void Setup(Stream rawin, Stream rawout) {
48  if (rawin == null || rawout == null) {
49  throw new IOException("rawin or rawin is null");
50  }
51  // Get the input and output and wrap around Data streams.
52  input = new BinaryReader(new BufferedStream(rawin, 32768));
53  output = new BinaryWriter(new BufferedStream(rawout, 32768));
54  }
55 
57  protected override void SendCommand(byte[] command, int offset, int size) {
58  output.Write(size);
59  output.Write(command, 0, size);
60  output.Flush();
61  }
62 
64  protected override byte[] ReceiveCommand(int timeout) {
65  if (closed) {
66  throw new IOException("IDatabaseInterface is closed!");
67  }
68  try {
69  int commandLength = input.ReadInt32();
70  byte[] buf = new byte[commandLength];
71  input.Read(buf, 0, commandLength);
72  return buf;
73  } catch (NullReferenceException) {
74  Console.Out.WriteLine("Throwable generated at: " + this);
75  throw;
76  }
77  }
78 
80  protected override void CloseConnection() {
81  try {
82  if (output != null)
83  output.Close();
84  } finally {
85  if (input != null)
86  input.Close();
87 
88  closed = true;
89  }
90  }
91  }
92 }
BinaryReader input
The data input stream for the db protocol.
BinaryWriter output
The data output stream for the db protocol.
void Setup(Stream rawin, Stream rawout)
Sets up the stream connection with the given input/output stream.
override void SendCommand(byte[] command, int offset, int size)
An stream implementation of an interface to a database.