| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 | 
							- // This code is derived from jcifs smb client library <jcifs at samba dot org>
 
- // Ported by J. Arturo <webmaster at komodosoft dot net>
 
- //  
 
- // This library is free software; you can redistribute it and/or
 
- // modify it under the terms of the GNU Lesser General Public
 
- // License as published by the Free Software Foundation; either
 
- // version 2.1 of the License, or (at your option) any later version.
 
- // 
 
- // This library is distributed in the hope that it will be useful,
 
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
- // Lesser General Public License for more details.
 
- // 
 
- // You should have received a copy of the GNU Lesser General Public
 
- // License along with this library; if not, write to the Free Software
 
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
- using System;
 
- using System.IO;
 
- using SharpCifs.Smb;
 
- using SharpCifs.Util.Sharpen;
 
- namespace SharpCifs.Util.Transport
 
- {
 
- 	/// <summary>
 
- 	/// This class simplifies communication for protocols that support
 
- 	/// multiplexing requests.
 
- 	/// </summary>
 
- 	/// <remarks>
 
- 	/// This class simplifies communication for protocols that support
 
- 	/// multiplexing requests. It encapsulates a stream and some protocol
 
- 	/// knowledge (provided by a concrete subclass) so that connecting,
 
- 	/// disconnecting, sending, and receiving can be syncronized
 
- 	/// properly. Apparatus is provided to send and receive requests
 
- 	/// concurrently.
 
- 	/// </remarks>
 
- 	public abstract class Transport : IRunnable
 
- 	{
 
- 		internal static int Id;
 
- 		//internal static LogStream log = LogStream.GetInstance();
 
- 	    public LogStream Log
 
- 	    {
 
- 	        get
 
- 	        {
 
- 	            return LogStream.GetInstance();
 
- 	        }
 
- 	    }
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		public static int Readn(InputStream @in, byte[] b, int off, int len)
 
- 		{
 
- 			int i = 0;
 
- 			int n = -5;
 
- 			while (i < len)
 
- 			{
 
- 				n = @in.Read(b, off + i, len - i);
 
- 				if (n <= 0)
 
- 				{
 
- 					break;
 
- 				}
 
- 				i += n;
 
- 			}
 
- 			return i;
 
- 		}
 
- 		internal int State;
 
- 		internal string Name = "Transport" + Id++;
 
- 		internal Thread Thread;
 
- 		internal TransportException Te;
 
- 		protected internal Hashtable ResponseMap = new Hashtable();
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		protected internal abstract void MakeKey(ServerMessageBlock request);
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		protected internal abstract ServerMessageBlock PeekKey();
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
-         protected internal abstract void DoSend(ServerMessageBlock request);
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		protected internal abstract void DoRecv(Response response);
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		protected internal abstract void DoSkip();
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		public virtual void Sendrecv(ServerMessageBlock request, Response response, long timeout)
 
- 		{
 
- 			lock (this)
 
- 			{
 
- 				MakeKey(request);
 
- 				response.IsReceived = false;
 
- 				try
 
- 				{
 
- 					ResponseMap.Put(request, response);
 
- 					DoSend(request);
 
- 					response.Expiration = Runtime.CurrentTimeMillis() + timeout;
 
- 					while (!response.IsReceived)
 
- 					{
 
- 						Runtime.Wait(this, timeout);
 
- 						timeout = response.Expiration - Runtime.CurrentTimeMillis();
 
- 						if (timeout <= 0)
 
- 						{
 
- 							throw new TransportException(Name + " timedout waiting for response to " + request
 
- 								);
 
- 						}
 
- 					}
 
- 				}
 
- 				catch (IOException ioe)
 
- 				{
 
- 					if (Log.Level > 2)
 
- 					{
 
- 						Runtime.PrintStackTrace(ioe, Log);
 
- 					}
 
- 					try
 
- 					{
 
- 						Disconnect(true);
 
- 					}
 
- 					catch (IOException ioe2)
 
- 					{
 
- 						Runtime.PrintStackTrace(ioe2, Log);
 
- 					}
 
- 					throw;
 
- 				}
 
- 				catch (Exception ie)
 
- 				{
 
- 					throw new TransportException(ie);
 
- 				}
 
- 				finally
 
- 				{
 
- 					//Sharpen.Collections.Remove(response_map, request);
 
-                     ResponseMap.Remove(request);
 
- 				}
 
- 			}
 
- 		}
 
- 		private void Loop()
 
- 		{
 
- 			while (Thread == Thread.CurrentThread())
 
- 			{
 
- 				try
 
- 				{
 
- 					ServerMessageBlock key = PeekKey();
 
- 					if (key == null)
 
- 					{
 
- 						throw new IOException("end of stream");
 
- 					}
 
- 					
 
-                     
 
-                     lock (this)                    
 
- 					{
 
- 						Response response = (Response)ResponseMap.Get(key);                      
 
- 						if (response == null)
 
- 						{
 
- 							if (Log.Level >= 4)
 
- 							{
 
- 								Log.WriteLine("Invalid key, skipping message");
 
- 							}
 
- 							DoSkip();
 
- 						}
 
- 						else
 
- 						{
 
- 							DoRecv(response);
 
- 							response.IsReceived = true;
 
- 							Runtime.NotifyAll(this);
 
- 						}
 
- 					}
 
- 				}
 
- 				catch (Exception ex)
 
- 				{
 
- 					string msg = ex.Message;
 
- 					bool timeout = msg != null && msg.Equals("Read timed out");
 
- 					bool hard = timeout == false;
 
- 					if (!timeout && Log.Level >= 3)
 
- 					{
 
- 						Runtime.PrintStackTrace(ex, Log);
 
- 					}
 
- 					try
 
- 					{
 
- 						Disconnect(hard);
 
- 					}
 
- 					catch (IOException ioe)
 
- 					{
 
- 						Runtime.PrintStackTrace(ioe, Log);
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 		/// <exception cref="System.Exception"></exception>
 
- 		protected internal abstract void DoConnect();
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		protected internal abstract void DoDisconnect(bool hard);
 
- 		/// <exception cref="SharpCifs.Util.Transport.TransportException"></exception>
 
- 		public virtual void Connect(long timeout)
 
- 		{
 
- 			lock (this)
 
- 			{
 
- 				try
 
- 				{
 
- 					switch (State)
 
- 					{
 
- 						case 0:
 
- 						{
 
- 							break;
 
- 						}
 
- 						case 3:
 
- 						{
 
- 							return;
 
- 						}
 
- 						case 4:
 
- 						{
 
- 							// already connected
 
- 							State = 0;
 
- 							throw new TransportException("Connection in error", Te);
 
- 						}
 
- 						default:
 
- 						{
 
- 							//TransportException te = new TransportException("Invalid state: " + state);
 
- 							State = 0;
 
-                             throw new TransportException("Invalid state: " + State);
 
- 						}
 
- 					}
 
- 					State = 1;
 
- 					Te = null;
 
- 					Thread = new Thread(this);
 
- 					Thread.SetDaemon(true);
 
- 					lock (Thread)
 
- 					{
 
- 						Thread.Start();
 
- 						Runtime.Wait(Thread, timeout);
 
- 						switch (State)
 
- 						{
 
- 							case 1:
 
- 							{
 
- 								State = 0;
 
- 								Thread = null;
 
- 								throw new TransportException("Connection timeout");
 
- 							}
 
- 							case 2:
 
- 							{
 
- 								if (Te != null)
 
- 								{
 
- 									State = 4;
 
- 									Thread = null;
 
- 									throw Te;
 
- 								}
 
- 								State = 3;
 
- 								return;
 
- 							}
 
- 						}
 
- 					}
 
- 				}
 
- 				catch (Exception ie)
 
- 				{
 
- 					State = 0;
 
- 					Thread = null;
 
- 					throw new TransportException(ie);
 
- 				}
 
- 				finally
 
- 				{
 
- 					if (State != 0 && State != 3 && State != 4)
 
- 					{
 
- 						if (Log.Level >= 1)
 
- 						{
 
- 							Log.WriteLine("Invalid state: " + State);
 
- 						}
 
- 						State = 0;
 
- 						Thread = null;
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 		/// <exception cref="System.IO.IOException"></exception>
 
- 		public virtual void Disconnect(bool hard)
 
- 		{
 
-             if (hard)
 
-             {
 
-                 IOException ioe = null;
 
-                 switch (State)
 
-                 {
 
-                     case 0:
 
-                         {
 
-                             return;
 
-                         }
 
-                     case 2:
 
-                         {
 
-                             hard = true;
 
-                             goto case 3;
 
-                         }
 
-                     case 3:
 
-                         {
 
-                             if (ResponseMap.Count != 0 && !hard)
 
-                             {
 
-                                 break;
 
-                             }
 
-                             try
 
-                             {
 
-                                 DoDisconnect(hard);
 
-                             }
 
-                             catch (IOException ioe0)
 
-                             {
 
-                                 ioe = ioe0;
 
-                             }
 
-                             goto case 4;
 
-                         }
 
-                     case 4:
 
-                         {
 
-                             Thread = null;
 
-                             State = 0;
 
-                             break;
 
-                         }
 
-                     default:
 
-                         {
 
-                             if (Log.Level >= 1)
 
-                             {
 
-                                 Log.WriteLine("Invalid state: " + State);
 
-                             }
 
-                             Thread = null;
 
-                             State = 0;
 
-                             break;
 
-                         }
 
-                 }
 
-                 if (ioe != null)
 
-                 {
 
-                     throw ioe;
 
-                 }
 
-                 return;
 
-             }
 
-             
 
-             lock (this)
 
- 			{
 
- 				IOException ioe = null;
 
- 				switch (State)
 
- 				{
 
- 					case 0:
 
- 					{
 
- 						return;
 
- 					}
 
- 					case 2:
 
- 					{
 
- 						hard = true;
 
- 						goto case 3;
 
- 					}
 
- 					case 3:
 
- 					{
 
- 						if (ResponseMap.Count != 0 && !hard)
 
- 						{
 
- 							break;
 
- 						}
 
- 						try
 
- 						{
 
- 							DoDisconnect(hard);
 
- 						}
 
- 						catch (IOException ioe0)
 
- 						{
 
- 							ioe = ioe0;
 
- 						}
 
- 						goto case 4;
 
- 					}
 
- 					case 4:
 
- 					{
 
- 						Thread = null;
 
- 						State = 0;
 
- 						break;
 
- 					}
 
- 					default:
 
- 					{
 
- 						if (Log.Level >= 1)
 
- 						{
 
- 							Log.WriteLine("Invalid state: " + State);
 
- 						}
 
- 						Thread = null;
 
- 						State = 0;
 
- 						break;
 
- 					}
 
- 				}
 
- 				if (ioe != null)
 
- 				{
 
- 					throw ioe;
 
- 				}
 
- 			}
 
- 		}
 
- 		public virtual void Run()
 
- 		{
 
- 			Thread runThread = Thread.CurrentThread();
 
- 			Exception ex0 = null;
 
- 			try
 
- 			{
 
- 				DoConnect();
 
- 			}
 
- 			catch (Exception ex)
 
- 			{
 
- 				ex0 = ex;
 
- 				// Defer to below where we're locked
 
- 				return;
 
- 			}
 
- 			finally
 
- 			{
 
- 				lock (runThread)
 
- 				{
 
- 					if (runThread != Thread)
 
- 					{
 
- 						if (ex0 != null)
 
- 						{
 
- 							if (Log.Level >= 2)
 
- 							{
 
- 								Runtime.PrintStackTrace(ex0, Log);
 
- 							}
 
- 						}
 
- 						//return;
 
- 					}
 
- 					if (ex0 != null)
 
- 					{
 
- 						Te = new TransportException(ex0);
 
- 					}
 
- 					State = 2;
 
- 					// run connected
 
- 					Runtime.Notify(runThread);
 
- 				}
 
- 			}
 
- 			Loop();
 
- 		}
 
- 		public override string ToString()
 
- 		{
 
- 			return Name;
 
- 		}
 
- 	}
 
- }
 
 
  |