1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 using System.Net.Sockets;
22 using ByteFX.Data.Common;
24 namespace ByteFX.Data.MySqlClient
27 /// Summary description for API.
29 internal class MySqlStream : Stream
35 public MySqlStream( string host, int port, int timeout )
48 if (stream is NetworkStream)
50 bool poll = socket.Poll(-1, SelectMode.SelectWrite );
51 poll = socket.Poll(-1, SelectMode.SelectRead );
52 poll = socket.Poll(-1, SelectMode.SelectError );
59 private void Create( string host, int port )
61 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
62 IPHostEntry he = Dns.GetHostByName( host );
63 IPEndPoint serverAddr = new IPEndPoint(he.AddressList[0], port);
65 socket.Connect(serverAddr);
66 stream = new NetworkStream(socket, true);
69 private void Create( string host )
73 if (host.ToLower().Equals("localhost"))
74 pipeName = @"\\.\pipe\MySql";
76 pipeName = String.Format(@"\\{0}\pipe\MySql", host);
78 stream = new ByteFX.Data.Common.NamedPipeStream(pipeName, FileAccess.ReadWrite);
81 public bool DataAvailable
85 if (stream is NetworkStream)
86 return ((NetworkStream)stream).DataAvailable;
87 else return (stream as NamedPipeStream).DataAvailable;
91 public override bool CanRead
93 get { return stream.CanRead; }
96 public override bool CanWrite
98 get { return stream.CanWrite; }
101 public override bool CanSeek
103 get { return stream.CanSeek; }
106 public override long Length
108 get { return stream.Length; }
111 public override long Position
113 get { return stream.Position; }
114 set { stream.Position = value; }
117 public override void Flush()
122 public override int ReadByte()
124 long start = Environment.TickCount;
125 long timeout_ticks = timeOut * TimeSpan.TicksPerSecond;
127 while (((Environment.TickCount - start) < timeout_ticks))
131 int b = stream.ReadByte();
135 throw new Exception("Timeout waiting for response from server");
138 public override int Read(byte[] buffer, int offset, int count)
140 long start = Environment.TickCount;
141 int numToRead = count;
142 long timeout_ticks = timeOut * TimeSpan.TicksPerSecond;
144 while (numToRead > 0 && ((Environment.TickCount - start) < timeout_ticks))
148 int bytes_read = stream.Read(buffer, offset, numToRead);
149 offset += bytes_read;
150 numToRead -= bytes_read;
154 throw new Exception("Timeout waiting for response from server");
158 public int ReadInt24()
160 byte[] bytes = new byte[3];
162 return (bytes[0] + (bytes[1]*256) + (bytes[2]*256*256));
165 public override void Close()
170 public override void SetLength(long length)
172 stream.SetLength( length );
175 public override void Write(byte[] buffer, int offset, int count)
177 stream.Write( buffer, offset, count );
180 public override long Seek( long offset, SeekOrigin origin )
182 return stream.Seek( offset, origin );