TdsParserStateObject.cs 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750
  1. //------------------------------------------------------------------------------
  2. // <copyright file="TdsParserStateObject.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.SqlClient {
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data.Common;
  12. using System.Data.ProviderBase;
  13. using System.Data.Sql;
  14. using System.Data.SqlTypes;
  15. using System.Diagnostics;
  16. using System.Globalization;
  17. using System.Linq;
  18. using System.Runtime.CompilerServices;
  19. using System.Runtime.InteropServices;
  20. using System.Runtime.ConstrainedExecution;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using System.Security;
  24. using System.Text;
  25. sealed internal class LastIOTimer {
  26. internal long _value;
  27. }
  28. sealed internal class TdsParserStateObject {
  29. const int AttentionTimeoutSeconds = 5;
  30. // Ticks to consider a connection "good" after a successful I/O (10,000 ticks = 1 ms)
  31. // The resolution of the timer is typically in the range 10 to 16 milliseconds according to msdn.
  32. // We choose a value that is smaller than the likely timer resolution, but
  33. // large enough to ensure that check connection execution will be 0.1% or less
  34. // of very small open, query, close loops.
  35. private const long CheckConnectionWindow = 50000;
  36. private static int _objectTypeCount; // Bid counter
  37. internal readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
  38. internal int ObjectID {
  39. get {
  40. return _objectID;
  41. }
  42. }
  43. private readonly TdsParser _parser; // TdsParser pointer
  44. private SNIHandle _sessionHandle = null; // the SNI handle we're to work on
  45. private readonly WeakReference _owner = new WeakReference(null); // the owner of this session, used to track when it's been orphaned
  46. internal SqlDataReader.SharedState _readerState; // susbset of SqlDataReader state (if it is the owner) necessary for parsing abandoned results in TDS
  47. private int _activateCount; // 0 when we're in the pool, 1 when we're not, all others are an error
  48. // Two buffers exist in tdsparser, an in buffer and an out buffer. For the out buffer, only
  49. // one bookkeeping variable is needed, the number of bytes used in the buffer. For the in buffer,
  50. // three variables are actually needed. First, we need to record from the netlib how many bytes it
  51. // read from the netlib, this variable is _inBytesRead. Then, we need to also keep track of how many
  52. // bytes we have used as we consume the bytes from the buffer, that variable is _inBytesUsed. Third,
  53. // we need to keep track of how many bytes are left in the packet, so that we know when we have reached
  54. // the end of the packet and so we need to consume the next header. That variable is _inBytesPacket.
  55. // Header length constants
  56. internal readonly int _inputHeaderLen = TdsEnums.HEADER_LEN;
  57. internal readonly int _outputHeaderLen = TdsEnums.HEADER_LEN;
  58. // Out buffer variables
  59. internal byte[] _outBuff; // internal write buffer - initialize on login
  60. internal int _outBytesUsed = TdsEnums.HEADER_LEN; // number of bytes used in internal write buffer -
  61. // - initialize past header
  62. // In buffer variables
  63. private byte[] _inBuff; // internal read buffer - initialize on login
  64. internal int _inBytesUsed = 0; // number of bytes used in internal read buffer
  65. internal int _inBytesRead = 0; // number of bytes read into internal read buffer
  66. internal int _inBytesPacket = 0; // number of bytes left in packet
  67. // Packet state variables
  68. internal byte _outputMessageType = 0; // tds header type
  69. internal byte _messageStatus; // tds header status
  70. internal byte _outputPacketNumber = 1; // number of packets sent to server
  71. // in message - start at 1 per ramas
  72. internal bool _pendingData = false;
  73. internal volatile bool _fResetEventOwned = false; // ResetEvent serializing call to sp_reset_connection
  74. internal volatile bool _fResetConnectionSent = false; // For multiple packet execute
  75. internal bool _errorTokenReceived = false; // Keep track of whether an error was received for the result.
  76. // This is reset upon each done token - there can be
  77. internal bool _bulkCopyOpperationInProgress = false; // Set to true during bulk copy and used to turn toggle write timeouts.
  78. internal bool _bulkCopyWriteTimeout = false; // Set to trun when _bulkCopyOpeperationInProgress is trun and write timeout happens
  79. // SNI variables // multiple resultsets in one batch.
  80. private SNIPacket _sniPacket = null; // Will have to re-vamp this for MARS
  81. internal SNIPacket _sniAsyncAttnPacket = null; // Packet to use to send Attn
  82. private WritePacketCache _writePacketCache = new WritePacketCache(); // Store write packets that are ready to be re-used
  83. private Dictionary<IntPtr, SNIPacket> _pendingWritePackets = new Dictionary<IntPtr,SNIPacket>(); // Stores write packets that have been sent to SNI, but have not yet finished writing (i.e. we are waiting for SNI's callback)
  84. private object _writePacketLockObject = new object(); // Used to synchronize access to _writePacketCache and _pendingWritePackets
  85. // Async variables
  86. private GCHandle _gcHandle; // keeps this object alive until we're closed.
  87. private int _pendingCallbacks; // we increment this before each async read/write call and decrement it in the callback. We use this to determine when to release the GcHandle...
  88. // Timeout variables
  89. private long _timeoutMilliseconds;
  90. private long _timeoutTime; // variable used for timeout computations, holds the value of the hi-res performance counter at which this request should expire
  91. internal volatile bool _attentionSent = false; // true if we sent an Attention to the server
  92. internal bool _attentionReceived = false; // NOTE: Received is not volatile as it is only ever accessed\modified by TryRun its callees (i.e. single threaded access)
  93. internal volatile bool _attentionSending = false;
  94. internal bool _internalTimeout = false; // an internal timeout occured
  95. private readonly LastIOTimer _lastSuccessfulIOTimer;
  96. // secure password information to be stored
  97. // At maximum number of secure string that need to be stored is two; one for login password and the other for new change password
  98. private SecureString[] _securePasswords = new SecureString[2] {null, null};
  99. private int[] _securePasswordOffsetsInBuffer = new int[2];
  100. // This variable is used to track whether another thread has requested a cancel. The
  101. // synchronization points are
  102. // On the user's execute thread:
  103. // 1) the first packet write
  104. // 2) session close - return this stateObj to the session pool
  105. // On cancel thread we only have the cancel call.
  106. // Currently all access to this variable is inside a lock, though I hope to limit that in the
  107. // future. The state diagram is:
  108. // 1) pre first packet write, if cancel is requested, set variable so exception is triggered
  109. // on user thread when first packet write is attempted
  110. // 2) post first packet write, but before session return - a call to cancel will send an
  111. // attention to the server
  112. // 3) post session close - no attention is allowed
  113. private bool _cancelled;
  114. private const int _waitForCancellationLockPollTimeout = 100;
  115. // This variable is used to prevent sending an attention by another thread that is not the
  116. // current owner of the stateObj. I currently do not know how this can happen. Mark added
  117. // the code but does not remember either. At some point, we need to research killing this
  118. // logic.
  119. private volatile int _allowObjectID;
  120. internal bool _hasOpenResult = false;
  121. // Cache the transaction for which this command was executed so upon completion we can
  122. // decrement the appropriate result count.
  123. internal SqlInternalTransaction _executedUnderTransaction = null;
  124. // TDS stream processing variables
  125. internal ulong _longlen; // plp data length indicator
  126. internal ulong _longlenleft; // Length of data left to read (64 bit lengths)
  127. internal int[] _decimalBits = null; // scratch buffer for decimal/numeric data
  128. internal byte[] _bTmp = new byte[TdsEnums.YUKON_HEADER_LEN]; // Scratch buffer for misc use
  129. internal int _bTmpRead = 0; // Counter for number of temporary bytes read
  130. internal Decoder _plpdecoder = null; // Decoder object to process plp character data
  131. internal bool _accumulateInfoEvents= false; // TRUE - accumulate info messages during TdsParser.Run, FALSE - fire them
  132. internal List<SqlError> _pendingInfoEvents = null;
  133. //
  134. // DO NOT USE THIS BUFFER FOR OTHER THINGS.
  135. // ProcessHeader can be called ANYTIME while doing network reads.
  136. private byte[] _partialHeaderBuffer = new byte[TdsEnums.HEADER_LEN]; // Scratch buffer for ProcessHeader
  137. internal int _partialHeaderBytesRead = 0;
  138. //
  139. internal _SqlMetaDataSet _cleanupMetaData = null;
  140. internal _SqlMetaDataSetCollection _cleanupAltMetaDataSetArray = null;
  141. // Used for blanking out password in trace.
  142. internal int _tracePasswordOffset = 0;
  143. internal int _tracePasswordLength = 0;
  144. internal int _traceChangePasswordOffset = 0;
  145. internal int _traceChangePasswordLength = 0;
  146. internal bool _receivedColMetaData; // Used to keep track of when to fire StatementCompleted event.
  147. private SniContext _sniContext=SniContext.Undefined;
  148. #if DEBUG
  149. private SniContext _debugOnlyCopyOfSniContext=SniContext.Undefined;
  150. #endif
  151. private bool _bcpLock = false;
  152. // Null bitmap compression (NBC) information for the current row
  153. private NullBitmap _nullBitmapInfo;
  154. // Async
  155. internal TaskCompletionSource<object> _networkPacketTaskSource;
  156. private Timer _networkPacketTimeout;
  157. internal bool _syncOverAsync = true;
  158. private bool _snapshotReplay = false;
  159. private StateSnapshot _snapshot;
  160. internal ExecutionContext _executionContext;
  161. internal bool _asyncReadWithoutSnapshot = false;
  162. #if DEBUG
  163. // Used to override the assert than ensures that the stacktraces on subsequent replays are the same
  164. // This is useful is you are purposefully running the replay from a different thread (e.g. during SqlDataReader.Close)
  165. internal bool _permitReplayStackTraceToDiffer = false;
  166. // Used to indicate that the higher level object believes that this stateObj has enough data to complete an operation
  167. // If this stateObj has to read, then it will raise an assert
  168. internal bool _shouldHaveEnoughData = false;
  169. #endif
  170. // local exceptions to cache warnings and errors
  171. internal SqlErrorCollection _errors;
  172. internal SqlErrorCollection _warnings;
  173. internal object _errorAndWarningsLock = new object();
  174. private bool _hasErrorOrWarning = false;
  175. // local exceptions to cache warnings and errors that occured prior to sending attention
  176. internal SqlErrorCollection _preAttentionErrors;
  177. internal SqlErrorCollection _preAttentionWarnings;
  178. volatile private TaskCompletionSource<object> _writeCompletionSource = null;
  179. volatile private int _asyncWriteCount = 0;
  180. volatile private Exception _delayedWriteAsyncCallbackException = null; // set by write async callback if completion source is not yet created
  181. // _readingcount is incremented when we are about to read.
  182. // We check the parser state afterwards.
  183. // When the read is completed, we decrement it before handling errors
  184. // as the error handling may end up calling Dispose.
  185. int _readingCount;
  186. // Test hooks
  187. #if DEBUG
  188. // This is a test hook to enable testing of the retry paths.
  189. // When set to true, almost every possible retry point will be attempted.
  190. // This will drastically impact performance.
  191. //
  192. // Sample code to enable:
  193. //
  194. // Type type = typeof(SqlDataReader).Assembly.GetType("System.Data.SqlClient.TdsParserStateObject");
  195. // System.Reflection.FieldInfo field = type.GetField("_forceAllPends", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
  196. // if (field != null) {
  197. // field.SetValue(null, true);
  198. // }
  199. //
  200. internal static bool _forceAllPends = false;
  201. // set this while making a call that should not block.
  202. // instead of blocking it will fail.
  203. internal static bool _failAsyncPends = false;
  204. // If this is set and an async read is made, then
  205. // we will switch to syncOverAsync mode for the
  206. // remainder of the async operation.
  207. internal static bool _forceSyncOverAsyncAfterFirstPend = false;
  208. // Requests to send attention will be ignored when _skipSendAttention is true.
  209. // This is useful to simulate circumstances where timeouts do not recover.
  210. internal static bool _skipSendAttention = false;
  211. // Prevents any pending read from completing until the user signals it using
  212. // CompletePendingReadWithSuccess() or CompletePendingReadWithFailure(int errorCode) in SqlCommand\SqlDataReader
  213. internal static bool _forcePendingReadsToWaitForUser;
  214. internal TaskCompletionSource<object> _realNetworkPacketTaskSource = null;
  215. // Set to true to enable checking the call stacks match when packet retry occurs.
  216. internal static bool _checkNetworkPacketRetryStacks;
  217. #endif
  218. //////////////////
  219. // Constructors //
  220. //////////////////
  221. internal TdsParserStateObject(TdsParser parser) {
  222. // Construct a physical connection
  223. Debug.Assert(null != parser, "no parser?");
  224. _parser = parser;
  225. // For physical connection, initialize to default login packet size.
  226. SetPacketSize(TdsEnums.DEFAULT_LOGIN_PACKET_SIZE);
  227. // we post a callback that represents the call to dispose; once the
  228. // object is disposed, the next callback will cause the GC Handle to
  229. // be released.
  230. IncrementPendingCallbacks();
  231. _lastSuccessfulIOTimer = new LastIOTimer();
  232. }
  233. internal TdsParserStateObject(TdsParser parser, SNIHandle physicalConnection, bool async) {
  234. // Construct a MARS session
  235. Debug.Assert(null != parser, "no parser?");
  236. _parser = parser;
  237. SniContext=SniContext.Snix_GetMarsSession;
  238. Debug.Assert(null != _parser._physicalStateObj, "no physical session?");
  239. Debug.Assert(null != _parser._physicalStateObj._inBuff, "no in buffer?");
  240. Debug.Assert(null != _parser._physicalStateObj._outBuff, "no out buffer?");
  241. Debug.Assert(_parser._physicalStateObj._outBuff.Length ==
  242. _parser._physicalStateObj._inBuff.Length, "Unexpected unequal buffers.");
  243. // Determine packet size based on physical connection buffer lengths.
  244. SetPacketSize(_parser._physicalStateObj._outBuff.Length);
  245. SNINativeMethodWrapper.ConsumerInfo myInfo = CreateConsumerInfo(async);
  246. _sessionHandle = new SNIHandle(myInfo, physicalConnection);
  247. if (_sessionHandle.Status != TdsEnums.SNI_SUCCESS) {
  248. AddError(parser.ProcessSNIError(this));
  249. ThrowExceptionAndWarning();
  250. }
  251. // we post a callback that represents the call to dispose; once the
  252. // object is disposed, the next callback will cause the GC Handle to
  253. // be released.
  254. IncrementPendingCallbacks();
  255. _lastSuccessfulIOTimer = parser._physicalStateObj._lastSuccessfulIOTimer;
  256. }
  257. ////////////////
  258. // Properties //
  259. ////////////////
  260. // BcpLock - use to lock this object if there is a potential risk of using this object
  261. // between tds packets
  262. internal bool BcpLock {
  263. get {
  264. return _bcpLock;
  265. }
  266. set {
  267. _bcpLock = value;
  268. }
  269. }
  270. #if DEBUG
  271. internal SniContext DebugOnlyCopyOfSniContext {
  272. get {
  273. return _debugOnlyCopyOfSniContext;
  274. }
  275. }
  276. #endif
  277. internal SNIHandle Handle {
  278. get {
  279. return _sessionHandle;
  280. }
  281. }
  282. internal bool HasOpenResult {
  283. get {
  284. return _hasOpenResult;
  285. }
  286. }
  287. #if DEBUG
  288. internal void InvalidateDebugOnlyCopyOfSniContext() {
  289. _debugOnlyCopyOfSniContext = SniContext.Undefined;
  290. }
  291. #endif
  292. internal bool IsOrphaned {
  293. get {
  294. Debug.Assert((0 == _activateCount && !_owner.IsAlive) // in pool
  295. || (1 == _activateCount && _owner.IsAlive && _owner.Target != null)
  296. || (1 == _activateCount && !_owner.IsAlive), "Unknown state on TdsParserStateObject.IsOrphaned!");
  297. return (0 != _activateCount && !_owner.IsAlive);
  298. }
  299. }
  300. internal object Owner {
  301. set {
  302. Debug.Assert(value == null || !_owner.IsAlive || ((value is SqlDataReader) && (((SqlDataReader)value).Command == _owner.Target)), "Should not be changing the owner of an owned stateObj");
  303. SqlDataReader reader=value as SqlDataReader;
  304. if (reader == null) {
  305. _readerState = null;
  306. }
  307. else {
  308. _readerState = reader._sharedState;
  309. }
  310. _owner.Target = value;
  311. }
  312. }
  313. internal bool HasOwner {
  314. get {
  315. return _owner.IsAlive;
  316. }
  317. }
  318. internal TdsParser Parser {
  319. get {
  320. return _parser;
  321. }
  322. }
  323. internal SniContext SniContext {
  324. get {
  325. return _sniContext;
  326. }
  327. set {
  328. _sniContext = value;
  329. #if DEBUG
  330. _debugOnlyCopyOfSniContext = value;
  331. #endif
  332. }
  333. }
  334. internal UInt32 Status {
  335. get {
  336. if (_sessionHandle != null) {
  337. return _sessionHandle.Status;
  338. }
  339. else { // SQL BU DT 395431.
  340. return TdsEnums.SNI_UNINITIALIZED;
  341. }
  342. }
  343. }
  344. internal bool TimeoutHasExpired {
  345. get {
  346. Debug.Assert(0 == _timeoutMilliseconds || 0 == _timeoutTime, "_timeoutTime hasn't been reset");
  347. return TdsParserStaticMethods.TimeoutHasExpired(_timeoutTime);
  348. }
  349. }
  350. internal long TimeoutTime {
  351. get {
  352. if (0 != _timeoutMilliseconds) {
  353. _timeoutTime = TdsParserStaticMethods.GetTimeout(_timeoutMilliseconds);
  354. _timeoutMilliseconds = 0;
  355. }
  356. return _timeoutTime;
  357. }
  358. set {
  359. _timeoutMilliseconds = 0;
  360. _timeoutTime = value;
  361. }
  362. }
  363. internal int GetTimeoutRemaining() {
  364. int remaining;
  365. if (0 != _timeoutMilliseconds) {
  366. remaining = (int)Math.Min((long)Int32.MaxValue, _timeoutMilliseconds);
  367. _timeoutTime = TdsParserStaticMethods.GetTimeout(_timeoutMilliseconds);
  368. _timeoutMilliseconds = 0;
  369. }
  370. else {
  371. remaining = TdsParserStaticMethods.GetTimeoutMilliseconds(_timeoutTime);
  372. }
  373. return remaining;
  374. }
  375. internal bool TryStartNewRow(bool isNullCompressed, int nullBitmapColumnsCount = 0) {
  376. Debug.Assert(!isNullCompressed || nullBitmapColumnsCount > 0, "Null-Compressed row requires columns count");
  377. if (_snapshot != null) {
  378. _snapshot.CloneNullBitmapInfo();
  379. }
  380. // initialize or unset null bitmap information for the current row
  381. if (isNullCompressed){
  382. // assert that NBCROW is not in use by Yukon or before
  383. Debug.Assert(_parser.IsKatmaiOrNewer, "NBCROW is sent by pre-Katmai server");
  384. if (!_nullBitmapInfo.TryInitialize(this, nullBitmapColumnsCount)) {
  385. return false;
  386. }
  387. }
  388. else {
  389. _nullBitmapInfo.Clean();
  390. }
  391. return true;
  392. }
  393. internal bool IsRowTokenReady() {
  394. // Removing one byte since TryReadByteArray\TryReadByte will aggressively read the next packet if there is no data left - so we need to ensure there is a spare byte
  395. int bytesRemaining = Math.Min(_inBytesPacket, _inBytesRead - _inBytesUsed) - 1;
  396. if (bytesRemaining > 0) {
  397. if (_inBuff[_inBytesUsed] == TdsEnums.SQLROW) {
  398. // At a row token, so we're ready
  399. return true;
  400. }
  401. else if (_inBuff[_inBytesUsed] == TdsEnums.SQLNBCROW) {
  402. // NBC row token, ensure that we have enough data for the bitmap
  403. // SQLNBCROW + Null Bitmap (copied from NullBitmap.TryInitialize)
  404. int bytesToRead = 1 + (_cleanupMetaData.Length + 7) / 8;
  405. return (bytesToRead <= bytesRemaining);
  406. }
  407. }
  408. // No data left, or not at a row token
  409. return false;
  410. }
  411. internal bool IsNullCompressionBitSet(int columnOrdinal) {
  412. return _nullBitmapInfo.IsGuaranteedNull(columnOrdinal);
  413. }
  414. private struct NullBitmap {
  415. private byte[] _nullBitmap;
  416. private int _columnsCount; // set to 0 if not used or > 0 for NBC rows
  417. internal bool TryInitialize(TdsParserStateObject stateObj, int columnsCount) {
  418. _columnsCount = columnsCount;
  419. // 1-8 columns need 1 byte
  420. // 9-16: 2 bytes, and so on
  421. int bitmapArrayLength = (columnsCount + 7) / 8;
  422. // allow reuse of previously allocated bitmap
  423. if (_nullBitmap == null || _nullBitmap.Length != bitmapArrayLength) {
  424. _nullBitmap = new byte[bitmapArrayLength];
  425. }
  426. // read the null bitmap compression information from TDS
  427. if (!stateObj.TryReadByteArray(_nullBitmap, 0, _nullBitmap.Length)) {
  428. return false;
  429. }
  430. if (Bid.TraceOn) {
  431. Bid.Trace("<sc.TdsParserStateObject.NullBitmap.Initialize|INFO|ADV> %d#, NBCROW bitmap received, column count = %d\n", stateObj.ObjectID, columnsCount);
  432. Bid.TraceBin("<sc.TdsParserStateObject.NullBitmap.Initialize|INFO|ADV> NBCROW bitmap data: ", _nullBitmap, (UInt16)_nullBitmap.Length);
  433. }
  434. return true;
  435. }
  436. internal bool ReferenceEquals(NullBitmap obj) {
  437. return object.ReferenceEquals(_nullBitmap, obj._nullBitmap);
  438. }
  439. internal NullBitmap Clone()
  440. {
  441. NullBitmap newBitmap = new NullBitmap();
  442. newBitmap._nullBitmap = _nullBitmap == null ? null : (byte[])_nullBitmap.Clone();
  443. newBitmap._columnsCount = _columnsCount;
  444. return newBitmap;
  445. }
  446. internal void Clean() {
  447. _columnsCount = 0;
  448. // no need to free _nullBitmap array - it is cached for the next row
  449. }
  450. /// <summary>
  451. /// If this method returns true, the value is guaranteed to be null. This is not true vice versa:
  452. /// if the bitmat value is false (if this method returns false), the value can be either null or non-null - no guarantee in this case.
  453. /// To determine whether it is null or not, read it from the TDS (per NBCROW design spec, for IMAGE/TEXT/NTEXT columns server might send
  454. /// bitmap = 0, when the actual value is null).
  455. /// </summary>
  456. internal bool IsGuaranteedNull(int columnOrdinal) {
  457. if (_columnsCount == 0) {
  458. // not an NBC row
  459. return false;
  460. }
  461. Debug.Assert(columnOrdinal >= 0 && columnOrdinal < _columnsCount, "Invalid column ordinal");
  462. byte testBit = (byte)(1 << (columnOrdinal & 0x7)); // columnOrdinal & 0x7 == columnOrdinal MOD 0x7
  463. byte testByte = _nullBitmap[columnOrdinal >> 3];
  464. return (testBit & testByte) != 0;
  465. }
  466. }
  467. /////////////////////
  468. // General methods //
  469. /////////////////////
  470. // If this object is part of a TdsParserSessionPool, then this *must* be called inside the pool's lock
  471. internal void Activate(object owner) {
  472. Debug.Assert(_parser.MARSOn, "Can not activate a non-MARS connection");
  473. Owner = owner; // must assign an owner for reclaimation to work
  474. int result = Interlocked.Increment(ref _activateCount); // must have non-zero activation count for reclaimation to work too.
  475. Debug.Assert(result == 1, "invalid deactivate count");
  476. }
  477. // This method is only called by the command or datareader as a result of a user initiated
  478. // cancel request.
  479. internal void Cancel(int objectID) {
  480. bool hasLock = false;
  481. try {
  482. // Keep looping until we either grabbed the lock (and therefore sent attention) or the connection closes\breaks
  483. while ((!hasLock) && (_parser.State != TdsParserState.Closed) && (_parser.State != TdsParserState.Broken)) {
  484. Monitor.TryEnter(this, _waitForCancellationLockPollTimeout, ref hasLock);
  485. if (hasLock) { // Lock for the time being - since we need to synchronize the attention send.
  486. // At some point in the future, I hope to remove this.
  487. // This lock is also protecting against concurrent close and async continuations
  488. // don't allow objectID -1 since it is reserved for 'not associated with a command'
  489. // yes, the 2^32-1 comand won't cancel - but it also won't cancel when we don't want it
  490. if ((!_cancelled) && (objectID == _allowObjectID) && (objectID != -1)) {
  491. _cancelled = true;
  492. if (_pendingData && !_attentionSent) {
  493. bool hasParserLock = false;
  494. // Keep looping until we have the parser lock (and so are allowed to write), or the conneciton closes\breaks
  495. while ((!hasParserLock) && (_parser.State != TdsParserState.Closed) && (_parser.State != TdsParserState.Broken)) {
  496. try {
  497. _parser.Connection._parserLock.Wait(canReleaseFromAnyThread: false, timeout: _waitForCancellationLockPollTimeout, lockTaken: ref hasParserLock);
  498. if (hasParserLock) {
  499. _parser.Connection.ThreadHasParserLockForClose = true;
  500. SendAttention();
  501. }
  502. }
  503. finally {
  504. if (hasParserLock) {
  505. if (_parser.Connection.ThreadHasParserLockForClose) {
  506. _parser.Connection.ThreadHasParserLockForClose = false;
  507. }
  508. _parser.Connection._parserLock.Release();
  509. }
  510. }
  511. }
  512. }
  513. }
  514. }
  515. }
  516. }
  517. finally {
  518. if (hasLock) {
  519. Monitor.Exit(this);
  520. }
  521. }
  522. }
  523. // CancelRequest - use to cancel while writing a request to the server
  524. //
  525. // o none of the request might have been sent to the server, simply reset the buffer,
  526. // sending attention does not hurt
  527. // o the request was partially written. Send an ignore header to the server. attention is
  528. // required if the server was waiting for data (e.g. insert bulk rows)
  529. // o the request was completely written out and the server started to process the request.
  530. // attention is required to have the server stop processing.
  531. //
  532. internal void CancelRequest() {
  533. ResetBuffer(); // clear out unsent buffer
  534. // VSDD#903514, if the first sqlbulkcopy timeout, _outputPacketNumber may not be 1,
  535. // the next sqlbulkcopy (same connection string) requires this to be 1, hence reset
  536. // it here when exception happens in the first sqlbulkcopy
  537. _outputPacketNumber = 1;
  538. // VSDD#907507, if bulkcopy write timeout happens, it already sent the attention,
  539. // so no need to send it again
  540. if (!_bulkCopyWriteTimeout)
  541. {
  542. SendAttention();
  543. Parser.ProcessPendingAck(this);
  544. }
  545. }
  546. public void CheckSetResetConnectionState(UInt32 error, CallbackType callbackType) {
  547. // Should only be called for MARS - that is the only time we need to take
  548. // the ResetConnection lock!
  549. // SQL BU DT 333026 - it was raised in a security review by [....] questioning whether
  550. // we need to actually process the resulting packet (sp_reset ack or error) to know if the
  551. // reset actually succeeded. There was a concern that if the reset failed and we proceeded
  552. // there might be a security issue present. We have been assured by the server that if
  553. // sp_reset fails, they guarantee they will kill the resulting connection. So - it is
  554. // safe for us to simply receive the packet and then consume the pre-login later.
  555. Debug.Assert(_parser.MARSOn, "Should not be calling CheckSetResetConnectionState on non MARS connection");
  556. if (_fResetEventOwned) {
  557. if (callbackType == CallbackType.Read && TdsEnums.SNI_SUCCESS == error) {
  558. // RESET SUCCEEDED!
  559. // If we are on read callback and no error occurred (and we own reset event) -
  560. // then we sent the sp_reset_connection and so we need to reset sp_reset_connection
  561. // flag to false, and then release the ResetEvent.
  562. _parser._fResetConnection = false;
  563. _fResetConnectionSent = false;
  564. _fResetEventOwned = !_parser._resetConnectionEvent.Set();
  565. Debug.Assert(!_fResetEventOwned, "Invalid AutoResetEvent state!");
  566. }
  567. if (TdsEnums.SNI_SUCCESS != error) {
  568. // RESET FAILED!
  569. //
  570. // If write or read failed with reset, we need to clear event but not mark connection
  571. // as reset.
  572. _fResetConnectionSent = false;
  573. _fResetEventOwned = !_parser._resetConnectionEvent.Set();
  574. Debug.Assert(!_fResetEventOwned, "Invalid AutoResetEvent state!");
  575. }
  576. }
  577. }
  578. internal void CloseSession() {
  579. ResetCancelAndProcessAttention();
  580. #if DEBUG
  581. InvalidateDebugOnlyCopyOfSniContext();
  582. #endif
  583. Parser.PutSession(this);
  584. }
  585. private void ResetCancelAndProcessAttention() {
  586. // This method is shared by CloseSession initiated by DataReader.Close or completed
  587. // command execution, as well as the session reclaimation code for cases where the
  588. // DataReader is opened and then GC'ed.
  589. lock(this) {
  590. // Reset cancel state.
  591. _cancelled = false;
  592. _allowObjectID = -1;
  593. if (_attentionSent) {
  594. // Make sure we're cleaning up the AttentionAck if Cancel happened before taking the lock.
  595. // We serialize Cancel/CloseSession to prevent a race condition between these two states.
  596. // The problem is that both sending and receiving attentions are time taking
  597. // operations.
  598. #if DEBUG
  599. TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
  600. RuntimeHelpers.PrepareConstrainedRegions();
  601. try
  602. {
  603. tdsReliabilitySection.Start();
  604. #endif //DEBUG
  605. Parser.ProcessPendingAck(this);
  606. #if DEBUG
  607. }
  608. finally
  609. {
  610. tdsReliabilitySection.Stop();
  611. }
  612. #endif //DEBUG
  613. }
  614. _internalTimeout = false;
  615. }
  616. }
  617. private SNINativeMethodWrapper.ConsumerInfo CreateConsumerInfo(bool async) {
  618. SNINativeMethodWrapper.ConsumerInfo myInfo = new SNINativeMethodWrapper.ConsumerInfo();
  619. Debug.Assert(_outBuff.Length == _inBuff.Length, "Unexpected unequal buffers.");
  620. myInfo.defaultBufferSize = _outBuff.Length; // Obtain packet size from outBuff size.
  621. if (async) {
  622. myInfo.readDelegate = SNILoadHandle.SingletonInstance.ReadAsyncCallbackDispatcher;
  623. myInfo.writeDelegate = SNILoadHandle.SingletonInstance.WriteAsyncCallbackDispatcher;
  624. _gcHandle = GCHandle.Alloc(this, GCHandleType.Normal);
  625. myInfo.key = (IntPtr)_gcHandle;
  626. }
  627. return myInfo;
  628. }
  629. internal void CreatePhysicalSNIHandle(string serverName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, byte[] spnBuffer, bool flushCache, bool async, bool fParallel) {
  630. SNINativeMethodWrapper.ConsumerInfo myInfo = CreateConsumerInfo(async);
  631. // Translate to SNI timeout values (Int32 milliseconds)
  632. long timeout;
  633. if (Int64.MaxValue == timerExpire) {
  634. timeout = Int32.MaxValue;
  635. }
  636. else {
  637. timeout = ADP.TimerRemainingMilliseconds(timerExpire);
  638. if (timeout > Int32.MaxValue) {
  639. timeout = Int32.MaxValue;
  640. }
  641. else if (0 > timeout) {
  642. timeout = 0;
  643. }
  644. }
  645. _sessionHandle = new SNIHandle(myInfo, serverName, spnBuffer, ignoreSniOpenTimeout, checked((int)timeout), out instanceName, flushCache, !async, fParallel);
  646. }
  647. internal bool Deactivate() {
  648. bool goodForReuse = false;
  649. try {
  650. TdsParserState state = Parser.State;
  651. if (state != TdsParserState.Broken && state != TdsParserState.Closed) {
  652. if (_pendingData) {
  653. Parser.DrainData(this); // This may throw - taking us to catch block.
  654. }
  655. if (HasOpenResult) { // SQL BU DT 383773 - need to decrement openResultCount for all pending operations.
  656. DecrementOpenResultCount();
  657. }
  658. ResetCancelAndProcessAttention();
  659. goodForReuse = true;
  660. }
  661. }
  662. catch (Exception e) {
  663. if (!ADP.IsCatchableExceptionType(e)) {
  664. throw;
  665. }
  666. ADP.TraceExceptionWithoutRethrow(e);
  667. }
  668. return goodForReuse;
  669. }
  670. // If this object is part of a TdsParserSessionPool, then this *must* be called inside the pool's lock
  671. internal void RemoveOwner() {
  672. if (_parser.MARSOn) {
  673. // We only care about the activation count for MARS connections
  674. int result = Interlocked.Decrement(ref _activateCount); // must have non-zero activation count for reclaimation to work too.
  675. Debug.Assert(result == 0, "invalid deactivate count");
  676. }
  677. Owner = null;
  678. }
  679. internal void DecrementOpenResultCount() {
  680. if (_executedUnderTransaction == null) {
  681. // If we were not executed under a transaction - decrement the global count
  682. // on the parser.
  683. _parser.DecrementNonTransactedOpenResultCount();
  684. }
  685. else {
  686. // If we were executed under a transaction - decrement the count on the transaction.
  687. _executedUnderTransaction.DecrementAndObtainOpenResultCount();
  688. _executedUnderTransaction = null;
  689. }
  690. _hasOpenResult = false;
  691. }
  692. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  693. internal int DecrementPendingCallbacks(bool release) {
  694. int remaining = Interlocked.Decrement(ref _pendingCallbacks);
  695. if (Bid.AdvancedOn) {
  696. Bid.Trace("<sc.TdsParserStateObject.DecrementPendingCallbacks|ADV> %d#, after decrementing _pendingCallbacks: %d\n", ObjectID, _pendingCallbacks);
  697. }
  698. if ((0 == remaining || release) && _gcHandle.IsAllocated) {
  699. if (Bid.AdvancedOn) {
  700. Bid.Trace("<sc.TdsParserStateObject.DecrementPendingCallbacks|ADV> %d#, FREEING HANDLE!\n", ObjectID);
  701. }
  702. _gcHandle.Free();
  703. }
  704. // NOTE: TdsParserSessionPool may call DecrementPendingCallbacks on a TdsParserStateObject which is already disposed
  705. // This is not dangerous (since the stateObj is no longer in use), but we need to add a workaround in the assert for it
  706. Debug.Assert((remaining == -1 && _sessionHandle == null) || (0 <= remaining && remaining < 3), string.Format("_pendingCallbacks values is invalid after decrementing: {0}", remaining));
  707. return remaining;
  708. }
  709. internal void Dispose() {
  710. SafeHandle packetHandle = _sniPacket;
  711. SafeHandle sessionHandle = _sessionHandle;
  712. SafeHandle asyncAttnPacket = _sniAsyncAttnPacket;
  713. _sniPacket = null;
  714. _sessionHandle = null;
  715. _sniAsyncAttnPacket = null;
  716. Timer networkPacketTimeout = _networkPacketTimeout;
  717. if (networkPacketTimeout != null) {
  718. _networkPacketTimeout = null;
  719. networkPacketTimeout.Dispose();
  720. }
  721. Debug.Assert(Volatile.Read(ref _readingCount) >= 0, "_readingCount is negative");
  722. if (Volatile.Read(ref _readingCount) > 0) {
  723. // if _reading is true, we need to wait for it to complete
  724. // if _reading is false, then future read attempts will
  725. // already see the null _sessionHandle and abort.
  726. // We block after nulling _sessionHandle but before disposing it
  727. // to give a chance for a read that has already grabbed the
  728. // handle to complete.
  729. SpinWait.SpinUntil(() => Volatile.Read(ref _readingCount) == 0);
  730. }
  731. if (null != sessionHandle || null != packetHandle) {
  732. // Comment CloseMARSSession
  733. //
  734. //
  735. RuntimeHelpers.PrepareConstrainedRegions();
  736. try {} finally {
  737. if (packetHandle != null) {
  738. packetHandle.Dispose();
  739. }
  740. if (asyncAttnPacket != null) {
  741. asyncAttnPacket.Dispose ();
  742. }
  743. if (sessionHandle != null) {
  744. sessionHandle.Dispose();
  745. DecrementPendingCallbacks(true); // Will dispose of GC handle.
  746. }
  747. }
  748. }
  749. if (_writePacketCache != null) {
  750. lock (_writePacketLockObject) {
  751. RuntimeHelpers.PrepareConstrainedRegions();
  752. try {} finally {
  753. _writePacketCache.Dispose();
  754. // Do not set _writePacketCache to null, just in case a WriteAsyncCallback completes after this point
  755. }
  756. }
  757. }
  758. }
  759. internal Int32 IncrementAndObtainOpenResultCount(SqlInternalTransaction transaction) {
  760. _hasOpenResult = true;
  761. if (transaction == null) {
  762. // If we are not passed a transaction, we are not executing under a transaction
  763. // and thus we should increment the global connection result count.
  764. return _parser.IncrementNonTransactedOpenResultCount();
  765. }
  766. else {
  767. // If we are passed a transaction, we are executing under a transaction
  768. // and thus we should increment the transaction's result count.
  769. _executedUnderTransaction = transaction;
  770. return transaction.IncrementAndObtainOpenResultCount();
  771. }
  772. }
  773. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  774. internal int IncrementPendingCallbacks() {
  775. int remaining = Interlocked.Increment(ref _pendingCallbacks);
  776. if (Bid.AdvancedOn) {
  777. Bid.Trace("<sc.TdsParserStateObject.IncrementPendingCallbacks|ADV> %d#, after incrementing _pendingCallbacks: %d\n", ObjectID, _pendingCallbacks);
  778. }
  779. Debug.Assert(0 < remaining && remaining <= 3, string.Format("_pendingCallbacks values is invalid after incrementing: {0}", remaining));
  780. return remaining;
  781. }
  782. internal void SetTimeoutSeconds(int timeout) {
  783. SetTimeoutMilliseconds((long)timeout * 1000L);
  784. }
  785. internal void SetTimeoutMilliseconds(long timeout) {
  786. if (timeout <= 0) {
  787. // 0 or less (i.e. Timespan.Infinite) == infinite (which is represented by Int64.MaxValue)
  788. _timeoutMilliseconds = 0;
  789. _timeoutTime = Int64.MaxValue;
  790. }
  791. else {
  792. _timeoutMilliseconds = timeout;
  793. _timeoutTime = 0;
  794. }
  795. }
  796. internal void StartSession(int objectID) {
  797. _allowObjectID = objectID;
  798. }
  799. internal void ThrowExceptionAndWarning(bool callerHasConnectionLock = false, bool asyncClose = false) {
  800. _parser.ThrowExceptionAndWarning(this, callerHasConnectionLock, asyncClose);
  801. }
  802. ////////////////////////////////////////////
  803. // TDS Packet/buffer manipulation methods //
  804. ////////////////////////////////////////////
  805. internal Task ExecuteFlush() {
  806. lock (this) {
  807. if (_cancelled && 1 == _outputPacketNumber) {
  808. ResetBuffer();
  809. _cancelled = false;
  810. throw SQL.OperationCancelled();
  811. }
  812. else {
  813. Task writePacketTask = WritePacket(TdsEnums.HARDFLUSH);
  814. if (writePacketTask == null) {
  815. _pendingData = true;
  816. _messageStatus = 0;
  817. return null;
  818. }
  819. else {
  820. return AsyncHelper.CreateContinuationTask(writePacketTask, () => { _pendingData = true; _messageStatus = 0; });
  821. }
  822. }
  823. }
  824. }
  825. // Processes the tds header that is present in the buffer
  826. internal bool TryProcessHeader() {
  827. Debug.Assert(_inBytesPacket == 0, "there should not be any bytes left in packet when ReadHeader is called");
  828. // if the header splits buffer reads - special case!
  829. if ((_partialHeaderBytesRead > 0) || (_inBytesUsed + _inputHeaderLen > _inBytesRead)) {
  830. // VSTS 219884: when some kind of MITM (man-in-the-middle) tool splits the network packets, the message header can be split over
  831. // several network packets.
  832. // Note: cannot use ReadByteArray here since it uses _inBytesPacket which is not set yet.
  833. do {
  834. int copy = Math.Min(_inBytesRead - _inBytesUsed, _inputHeaderLen - _partialHeaderBytesRead);
  835. Debug.Assert(copy > 0, "ReadNetworkPacket read empty buffer");
  836. Buffer.BlockCopy(_inBuff, _inBytesUsed, _partialHeaderBuffer, _partialHeaderBytesRead, copy);
  837. _partialHeaderBytesRead += copy;
  838. _inBytesUsed += copy;
  839. Debug.Assert(_partialHeaderBytesRead <= _inputHeaderLen, "Read more bytes for header than required");
  840. if (_partialHeaderBytesRead == _inputHeaderLen) {
  841. // All read
  842. _partialHeaderBytesRead = 0;
  843. _inBytesPacket = ((int)_partialHeaderBuffer[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8 |
  844. (int)_partialHeaderBuffer[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1]) - _inputHeaderLen;
  845. _messageStatus = _partialHeaderBuffer[1];
  846. }
  847. else {
  848. Debug.Assert(_inBytesUsed == _inBytesRead, "Did not use all data while reading partial header");
  849. // Require more data
  850. if (_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed) {
  851. // NOTE: ReadNetworkPacket does nothing if the parser state is closed or broken
  852. // to avoid infinite loop, we raise an exception
  853. ThrowExceptionAndWarning();
  854. //
  855. return true;
  856. }
  857. if (!TryReadNetworkPacket()) {
  858. return false;
  859. }
  860. if (_internalTimeout) {
  861. ThrowExceptionAndWarning();
  862. //
  863. return true;
  864. }
  865. }
  866. } while (_partialHeaderBytesRead != 0); // This is reset to 0 once we have read everything that we need
  867. AssertValidState();
  868. }
  869. else {
  870. // normal header processing...
  871. _messageStatus = _inBuff[_inBytesUsed + 1];
  872. _inBytesPacket = ((int)_inBuff[_inBytesUsed + TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8 |
  873. (int)_inBuff[_inBytesUsed + TdsEnums.HEADER_LEN_FIELD_OFFSET + 1]) - _inputHeaderLen;
  874. _inBytesUsed += _inputHeaderLen;
  875. AssertValidState();
  876. }
  877. if (_inBytesPacket < 0) {
  878. // either TDS stream is corrupted or there is multithreaded misuse of connection
  879. // NOTE: usually we do not proactively apply checks to TDS data, but this situation happened several times
  880. // and caused infinite loop in CleanWire (VSTFDEVDIV\DEVDIV2:149937)
  881. throw SQL.ParsingError();
  882. }
  883. return true;
  884. }
  885. // This ensure that there is data available to be read in the buffer and that the header has been processed
  886. // NOTE: This method (and all it calls) should be retryable without replaying a snapshot
  887. internal bool TryPrepareBuffer() {
  888. TdsParser.ReliabilitySection.Assert("unreliable call to ReadBuffer"); // you need to setup for a thread abort somewhere before you call this method
  889. Debug.Assert(_inBuff != null, "packet buffer should not be null!");
  890. // Header spans packets, or we haven't read the header yet - process header
  891. if ((_inBytesPacket == 0) && (_inBytesUsed < _inBytesRead)) {
  892. if (!TryProcessHeader()) {
  893. return false;
  894. }
  895. Debug.Assert(_inBytesPacket != 0, "_inBytesPacket cannot be 0 after processing header!");
  896. AssertValidState();
  897. }
  898. // If we're out of data, need to read more
  899. if (_inBytesUsed == _inBytesRead) {
  900. // If the _inBytesPacket is not zero, then we have data left in the packet, but the data in the packet
  901. // spans the buffer, so we can read any amount of data possible, and we do not need to call ProcessHeader
  902. // because there isn't a header at the beginning of the data that we are reading.
  903. if (_inBytesPacket > 0) {
  904. if (!TryReadNetworkPacket()) {
  905. return false;
  906. }
  907. }
  908. else if (_inBytesPacket == 0) {
  909. // Else we have finished the packet and so we must read as much data as possible
  910. if (!TryReadNetworkPacket()) {
  911. return false;
  912. }
  913. if (!TryProcessHeader()) {
  914. return false;
  915. }
  916. Debug.Assert(_inBytesPacket != 0, "_inBytesPacket cannot be 0 after processing header!");
  917. if (_inBytesUsed == _inBytesRead) {
  918. // we read a header but didn't get anything else except it
  919. // VSTS 219884: it can happen that the TDS packet header and its data are split across two network packets.
  920. // Read at least one more byte to get/cache the first data portion of this TDS packet
  921. if (!TryReadNetworkPacket()) {
  922. return false;
  923. }
  924. }
  925. }
  926. else {
  927. Debug.Assert(false, "entered negative _inBytesPacket loop");
  928. }
  929. AssertValidState();
  930. }
  931. return true;
  932. }
  933. internal void ResetBuffer() {
  934. _outBytesUsed = _outputHeaderLen;
  935. }
  936. internal bool SetPacketSize(int size) {
  937. if (size > TdsEnums.MAX_PACKET_SIZE) {
  938. throw SQL.InvalidPacketSize();
  939. }
  940. Debug.Assert(size >= 1, "Cannot set packet size to less than 1.");
  941. Debug.Assert( (_outBuff == null && _inBuff == null) ||
  942. (_outBuff.Length == _inBuff.Length),
  943. "Buffers are not in consistent state");
  944. Debug.Assert( (_outBuff == null && _inBuff == null) ||
  945. this == _parser._physicalStateObj,
  946. "SetPacketSize should only be called on a stateObj with null buffers on the physicalStateObj!");
  947. Debug.Assert( _inBuff == null
  948. ||
  949. (_parser.IsYukonOrNewer &&
  950. _outBytesUsed == (_outputHeaderLen + BitConverter.ToInt32(_outBuff, _outputHeaderLen)) &&
  951. _outputPacketNumber == 1)
  952. ||
  953. (_outBytesUsed == _outputHeaderLen && _outputPacketNumber == 1),
  954. "SetPacketSize called with data in the buffer!");
  955. if (_inBuff == null || _inBuff.Length != size) { // We only check _inBuff, since two buffers should be consistent.
  956. // Allocate or re-allocate _inBuff.
  957. if (_inBuff == null) {
  958. _inBuff = new byte[size];
  959. _inBytesRead = 0;
  960. _inBytesUsed = 0;
  961. }
  962. else if (size != _inBuff.Length) {
  963. // If new size is other than existing...
  964. if (_inBytesRead > _inBytesUsed) {
  965. // if we still have data left in the buffer we must keep that array reference and then copy into new one
  966. byte[] temp = _inBuff;
  967. _inBuff = new byte[size];
  968. // copy remainder of unused data
  969. int remainingData = _inBytesRead - _inBytesUsed;
  970. if ((temp.Length < _inBytesUsed + remainingData) || (_inBuff.Length < remainingData)) {
  971. string errormessage = Res.GetString(Res.SQL_InvalidInternalPacketSize) + ' ' + temp.Length + ", " + _inBytesUsed + ", " + remainingData + ", " + _inBuff.Length;
  972. throw SQL.InvalidInternalPacketSize (errormessage);
  973. }
  974. Buffer.BlockCopy(temp, _inBytesUsed, _inBuff, 0, remainingData);
  975. _inBytesRead = _inBytesRead - _inBytesUsed;
  976. _inBytesUsed = 0;
  977. AssertValidState();
  978. }
  979. else {
  980. // buffer is empty - just create the new one that is double the size of the old one
  981. _inBuff = new byte[size];
  982. _inBytesRead = 0;
  983. _inBytesUsed = 0;
  984. }
  985. }
  986. // Always re-allocate _outBuff - assert is above to verify state.
  987. _outBuff = new byte[size];
  988. _outBytesUsed = _outputHeaderLen;
  989. AssertValidState();
  990. return true;
  991. }
  992. return false;
  993. }
  994. ///////////////////////////////////////
  995. // Buffer read methods - data values //
  996. ///////////////////////////////////////
  997. // look at the next byte without pulling it off the wire, don't just returun _inBytesUsed since we may
  998. // have to go to the network to get the next byte.
  999. internal bool TryPeekByte(out byte value) {
  1000. if (!TryReadByte(out value)) {
  1001. return false;
  1002. }
  1003. // now do fixup
  1004. _inBytesPacket++;
  1005. _inBytesUsed--;
  1006. AssertValidState();
  1007. return true;
  1008. }
  1009. // Takes a byte array, an offset, and a len and fills the array from the offset to len number of
  1010. // bytes from the in buffer.
  1011. public bool TryReadByteArray(byte[] buff, int offset, int len) {
  1012. int ignored;
  1013. return TryReadByteArray(buff, offset, len, out ignored);
  1014. }
  1015. // NOTE: This method must be retriable WITHOUT replaying a snapshot
  1016. // Every time you call this method increment the offset and decrease len by the value of totalRead
  1017. public bool TryReadByteArray(byte[] buff, int offset, int len, out int totalRead) {
  1018. TdsParser.ReliabilitySection.Assert("unreliable call to ReadByteArray"); // you need to setup for a thread abort somewhere before you call this method
  1019. totalRead = 0;
  1020. #if DEBUG
  1021. if (_snapshot != null && _snapshot.DoPend()) {
  1022. _networkPacketTaskSource = new TaskCompletionSource<object>();
  1023. Thread.MemoryBarrier();
  1024. if (_forcePendingReadsToWaitForUser) {
  1025. _realNetworkPacketTaskSource = new TaskCompletionSource<object>();
  1026. _realNetworkPacketTaskSource.SetResult(null);
  1027. }
  1028. else {
  1029. _networkPacketTaskSource.TrySetResult(null);
  1030. }
  1031. return false;
  1032. }
  1033. #endif
  1034. Debug.Assert(buff == null || buff.Length >= len, "Invalid length sent to ReadByteArray()!");
  1035. // loop through and read up to array length
  1036. while (len > 0) {
  1037. if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead)) {
  1038. if (!TryPrepareBuffer()) {
  1039. return false;
  1040. }
  1041. }
  1042. int bytesToRead = Math.Min(len, Math.Min(_inBytesPacket, _inBytesRead - _inBytesUsed));
  1043. Debug.Assert(bytesToRead > 0, "0 byte read in TryReadByteArray");
  1044. if (buff != null) {
  1045. Buffer.BlockCopy(_inBuff, _inBytesUsed, buff, offset + totalRead, bytesToRead);
  1046. }
  1047. totalRead += bytesToRead;
  1048. _inBytesUsed += bytesToRead;
  1049. _inBytesPacket -= bytesToRead;
  1050. len -= bytesToRead;
  1051. AssertValidState();
  1052. }
  1053. if ((_messageStatus != TdsEnums.ST_EOM) && ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead))) {
  1054. if (!TryPrepareBuffer()) {
  1055. return false;
  1056. }
  1057. }
  1058. AssertValidState();
  1059. return true;
  1060. }
  1061. // Takes no arguments and returns a byte from the buffer. If the buffer is empty, it is filled
  1062. // before the byte is returned.
  1063. internal bool TryReadByte(out byte value) {
  1064. TdsParser.ReliabilitySection.Assert("unreliable call to ReadByte"); // you need to setup for a thread abort somewhere before you call this method
  1065. Debug.Assert(_inBytesUsed >= 0 && _inBytesUsed <= _inBytesRead, "ERROR - TDSParser: _inBytesUsed < 0 or _inBytesUsed > _inBytesRead");
  1066. value = 0;
  1067. #if DEBUG
  1068. if (_snapshot != null && _snapshot.DoPend()) {
  1069. _networkPacketTaskSource = new TaskCompletionSource<object>();
  1070. Thread.MemoryBarrier();
  1071. if (_forcePendingReadsToWaitForUser) {
  1072. _realNetworkPacketTaskSource = new TaskCompletionSource<object>();
  1073. _realNetworkPacketTaskSource.SetResult(null);
  1074. }
  1075. else {
  1076. _networkPacketTaskSource.TrySetResult(null);
  1077. }
  1078. return false;
  1079. }
  1080. #endif
  1081. if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead)) {
  1082. if (!TryPrepareBuffer()) {
  1083. return false;
  1084. }
  1085. }
  1086. // decrement the number of bytes left in the packet
  1087. _inBytesPacket--;
  1088. Debug.Assert(_inBytesPacket >= 0, "ERROR - TDSParser: _inBytesPacket < 0");
  1089. // return the byte from the buffer and increment the counter for number of bytes used in the in buffer
  1090. value = (_inBuff[_inBytesUsed++]);
  1091. AssertValidState();
  1092. return true;
  1093. }
  1094. internal bool TryReadChar(out char value) {
  1095. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1096. byte[] buffer;
  1097. int offset;
  1098. if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2)) {
  1099. // If the char isn't fully in the buffer, or if it isn't fully in the packet,
  1100. // then use ReadByteArray since the logic is there to take care of that.
  1101. if (!TryReadByteArray(_bTmp, 0, 2)) {
  1102. value = '\0';
  1103. return false;
  1104. }
  1105. buffer = _bTmp;
  1106. offset = 0;
  1107. }
  1108. else {
  1109. // The entire char is in the packet and in the buffer, so just return it
  1110. // and take care of the counters.
  1111. buffer = _inBuff;
  1112. offset = _inBytesUsed;
  1113. _inBytesUsed += 2;
  1114. _inBytesPacket -= 2;
  1115. }
  1116. AssertValidState();
  1117. value = (char)((buffer[offset + 1] << 8) + buffer[offset]);
  1118. return true;
  1119. }
  1120. internal bool TryReadInt16(out short value) {
  1121. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1122. byte[] buffer;
  1123. int offset;
  1124. if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2)) {
  1125. // If the int16 isn't fully in the buffer, or if it isn't fully in the packet,
  1126. // then use ReadByteArray since the logic is there to take care of that.
  1127. if (!TryReadByteArray(_bTmp, 0, 2)) {
  1128. value = default(short);
  1129. return false;
  1130. }
  1131. buffer = _bTmp;
  1132. offset = 0;
  1133. }
  1134. else {
  1135. // The entire int16 is in the packet and in the buffer, so just return it
  1136. // and take care of the counters.
  1137. buffer = _inBuff;
  1138. offset = _inBytesUsed;
  1139. _inBytesUsed += 2;
  1140. _inBytesPacket -= 2;
  1141. }
  1142. AssertValidState();
  1143. value = (Int16)((buffer[offset + 1] << 8) + buffer[offset]);
  1144. return true;
  1145. }
  1146. internal bool TryReadInt32(out int value) {
  1147. TdsParser.ReliabilitySection.Assert("unreliable call to ReadInt32"); // you need to setup for a thread abort somewhere before you call this method
  1148. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1149. if (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4)) {
  1150. // If the int isn't fully in the buffer, or if it isn't fully in the packet,
  1151. // then use ReadByteArray since the logic is there to take care of that.
  1152. if (!TryReadByteArray(_bTmp, 0, 4)) {
  1153. value = 0;
  1154. return false;
  1155. }
  1156. AssertValidState();
  1157. value = BitConverter.ToInt32(_bTmp, 0);
  1158. return true;
  1159. }
  1160. else {
  1161. // The entire int is in the packet and in the buffer, so just return it
  1162. // and take care of the counters.
  1163. value = BitConverter.ToInt32(_inBuff, _inBytesUsed);
  1164. _inBytesUsed += 4;
  1165. _inBytesPacket -= 4;
  1166. AssertValidState();
  1167. return true;
  1168. }
  1169. }
  1170. // This method is safe to call when doing async without snapshot
  1171. internal bool TryReadInt64(out long value) {
  1172. TdsParser.ReliabilitySection.Assert("unreliable call to ReadInt64"); // you need to setup for a thread abort somewhere before you call this method
  1173. if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead)) {
  1174. if (!TryPrepareBuffer()) {
  1175. value = 0;
  1176. return false;
  1177. }
  1178. }
  1179. if ((_bTmpRead > 0) || (((_inBytesUsed + 8) > _inBytesRead) || (_inBytesPacket < 8))) {
  1180. // If the long isn't fully in the buffer, or if it isn't fully in the packet,
  1181. // then use ReadByteArray since the logic is there to take care of that.
  1182. int bytesRead = 0;
  1183. if (!TryReadByteArray(_bTmp, _bTmpRead, 8 - _bTmpRead, out bytesRead)) {
  1184. Debug.Assert(_bTmpRead + bytesRead <= 8, "Read more data than required");
  1185. _bTmpRead += bytesRead;
  1186. value = 0;
  1187. return false;
  1188. }
  1189. else {
  1190. Debug.Assert(_bTmpRead + bytesRead == 8, "TryReadByteArray returned true without reading all data required");
  1191. _bTmpRead = 0;
  1192. AssertValidState();
  1193. value = BitConverter.ToInt64(_bTmp, 0);
  1194. return true;
  1195. }
  1196. }
  1197. else {
  1198. // The entire long is in the packet and in the buffer, so just return it
  1199. // and take care of the counters.
  1200. value = BitConverter.ToInt64(_inBuff, _inBytesUsed);
  1201. _inBytesUsed += 8;
  1202. _inBytesPacket -= 8;
  1203. AssertValidState();
  1204. return true;
  1205. }
  1206. }
  1207. internal bool TryReadUInt16(out ushort value) {
  1208. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1209. byte[] buffer;
  1210. int offset;
  1211. if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2)) {
  1212. // If the uint16 isn't fully in the buffer, or if it isn't fully in the packet,
  1213. // then use ReadByteArray since the logic is there to take care of that.
  1214. if (!TryReadByteArray(_bTmp, 0, 2)) {
  1215. value = default(ushort);
  1216. return false;
  1217. }
  1218. buffer = _bTmp;
  1219. offset = 0;
  1220. }
  1221. else {
  1222. // The entire uint16 is in the packet and in the buffer, so just return it
  1223. // and take care of the counters.
  1224. buffer = _inBuff;
  1225. offset = _inBytesUsed;
  1226. _inBytesUsed += 2;
  1227. _inBytesPacket -= 2;
  1228. }
  1229. AssertValidState();
  1230. value = (UInt16)((buffer[offset + 1] << 8) + buffer[offset]);
  1231. return true;
  1232. }
  1233. // This method is safe to call when doing async without replay
  1234. internal bool TryReadUInt32(out uint value) {
  1235. TdsParser.ReliabilitySection.Assert("unreliable call to ReadUInt32"); // you need to setup for a thread abort somewhere before you call this method
  1236. if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead)) {
  1237. if (!TryPrepareBuffer()) {
  1238. value = 0;
  1239. return false;
  1240. }
  1241. }
  1242. if ((_bTmpRead > 0) || (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4))) {
  1243. // If the int isn't fully in the buffer, or if it isn't fully in the packet,
  1244. // then use ReadByteArray since the logic is there to take care of that.
  1245. int bytesRead = 0;
  1246. if (!TryReadByteArray(_bTmp, _bTmpRead, 4 - _bTmpRead, out bytesRead)) {
  1247. Debug.Assert(_bTmpRead + bytesRead <= 4, "Read more data than required");
  1248. _bTmpRead += bytesRead;
  1249. value = 0;
  1250. return false;
  1251. }
  1252. else {
  1253. Debug.Assert(_bTmpRead + bytesRead == 4, "TryReadByteArray returned true without reading all data required");
  1254. _bTmpRead = 0;
  1255. AssertValidState();
  1256. value = BitConverter.ToUInt32(_bTmp, 0);
  1257. return true;
  1258. }
  1259. }
  1260. else {
  1261. // The entire int is in the packet and in the buffer, so just return it
  1262. // and take care of the counters.
  1263. value = BitConverter.ToUInt32(_inBuff, _inBytesUsed);
  1264. _inBytesUsed += 4;
  1265. _inBytesPacket -= 4;
  1266. AssertValidState();
  1267. return true;
  1268. }
  1269. }
  1270. internal bool TryReadSingle(out float value) {
  1271. TdsParser.ReliabilitySection.Assert("unreliable call to ReadSingle"); // you need to setup for a thread abort somewhere before you call this method
  1272. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1273. if (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4)) {
  1274. // If the float isn't fully in the buffer, or if it isn't fully in the packet,
  1275. // then use ReadByteArray since the logic is there to take care of that.
  1276. if (!TryReadByteArray(_bTmp, 0, 4)) {
  1277. value = default(float);
  1278. return false;
  1279. }
  1280. AssertValidState();
  1281. value = BitConverter.ToSingle(_bTmp, 0);
  1282. return true;
  1283. }
  1284. else {
  1285. // The entire float is in the packet and in the buffer, so just return it
  1286. // and take care of the counters.
  1287. value = BitConverter.ToSingle(_inBuff, _inBytesUsed);
  1288. _inBytesUsed += 4;
  1289. _inBytesPacket -= 4;
  1290. AssertValidState();
  1291. return true;
  1292. }
  1293. }
  1294. internal bool TryReadDouble(out double value) {
  1295. TdsParser.ReliabilitySection.Assert("unreliable call to ReadDouble"); // you need to setup for a thread abort somewhere before you call this method
  1296. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1297. if (((_inBytesUsed + 8) > _inBytesRead) || (_inBytesPacket < 8)) {
  1298. // If the double isn't fully in the buffer, or if it isn't fully in the packet,
  1299. // then use ReadByteArray since the logic is there to take care of that.
  1300. if (!TryReadByteArray(_bTmp, 0, 8)) {
  1301. value = default(double);
  1302. return false;
  1303. }
  1304. AssertValidState();
  1305. value = BitConverter.ToDouble(_bTmp, 0);
  1306. return true;
  1307. }
  1308. else {
  1309. // The entire double is in the packet and in the buffer, so just return it
  1310. // and take care of the counters.
  1311. value = BitConverter.ToDouble(_inBuff, _inBytesUsed);
  1312. _inBytesUsed += 8;
  1313. _inBytesPacket -= 8;
  1314. AssertValidState();
  1315. return true;
  1316. }
  1317. }
  1318. internal bool TryReadString(int length, out string value) {
  1319. TdsParser.ReliabilitySection.Assert("unreliable call to ReadString"); // you need to setup for a thread abort somewhere before you call this method
  1320. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1321. int cBytes = length << 1;
  1322. byte[] buf;
  1323. int offset = 0;
  1324. if (((_inBytesUsed + cBytes) > _inBytesRead) || (_inBytesPacket < cBytes)) {
  1325. if (_bTmp == null || _bTmp.Length < cBytes) {
  1326. _bTmp = new byte[cBytes];
  1327. }
  1328. if (!TryReadByteArray(_bTmp, 0, cBytes)) {
  1329. value = null;
  1330. return false;
  1331. }
  1332. // assign local to point to parser scratch buffer
  1333. buf = _bTmp;
  1334. AssertValidState();
  1335. }
  1336. else {
  1337. // assign local to point to _inBuff
  1338. buf = _inBuff;
  1339. offset = _inBytesUsed;
  1340. _inBytesUsed += cBytes;
  1341. _inBytesPacket -= cBytes;
  1342. AssertValidState();
  1343. }
  1344. value = System.Text.Encoding.Unicode.GetString(buf, offset, cBytes);
  1345. return true;
  1346. }
  1347. internal bool TryReadStringWithEncoding(int length, System.Text.Encoding encoding, bool isPlp, out string value) {
  1348. TdsParser.ReliabilitySection.Assert("unreliable call to ReadStringWithEncoding"); // you need to setup for a thread abort somewhere before you call this method
  1349. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1350. if (null == encoding) {
  1351. // Bug 462435:CR: TdsParser.DrainData(stateObj) hitting timeout exception after Connection Resiliency change
  1352. // http://vstfdevdiv:8080/web/wi.aspx?pcguid=22f9acc9-569a-41ff-b6ac-fac1b6370209&id=462435
  1353. // Need to skip the current column before throwing the error - this ensures that the state shared between this and the data reader is consistent when calling DrainData
  1354. if (isPlp) {
  1355. ulong ignored;
  1356. if (!_parser.TrySkipPlpValue((ulong)length, this, out ignored)) {
  1357. value = null;
  1358. return false;
  1359. }
  1360. }
  1361. else {
  1362. if (!TrySkipBytes(length)) {
  1363. value = null;
  1364. return false;
  1365. }
  1366. }
  1367. _parser.ThrowUnsupportedCollationEncountered(this);
  1368. }
  1369. byte[] buf = null;
  1370. int offset = 0;
  1371. if (isPlp) {
  1372. if (!TryReadPlpBytes(ref buf, 0, Int32.MaxValue, out length)) {
  1373. value = null;
  1374. return false;
  1375. }
  1376. AssertValidState();
  1377. }
  1378. else {
  1379. if (((_inBytesUsed + length) > _inBytesRead) || (_inBytesPacket < length)) {
  1380. if (_bTmp == null || _bTmp.Length < length) {
  1381. _bTmp = new byte[length];
  1382. }
  1383. if (!TryReadByteArray(_bTmp, 0, length)) {
  1384. value = null;
  1385. return false;
  1386. }
  1387. // assign local to point to parser scratch buffer
  1388. buf = _bTmp;
  1389. AssertValidState();
  1390. }
  1391. else {
  1392. // assign local to point to _inBuff
  1393. buf = _inBuff;
  1394. offset = _inBytesUsed;
  1395. _inBytesUsed += length;
  1396. _inBytesPacket -= length;
  1397. AssertValidState();
  1398. }
  1399. }
  1400. // BCL optimizes to not use char[] underneath
  1401. value = encoding.GetString(buf, offset, length);
  1402. return true;
  1403. }
  1404. internal ulong ReadPlpLength(bool returnPlpNullIfNull) {
  1405. ulong value;
  1406. Debug.Assert(_syncOverAsync, "Should not attempt pends in a synchronous call");
  1407. bool result = TryReadPlpLength(returnPlpNullIfNull, out value);
  1408. if (!result) { throw SQL.SynchronousCallMayNotPend(); }
  1409. return value;
  1410. }
  1411. // Reads the length of either the entire data or the length of the next chunk in a
  1412. // partially length prefixed data
  1413. // After this call, call ReadPlpBytes/ReadPlpUnicodeChars untill the specified length of data
  1414. // is consumed. Repeat this until ReadPlpLength returns 0 in order to read the
  1415. // entire stream.
  1416. // When this function returns 0, it means the data stream is read completely and the
  1417. // plp state in the tdsparser is cleaned.
  1418. internal bool TryReadPlpLength(bool returnPlpNullIfNull, out ulong lengthLeft) {
  1419. uint chunklen;
  1420. // bool firstchunk = false;
  1421. bool isNull = false;
  1422. Debug.Assert(_longlenleft == 0, "Out of synch length read request");
  1423. if (_longlen == 0) {
  1424. // First chunk is being read. Find out what type of chunk it is
  1425. long value;
  1426. if (!TryReadInt64(out value)) {
  1427. lengthLeft = 0;
  1428. return false;
  1429. }
  1430. _longlen = (ulong)value;
  1431. // firstchunk = true;
  1432. }
  1433. if (_longlen == TdsEnums.SQL_PLP_NULL) {
  1434. _longlen = 0;
  1435. _longlenleft = 0;
  1436. isNull = true;
  1437. }
  1438. else {
  1439. // Data is coming in uint chunks, read length of next chunk
  1440. if (!TryReadUInt32(out chunklen)) {
  1441. lengthLeft = 0;
  1442. return false;
  1443. }
  1444. if (chunklen == TdsEnums.SQL_PLP_CHUNK_TERMINATOR) {
  1445. _longlenleft = 0;
  1446. _longlen = 0;
  1447. }
  1448. else {
  1449. _longlenleft = (ulong)chunklen;
  1450. }
  1451. }
  1452. AssertValidState();
  1453. if (isNull && returnPlpNullIfNull) {
  1454. lengthLeft = TdsEnums.SQL_PLP_NULL;
  1455. return true;
  1456. }
  1457. lengthLeft = _longlenleft;
  1458. return true;
  1459. }
  1460. internal int ReadPlpBytesChunk(byte[] buff, int offset, int len) {
  1461. Debug.Assert(_syncOverAsync, "Should not attempt pends in a synchronous call");
  1462. Debug.Assert(_longlenleft > 0, "Read when no data available");
  1463. int value;
  1464. int bytesToRead = (int)Math.Min(_longlenleft, (ulong)len);
  1465. bool result = TryReadByteArray(buff, offset, bytesToRead, out value);
  1466. _longlenleft -= (ulong)bytesToRead;
  1467. if (!result) { throw SQL.SynchronousCallMayNotPend(); }
  1468. return value;
  1469. }
  1470. // Reads the requested number of bytes from a plp data stream, or the entire data if
  1471. // requested length is -1 or larger than the actual length of data. First call to this method
  1472. // should be preceeded by a call to ReadPlpLength or ReadDataLength.
  1473. // Returns the actual bytes read.
  1474. // NOTE: This method must be retriable WITHOUT replaying a snapshot
  1475. // Every time you call this method increment the offst and decrease len by the value of totalBytesRead
  1476. internal bool TryReadPlpBytes(ref byte[] buff, int offst, int len, out int totalBytesRead) {
  1477. int bytesRead = 0;
  1478. int bytesLeft;
  1479. byte[] newbuf;
  1480. ulong ignored;
  1481. if (_longlen == 0) {
  1482. Debug.Assert(_longlenleft == 0);
  1483. if (buff == null) {
  1484. buff = new byte[0];
  1485. }
  1486. AssertValidState();
  1487. totalBytesRead = 0;
  1488. return true; // No data
  1489. }
  1490. Debug.Assert((_longlen != TdsEnums.SQL_PLP_NULL),
  1491. "Out of sync plp read request");
  1492. Debug.Assert((buff == null && offst == 0) || (buff.Length >= offst + len), "Invalid length sent to ReadPlpBytes()!");
  1493. bytesLeft = len;
  1494. // If total length is known up front, allocate the whole buffer in one shot instead of realloc'ing and copying over each time
  1495. if (buff == null && _longlen != TdsEnums.SQL_PLP_UNKNOWNLEN) {
  1496. buff = new byte[(int)Math.Min((int)_longlen, len)];
  1497. }
  1498. if (_longlenleft == 0) {
  1499. if (!TryReadPlpLength(false, out ignored)) {
  1500. totalBytesRead = 0;
  1501. return false;
  1502. }
  1503. if (_longlenleft == 0) { // Data read complete
  1504. totalBytesRead = 0;
  1505. return true;
  1506. }
  1507. }
  1508. if (buff == null) {
  1509. buff = new byte[_longlenleft];
  1510. }
  1511. totalBytesRead = 0;
  1512. while (bytesLeft > 0) {
  1513. int bytesToRead = (int)Math.Min(_longlenleft, (ulong)bytesLeft);
  1514. if (buff.Length < (offst + bytesToRead)) {
  1515. // Grow the array
  1516. newbuf = new byte[offst + bytesToRead];
  1517. Buffer.BlockCopy(buff, 0, newbuf, 0, offst);
  1518. buff = newbuf;
  1519. }
  1520. bool result = TryReadByteArray(buff, offst, bytesToRead, out bytesRead);
  1521. Debug.Assert(bytesRead <= bytesLeft, "Read more bytes than we needed");
  1522. Debug.Assert((ulong)bytesRead <= _longlenleft, "Read more bytes than is available");
  1523. bytesLeft -= bytesRead;
  1524. offst += bytesRead;
  1525. totalBytesRead += bytesRead;
  1526. _longlenleft -= (ulong)bytesRead;
  1527. if (!result) {
  1528. return false;
  1529. }
  1530. if (_longlenleft == 0) { // Read the next chunk or cleanup state if hit the end
  1531. if (!TryReadPlpLength(false, out ignored)) {
  1532. return false;
  1533. }
  1534. }
  1535. AssertValidState();
  1536. // Catch the point where we read the entire plp data stream and clean up state
  1537. if (_longlenleft == 0) // Data read complete
  1538. break;
  1539. }
  1540. return true;
  1541. }
  1542. /////////////////////////////////////////
  1543. // Value Skip Logic //
  1544. /////////////////////////////////////////
  1545. // Reads bytes from the buffer but doesn't return them, in effect simply deleting them.
  1546. // Does not handle plp fields, need to use SkipPlpBytesValue for those.
  1547. // Does not handle null values or NBC bitmask, ensure the value is not null before calling this method
  1548. internal bool TrySkipLongBytes(long num) {
  1549. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1550. int cbSkip = 0;
  1551. while (num > 0) {
  1552. cbSkip = (int)Math.Min((long)Int32.MaxValue, num);
  1553. if (!TryReadByteArray(null, 0, cbSkip)) {
  1554. return false;
  1555. }
  1556. num -= (long)cbSkip;
  1557. }
  1558. return true;
  1559. }
  1560. // Reads bytes from the buffer but doesn't return them, in effect simply deleting them.
  1561. internal bool TrySkipBytes(int num) {
  1562. Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
  1563. return TryReadByteArray(null, 0, num);
  1564. }
  1565. /////////////////////////////////////////
  1566. // Network/Packet Reading & Processing //
  1567. /////////////////////////////////////////
  1568. internal void SetSnapshot() {
  1569. _snapshot = new StateSnapshot(this);
  1570. _snapshot.Snap();
  1571. _snapshotReplay = false;
  1572. }
  1573. internal void ResetSnapshot() {
  1574. _snapshot = null;
  1575. _snapshotReplay = false;
  1576. }
  1577. #if DEBUG
  1578. StackTrace _lastStack;
  1579. #endif
  1580. internal bool TryReadNetworkPacket()
  1581. {
  1582. TdsParser.ReliabilitySection.Assert("unreliable call to TryReadNetworkPacket"); // you need to setup for a thread abort somewhere before you call this method
  1583. #if DEBUG
  1584. Debug.Assert(!_shouldHaveEnoughData || _attentionSent, "Caller said there should be enough data, but we are currently reading a packet");
  1585. #endif
  1586. if (_snapshot != null) {
  1587. if (_snapshotReplay) {
  1588. if (_snapshot.Replay()) {
  1589. #if DEBUG
  1590. if (_checkNetworkPacketRetryStacks) {
  1591. _snapshot.CheckStack(new StackTrace());
  1592. }
  1593. #endif
  1594. Bid.Trace("<sc.TdsParser.ReadNetworkPacket|INFO|ADV> Async packet replay\n");
  1595. return true;
  1596. }
  1597. #if DEBUG
  1598. else {
  1599. if (_checkNetworkPacketRetryStacks) {
  1600. _lastStack = new StackTrace();
  1601. }
  1602. }
  1603. #endif
  1604. }
  1605. // previous buffer is in snapshot
  1606. _inBuff = new byte[_inBuff.Length];
  1607. }
  1608. if (_syncOverAsync) {
  1609. ReadSniSyncOverAsync();
  1610. return true;
  1611. }
  1612. ReadSni(new TaskCompletionSource<object>());
  1613. #if DEBUG
  1614. if (_failAsyncPends) {
  1615. throw new InvalidOperationException("Attempted to pend a read when _failAsyncPends test hook was enabled");
  1616. }
  1617. if (_forceSyncOverAsyncAfterFirstPend) {
  1618. _syncOverAsync = true;
  1619. }
  1620. #endif
  1621. Debug.Assert((_snapshot != null) ^ _asyncReadWithoutSnapshot, "Must have either _snapshot set up or _asyncReadWithoutSnapshot enabled (but not both) to pend a read");
  1622. return false;
  1623. }
  1624. internal void PrepareReplaySnapshot() {
  1625. _networkPacketTaskSource = null;
  1626. _snapshot.PrepareReplay();
  1627. }
  1628. internal void ReadSniSyncOverAsync() {
  1629. if (_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed) {
  1630. throw ADP.ClosedConnectionError();
  1631. }
  1632. IntPtr readPacket = IntPtr.Zero;
  1633. UInt32 error;
  1634. RuntimeHelpers.PrepareConstrainedRegions();
  1635. bool shouldDecrement = false;
  1636. try {
  1637. TdsParser.ReliabilitySection.Assert("unreliable call to ReadSniSync"); // you need to setup for a thread abort somewhere before you call this method
  1638. Interlocked.Increment(ref _readingCount);
  1639. shouldDecrement = true;
  1640. SNIHandle handle = Handle;
  1641. if (handle == null) {
  1642. throw ADP.ClosedConnectionError();
  1643. }
  1644. error = SNINativeMethodWrapper.SNIReadSyncOverAsync(handle, ref readPacket, GetTimeoutRemaining());
  1645. Interlocked.Decrement(ref _readingCount);
  1646. shouldDecrement = false;
  1647. if (_parser.MARSOn) { // Only take reset lock on MARS and Async.
  1648. CheckSetResetConnectionState(error, CallbackType.Read);
  1649. }
  1650. if (TdsEnums.SNI_SUCCESS == error) { // Success - process results!
  1651. Debug.Assert(ADP.PtrZero != readPacket, "ReadNetworkPacket cannot be null in syncronous operation!");
  1652. ProcessSniPacket(readPacket, 0);
  1653. #if DEBUG
  1654. if (_forcePendingReadsToWaitForUser) {
  1655. _networkPacketTaskSource = new TaskCompletionSource<object>();
  1656. Thread.MemoryBarrier();
  1657. _networkPacketTaskSource.Task.Wait();
  1658. _networkPacketTaskSource = null;
  1659. }
  1660. #endif
  1661. }
  1662. else { // Failure!
  1663. Debug.Assert(IntPtr.Zero == readPacket, "unexpected readPacket without corresponding SNIPacketRelease");
  1664. ReadSniError(this, error);
  1665. }
  1666. }
  1667. finally {
  1668. if (shouldDecrement) {
  1669. Interlocked.Decrement(ref _readingCount);
  1670. }
  1671. if (readPacket != IntPtr.Zero) {
  1672. // Be sure to release packet, otherwise it will be leaked by native.
  1673. SNINativeMethodWrapper.SNIPacketRelease(readPacket);
  1674. }
  1675. AssertValidState();
  1676. }
  1677. }
  1678. internal void OnConnectionClosed() {
  1679. // the stateObj is not null, so the async invocation that registered this callback
  1680. // via the SqlReferenceCollection has not yet completed. We will look for a
  1681. // _networkPacketTaskSource and mark it faulted. If we don't find it, then
  1682. // TdsParserStateObject.ReadSni will abort when it does look to see if the parser
  1683. // is open. If we do, then when the call that created it completes and a continuation
  1684. // is registered, we will ensure the completion is called.
  1685. // Note, this effort is necessary because when the app domain is being unloaded,
  1686. // we don't get callback from SNI.
  1687. // first mark parser broken. This is to ensure that ReadSni will abort if it has
  1688. // not yet executed.
  1689. Parser.State = TdsParserState.Broken;
  1690. Parser.Connection.BreakConnection();
  1691. // Ensure that changing state occurs before checking _networkPacketTaskSource
  1692. Thread.MemoryBarrier();
  1693. // then check for networkPacketTaskSource
  1694. var taskSource = _networkPacketTaskSource;
  1695. if (taskSource != null) {
  1696. taskSource.TrySetException(ADP.ExceptionWithStackTrace(ADP.ClosedConnectionError()));
  1697. }
  1698. taskSource = _writeCompletionSource;
  1699. if (taskSource != null) {
  1700. taskSource.TrySetException(ADP.ExceptionWithStackTrace(ADP.ClosedConnectionError()));
  1701. }
  1702. }
  1703. private void OnTimeout(object state) {
  1704. if (!_internalTimeout) {
  1705. _internalTimeout = true;
  1706. // lock protects against Close and Cancel
  1707. lock (this) {
  1708. if (!_attentionSent) {
  1709. AddError(new SqlError(TdsEnums.TIMEOUT_EXPIRED, (byte)0x00, TdsEnums.MIN_ERROR_CLASS, _parser.Server, _parser.Connection.TimeoutErrorInternal.GetErrorMessage(), "", 0, TdsEnums.SNI_WAIT_TIMEOUT));
  1710. // Grab a reference to the _networkPacketTaskSource in case it becomes null while we are trying to use it
  1711. TaskCompletionSource<object> source = _networkPacketTaskSource;
  1712. if (_parser.Connection.IsInPool) {
  1713. // Dev11 Bug 390048 : Timing issue between OnTimeout and ReadAsyncCallback results in SqlClient's packet parsing going out of [....]
  1714. // We should never timeout if the connection is currently in the pool: the safest thing to do here is to doom the connection to avoid corruption
  1715. Debug.Assert(_parser.Connection.IsConnectionDoomed, "Timeout occurred while the connection is in the pool");
  1716. _parser.State = TdsParserState.Broken;
  1717. _parser.Connection.BreakConnection();
  1718. if (source != null) {
  1719. source.TrySetCanceled();
  1720. }
  1721. }
  1722. else if (_parser.State == TdsParserState.OpenLoggedIn) {
  1723. try {
  1724. SendAttention(mustTakeWriteLock: true);
  1725. }
  1726. catch (Exception e) {
  1727. if (!ADP.IsCatchableExceptionType(e)) {
  1728. throw;
  1729. }
  1730. // if unable to send attention, cancel the _networkPacketTaskSource to
  1731. // request the parser be broken. SNIWritePacket errors will already
  1732. // be in the _errors collection.
  1733. if (source != null) {
  1734. source.TrySetCanceled();
  1735. }
  1736. }
  1737. }
  1738. // If we still haven't received a packet then we don't want to actually close the connection
  1739. // from another thread, so complete the pending operation as cancelled, informing them to break it
  1740. if (source != null) {
  1741. Task.Delay(AttentionTimeoutSeconds * 1000).ContinueWith(_ => {
  1742. // Only break the connection if the read didn't finish
  1743. if (!source.Task.IsCompleted) {
  1744. int pendingCallback = IncrementPendingCallbacks();
  1745. RuntimeHelpers.PrepareConstrainedRegions();
  1746. try {
  1747. // If pendingCallback is at 3, then ReadAsyncCallback hasn't been called yet
  1748. // So it is safe for us to break the connection and cancel the Task (since we are not sure that ReadAsyncCallback will ever be called)
  1749. if ((pendingCallback == 3) && (!source.Task.IsCompleted)) {
  1750. Debug.Assert(source == _networkPacketTaskSource, "_networkPacketTaskSource which is being timed is not the current task source");
  1751. // Try to throw the timeout exception and store it in the task
  1752. bool exceptionStored = false;
  1753. try {
  1754. CheckThrowSNIException();
  1755. }
  1756. catch (Exception ex) {
  1757. if (source.TrySetException(ex)) {
  1758. exceptionStored = true;
  1759. }
  1760. }
  1761. // Ensure that the connection is no longer usable
  1762. // This is needed since the timeout error added above is non-fatal (and so throwing it won't break the connection)
  1763. _parser.State = TdsParserState.Broken;
  1764. _parser.Connection.BreakConnection();
  1765. // If we didn't get an exception (something else observed it?) then ensure that the task is cancelled
  1766. if (!exceptionStored) {
  1767. source.TrySetCanceled();
  1768. }
  1769. }
  1770. }
  1771. finally {
  1772. DecrementPendingCallbacks(release: false);
  1773. }
  1774. }
  1775. });
  1776. }
  1777. }
  1778. }
  1779. }
  1780. }
  1781. internal void ReadSni(TaskCompletionSource<object> completion) {
  1782. Debug.Assert(_networkPacketTaskSource == null || ((_asyncReadWithoutSnapshot) && (_networkPacketTaskSource.Task.IsCompleted)), "Pending async call or failed to replay snapshot when calling ReadSni");
  1783. _networkPacketTaskSource = completion;
  1784. // Ensure that setting the completion source is completed before checking the state
  1785. Thread.MemoryBarrier();
  1786. // We must check after assigning _networkPacketTaskSource to avoid ----s with
  1787. // SqlCommand.OnConnectionClosed
  1788. if (_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed) {
  1789. throw ADP.ClosedConnectionError();
  1790. }
  1791. #if DEBUG
  1792. if (_forcePendingReadsToWaitForUser) {
  1793. _realNetworkPacketTaskSource = new TaskCompletionSource<object>();
  1794. }
  1795. #endif
  1796. IntPtr readPacket = IntPtr.Zero;
  1797. UInt32 error = 0;
  1798. RuntimeHelpers.PrepareConstrainedRegions();
  1799. try {
  1800. Debug.Assert(completion != null, "Async on but null asyncResult passed");
  1801. if (_networkPacketTimeout == null) {
  1802. _networkPacketTimeout = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
  1803. }
  1804. // -1 == Infinite
  1805. // 0 == Already timed out (NOTE: To simulate the same behavior as [....] we will only timeout on 0 if we receive an IO Pending from SNI)
  1806. // >0 == Actual timeout remaining
  1807. int msecsRemaining = GetTimeoutRemaining();
  1808. if (msecsRemaining > 0) {
  1809. ChangeNetworkPacketTimeout(msecsRemaining, Timeout.Infinite);
  1810. }
  1811. SNIHandle handle = null;
  1812. RuntimeHelpers.PrepareConstrainedRegions();
  1813. try {} finally {
  1814. Interlocked.Increment(ref _readingCount);
  1815. handle = Handle;
  1816. if (handle != null) {
  1817. IncrementPendingCallbacks();
  1818. error = SNINativeMethodWrapper.SNIReadAsync(handle, ref readPacket);
  1819. if (!(TdsEnums.SNI_SUCCESS == error || TdsEnums.SNI_SUCCESS_IO_PENDING == error)) {
  1820. DecrementPendingCallbacks(false); // Failure - we won't receive callback!
  1821. }
  1822. }
  1823. Interlocked.Decrement(ref _readingCount);
  1824. }
  1825. if (handle == null) {
  1826. throw ADP.ClosedConnectionError();
  1827. }
  1828. if (TdsEnums.SNI_SUCCESS == error) { // Success - process results!
  1829. Debug.Assert(ADP.PtrZero != readPacket, "ReadNetworkPacket should not have been null on this async operation!");
  1830. ReadAsyncCallback(ADP.PtrZero, readPacket, 0);
  1831. }
  1832. else if (TdsEnums.SNI_SUCCESS_IO_PENDING != error) { // FAILURE!
  1833. Debug.Assert(IntPtr.Zero == readPacket, "unexpected readPacket without corresponding SNIPacketRelease");
  1834. ReadSniError(this, error);
  1835. #if DEBUG
  1836. if ((_forcePendingReadsToWaitForUser) && (_realNetworkPacketTaskSource != null)) {
  1837. _realNetworkPacketTaskSource.TrySetResult(null);
  1838. }
  1839. else
  1840. #endif
  1841. {
  1842. _networkPacketTaskSource.TrySetResult(null);
  1843. }
  1844. // Disable timeout timer on error
  1845. ChangeNetworkPacketTimeout(Timeout.Infinite, Timeout.Infinite);
  1846. }
  1847. else if (msecsRemaining == 0) { // Got IO Pending, but we have no time left to wait
  1848. // Immediately schedule the timeout timer to fire
  1849. ChangeNetworkPacketTimeout(0, Timeout.Infinite);
  1850. }
  1851. // DO NOT HANDLE PENDING READ HERE - which is TdsEnums.SNI_SUCCESS_IO_PENDING state.
  1852. // That is handled by user who initiated async read, or by ReadNetworkPacket which is [....] over async.
  1853. }
  1854. finally {
  1855. if (readPacket != IntPtr.Zero) {
  1856. // Be sure to release packet, otherwise it will be leaked by native.
  1857. SNINativeMethodWrapper.SNIPacketRelease(readPacket);
  1858. }
  1859. AssertValidState();
  1860. }
  1861. }
  1862. /// <summary>
  1863. /// Checks to see if the underlying connection is still alive (used by connection pool resilency)
  1864. /// NOTE: This is not safe to do on a connection that is currently in use
  1865. /// NOTE: This will mark the connection as broken if it is found to be dead
  1866. /// </summary>
  1867. /// <param name="throwOnException">If true then an exception will be thrown if the connection is found to be dead, otherwise no exception will be thrown</param>
  1868. /// <returns>True if the connection is still alive, otherwise false</returns>
  1869. internal bool IsConnectionAlive(bool throwOnException)
  1870. {
  1871. Debug.Assert(_parser.Connection == null || _parser.Connection.Pool != null, "Shouldn't be calling IsConnectionAlive on non-pooled connections");
  1872. bool isAlive = true;
  1873. if (DateTime.UtcNow.Ticks - _lastSuccessfulIOTimer._value > CheckConnectionWindow) {
  1874. if ((_parser == null) || ((_parser.State == TdsParserState.Broken) || (_parser.State == TdsParserState.Closed))) {
  1875. isAlive = false;
  1876. if (throwOnException) {
  1877. throw SQL.ConnectionDoomed();
  1878. }
  1879. }
  1880. else if ((_pendingCallbacks > 1) || ((_parser.Connection != null) && (!_parser.Connection.IsInPool))) {
  1881. // This connection is currently in use, assume that the connection is 'alive'
  1882. // NOTE: SNICheckConnection is not currently supported for connections that are in use
  1883. Debug.Assert(true, "Call to IsConnectionAlive while connection is in use");
  1884. }
  1885. else {
  1886. UInt32 error;
  1887. IntPtr readPacket = IntPtr.Zero;
  1888. RuntimeHelpers.PrepareConstrainedRegions();
  1889. try {
  1890. TdsParser.ReliabilitySection.Assert("unreliable call to IsConnectionAlive"); // you need to setup for a thread abort somewhere before you call this method
  1891. SniContext = SniContext.Snix_Connect;
  1892. error = SNINativeMethodWrapper.SNICheckConnection(Handle);
  1893. if ((error != TdsEnums.SNI_SUCCESS) && (error != TdsEnums.SNI_WAIT_TIMEOUT)) {
  1894. // Connection is dead
  1895. Bid.Trace("<sc.TdsParser.IsConnectionAlive|Info> received error %d on idle connection\n", (int)error);
  1896. isAlive = false;
  1897. if (throwOnException) {
  1898. // Get the error from SNI so that we can throw the correct exception
  1899. AddError(_parser.ProcessSNIError(this));
  1900. ThrowExceptionAndWarning();
  1901. }
  1902. }
  1903. else {
  1904. _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks;
  1905. }
  1906. }
  1907. finally {
  1908. if (readPacket != IntPtr.Zero) {
  1909. // Be sure to release packet, otherwise it will be leaked by native.
  1910. SNINativeMethodWrapper.SNIPacketRelease(readPacket);
  1911. }
  1912. }
  1913. }
  1914. }
  1915. return isAlive;
  1916. }
  1917. /// <summary>
  1918. /// Checks to see if the underlying connection is still valid (used by idle connection resiliency - for active connections)
  1919. /// NOTE: This is not safe to do on a connection that is currently in use
  1920. /// NOTE: This will mark the connection as broken if it is found to be dead
  1921. /// </summary>
  1922. /// <returns>True if the connection is still alive, otherwise false</returns>
  1923. internal bool ValidateSNIConnection() {
  1924. if ((_parser == null) || ((_parser.State == TdsParserState.Broken) || (_parser.State == TdsParserState.Closed))) {
  1925. return false;
  1926. }
  1927. if (DateTime.UtcNow.Ticks - _lastSuccessfulIOTimer._value <= CheckConnectionWindow) {
  1928. return true;
  1929. }
  1930. UInt32 error=TdsEnums.SNI_SUCCESS;
  1931. SniContext = SniContext.Snix_Connect;
  1932. try {
  1933. Interlocked.Increment(ref _readingCount);
  1934. SNIHandle handle = Handle;
  1935. if (handle != null) {
  1936. error = SNINativeMethodWrapper.SNICheckConnection(handle);
  1937. }
  1938. }
  1939. finally {
  1940. Interlocked.Decrement(ref _readingCount);
  1941. }
  1942. return (error == TdsEnums.SNI_SUCCESS) || (error == TdsEnums.SNI_WAIT_TIMEOUT);
  1943. }
  1944. // This method should only be called by ReadSni! If not - it may have problems with timeouts!
  1945. private void ReadSniError(TdsParserStateObject stateObj, UInt32 error) {
  1946. TdsParser.ReliabilitySection.Assert("unreliable call to ReadSniSyncError"); // you need to setup for a thread abort somewhere before you call this method
  1947. if (TdsEnums.SNI_WAIT_TIMEOUT == error) {
  1948. Debug.Assert(_syncOverAsync, "Should never reach here with async on!");
  1949. bool fail = false;
  1950. if (_internalTimeout) { // This is now our second timeout - time to give up.
  1951. fail = true;
  1952. }
  1953. else {
  1954. stateObj._internalTimeout = true;
  1955. Debug.Assert(_parser.Connection != null, "SqlConnectionInternalTds handler can not be null at this point.");
  1956. AddError(new SqlError(TdsEnums.TIMEOUT_EXPIRED, (byte)0x00, TdsEnums.MIN_ERROR_CLASS, _parser.Server, _parser.Connection.TimeoutErrorInternal.GetErrorMessage(), "", 0, TdsEnums.SNI_WAIT_TIMEOUT));
  1957. if (!stateObj._attentionSent) {
  1958. if (stateObj.Parser.State == TdsParserState.OpenLoggedIn) {
  1959. stateObj.SendAttention(mustTakeWriteLock: true);
  1960. IntPtr syncReadPacket = IntPtr.Zero;
  1961. RuntimeHelpers.PrepareConstrainedRegions();
  1962. bool shouldDecrement = false;
  1963. try {
  1964. Interlocked.Increment(ref _readingCount);
  1965. shouldDecrement = true;
  1966. SNIHandle handle = Handle;
  1967. if (handle == null) {
  1968. throw ADP.ClosedConnectionError();
  1969. }
  1970. error = SNINativeMethodWrapper.SNIReadSyncOverAsync(handle, ref syncReadPacket, stateObj.GetTimeoutRemaining());
  1971. Interlocked.Decrement(ref _readingCount);
  1972. shouldDecrement = false;
  1973. if (TdsEnums.SNI_SUCCESS == error) {
  1974. // We will end up letting the run method deal with the expected done:done_attn token stream.
  1975. stateObj.ProcessSniPacket(syncReadPacket, 0);
  1976. return;
  1977. }
  1978. else {
  1979. Debug.Assert(IntPtr.Zero == syncReadPacket, "unexpected syncReadPacket without corresponding SNIPacketRelease");
  1980. fail = true; // Subsequent read failed, time to give up.
  1981. }
  1982. }
  1983. finally {
  1984. if (shouldDecrement) {
  1985. Interlocked.Decrement(ref _readingCount);
  1986. }
  1987. if (syncReadPacket != IntPtr.Zero) {
  1988. // Be sure to release packet, otherwise it will be leaked by native.
  1989. SNINativeMethodWrapper.SNIPacketRelease(syncReadPacket);
  1990. }
  1991. }
  1992. }
  1993. else {
  1994. if (_parser._loginWithFailover)
  1995. {
  1996. // For DbMirroring Failover during login, never break the connection, just close the TdsParser
  1997. _parser.Disconnect();
  1998. }
  1999. else if ((_parser.State == TdsParserState.OpenNotLoggedIn) && (_parser.Connection.ConnectionOptions.MultiSubnetFailover))
  2000. {
  2001. // For MultiSubnet Failover during login, never break the connection, just close the TdsParser
  2002. _parser.Disconnect();
  2003. }
  2004. else
  2005. fail = true; // We aren't yet logged in - just fail.
  2006. }
  2007. }
  2008. }
  2009. if (fail) {
  2010. _parser.State = TdsParserState.Broken; // We failed subsequent read, we have to quit!
  2011. _parser.Connection.BreakConnection();
  2012. }
  2013. }
  2014. else {
  2015. // Caution: ProcessSNIError always returns a fatal error!
  2016. AddError(_parser.ProcessSNIError(stateObj));
  2017. }
  2018. ThrowExceptionAndWarning();
  2019. AssertValidState();
  2020. }
  2021. //
  2022. public void ProcessSniPacket(IntPtr packet, UInt32 error) {
  2023. if (error != 0) {
  2024. if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken)) {
  2025. // Do nothing with with callback if closed or broken and error not 0 - callback can occur
  2026. // after connection has been closed. PROBLEM IN NETLIB - DESIGN FLAW.
  2027. return;
  2028. }
  2029. AddError(_parser.ProcessSNIError(this));
  2030. AssertValidState();
  2031. }
  2032. else {
  2033. UInt32 dataSize = 0;
  2034. UInt32 getDataError = SNINativeMethodWrapper.SNIPacketGetData(packet, _inBuff, ref dataSize);
  2035. if (getDataError == TdsEnums.SNI_SUCCESS) {
  2036. if (_inBuff.Length < dataSize) {
  2037. Debug.Assert(true, "Unexpected dataSize on Read");
  2038. throw SQL.InvalidInternalPacketSize (Res.GetString(Res.SqlMisc_InvalidArraySizeMessage));
  2039. }
  2040. _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks;
  2041. _inBytesRead = (int)dataSize;
  2042. _inBytesUsed = 0;
  2043. if (_snapshot != null) {
  2044. _snapshot.PushBuffer(_inBuff, _inBytesRead);
  2045. if (_snapshotReplay) {
  2046. _snapshot.Replay();
  2047. #if DEBUG
  2048. _snapshot.AssertCurrent();
  2049. #endif
  2050. }
  2051. }
  2052. SniReadStatisticsAndTracing();
  2053. if (Bid.AdvancedOn) {
  2054. Bid.TraceBin("<sc.TdsParser.ReadNetworkPacketAsyncCallback|INFO|ADV> Packet read", _inBuff, (UInt16)_inBytesRead);
  2055. }
  2056. AssertValidState();
  2057. }
  2058. else {
  2059. throw SQL.ParsingError();
  2060. }
  2061. }
  2062. }
  2063. private void ChangeNetworkPacketTimeout(int dueTime, int period) {
  2064. Timer networkPacketTimeout = _networkPacketTimeout;
  2065. if (networkPacketTimeout != null) {
  2066. try {
  2067. networkPacketTimeout.Change(dueTime, period);
  2068. }
  2069. catch (ObjectDisposedException) {
  2070. // _networkPacketTimeout is set to null before Disposing, but there is still a slight chance
  2071. // that object was disposed after we took a copy
  2072. }
  2073. }
  2074. }
  2075. public void ReadAsyncCallback(IntPtr key, IntPtr packet, UInt32 error) { // Key never used.
  2076. // Note - it's possible that when native calls managed that an asynchronous exception
  2077. // could occur in the native->managed transition, which would
  2078. // have two impacts:
  2079. // 1) user event not called
  2080. // 2) DecrementPendingCallbacks not called, which would mean this object would be leaked due
  2081. // to the outstanding GCRoot until AppDomain.Unload.
  2082. // We live with the above for the time being due to the constraints of the current
  2083. // reliability infrastructure provided by the CLR.
  2084. TaskCompletionSource<object> source = _networkPacketTaskSource;
  2085. #if DEBUG
  2086. if ((_forcePendingReadsToWaitForUser) && (_realNetworkPacketTaskSource != null)) {
  2087. source = _realNetworkPacketTaskSource;
  2088. }
  2089. #endif
  2090. // The mars physical connection can get a callback
  2091. // with a packet but no result after the connection is closed.
  2092. if (source == null && _parser._pMarsPhysicalConObj == this) {
  2093. return;
  2094. }
  2095. RuntimeHelpers.PrepareConstrainedRegions();
  2096. bool processFinallyBlock = true;
  2097. try {
  2098. Debug.Assert(IntPtr.Zero == packet || IntPtr.Zero != packet && source != null, "AsyncResult null on callback");
  2099. if (_parser.MARSOn) { // Only take reset lock on MARS and Async.
  2100. CheckSetResetConnectionState(error, CallbackType.Read);
  2101. }
  2102. ChangeNetworkPacketTimeout(Timeout.Infinite, Timeout.Infinite);
  2103. ProcessSniPacket(packet, error);
  2104. }
  2105. catch (Exception e) {
  2106. processFinallyBlock = ADP.IsCatchableExceptionType(e);
  2107. throw;
  2108. }
  2109. finally {
  2110. // pendingCallbacks may be 2 after decrementing, this indicates that a fatal timeout is occuring, and therefore we shouldn't complete the task
  2111. int pendingCallbacks = DecrementPendingCallbacks(false); // may dispose of GC handle.
  2112. if ((processFinallyBlock) && (source != null) && (pendingCallbacks < 2)) {
  2113. if (error == 0) {
  2114. if (_executionContext != null) {
  2115. ExecutionContext.Run(_executionContext, (state) => source.TrySetResult(null), null);
  2116. }
  2117. else {
  2118. source.TrySetResult(null);
  2119. }
  2120. }
  2121. else {
  2122. if (_executionContext != null) {
  2123. ExecutionContext.Run(_executionContext, (state) => ReadAsyncCallbackCaptureException(source), null);
  2124. }
  2125. else {
  2126. ReadAsyncCallbackCaptureException(source);
  2127. }
  2128. }
  2129. }
  2130. AssertValidState();
  2131. }
  2132. }
  2133. private void ReadAsyncCallbackCaptureException(TaskCompletionSource<object> source) {
  2134. bool captureSuccess = false;
  2135. try {
  2136. if (_hasErrorOrWarning) {
  2137. // Do the close on another thread, since we don't want to block the callback thread
  2138. ThrowExceptionAndWarning(asyncClose: true);
  2139. }
  2140. else if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken)) {
  2141. // Connection was closed by another thread before we parsed the packet, so no error was added to the collection
  2142. throw ADP.ClosedConnectionError();
  2143. }
  2144. }
  2145. catch (Exception ex) {
  2146. if (source.TrySetException(ex)) {
  2147. // There was an exception, and it was successfully stored in the task
  2148. captureSuccess = true;
  2149. }
  2150. }
  2151. if (!captureSuccess) {
  2152. // Either there was no exception, or the task was already completed
  2153. // This is unusual, but possible if a fatal timeout occured on another thread (which should mean that the connection is now broken)
  2154. Debug.Assert(_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed || _parser.Connection.IsConnectionDoomed, "Failed to capture exception while the connection was still healthy");
  2155. // The safest thing to do is to ensure that the connection is broken and attempt to cancel the task
  2156. // This must be done from another thread to not block the callback thread
  2157. Task.Factory.StartNew(() => {
  2158. _parser.State = TdsParserState.Broken;
  2159. _parser.Connection.BreakConnection();
  2160. source.TrySetCanceled();
  2161. });
  2162. }
  2163. }
  2164. #pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
  2165. public void WriteAsyncCallback(IntPtr key, IntPtr packet, UInt32 sniError) { // Key never used.
  2166. RemovePacketFromPendingList(packet);
  2167. try {
  2168. if (sniError != TdsEnums.SNI_SUCCESS) {
  2169. Bid.Trace("<sc.TdsParser.WriteAsyncCallback|Info> write async returned error code %d\n", (int)sniError);
  2170. try {
  2171. AddError(_parser.ProcessSNIError(this));
  2172. ThrowExceptionAndWarning(asyncClose: true);
  2173. }
  2174. catch (Exception e) {
  2175. var writeCompletionSource = _writeCompletionSource;
  2176. if (writeCompletionSource != null) {
  2177. writeCompletionSource.TrySetException(e);
  2178. }
  2179. else {
  2180. _delayedWriteAsyncCallbackException = e;
  2181. // Ensure that _delayedWriteAsyncCallbackException is set before checking _writeCompletionSource
  2182. Thread.MemoryBarrier();
  2183. // Double check that _writeCompletionSource hasn't been created in the meantime
  2184. writeCompletionSource = _writeCompletionSource;
  2185. if (writeCompletionSource != null) {
  2186. var delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
  2187. if (delayedException != null) {
  2188. writeCompletionSource.TrySetException(delayedException);
  2189. }
  2190. }
  2191. }
  2192. return;
  2193. }
  2194. }
  2195. else {
  2196. _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks;
  2197. }
  2198. }
  2199. finally {
  2200. #if DEBUG
  2201. if (SqlCommand.DebugForceAsyncWriteDelay > 0) {
  2202. new Timer(obj => {
  2203. Interlocked.Decrement(ref _asyncWriteCount);
  2204. var writeCompletionSource = _writeCompletionSource;
  2205. if (_asyncWriteCount == 0 && writeCompletionSource != null) {
  2206. writeCompletionSource.TrySetResult(null);
  2207. }
  2208. }, null, SqlCommand.DebugForceAsyncWriteDelay, Timeout.Infinite);
  2209. }
  2210. else {
  2211. #else
  2212. {
  2213. #endif
  2214. Interlocked.Decrement(ref _asyncWriteCount);
  2215. }
  2216. }
  2217. #if DEBUG
  2218. if (SqlCommand.DebugForceAsyncWriteDelay > 0) {
  2219. return;
  2220. }
  2221. #endif
  2222. var completionSource = _writeCompletionSource;
  2223. if (_asyncWriteCount == 0 && completionSource != null) {
  2224. completionSource.TrySetResult(null);
  2225. }
  2226. }
  2227. #pragma warning restore 420
  2228. /////////////////////////////////////////
  2229. // Network/Packet Writing & Processing //
  2230. /////////////////////////////////////////
  2231. //
  2232. // Takes a secure string and offsets and saves them for a write latter when the information is written out to SNI Packet
  2233. // This method is provided to better handle the life cycle of the clear text of the secure string
  2234. // This method also ensures that the clear text is not held in the unpined managed buffer so that it avoids getting moved around by CLR garbage collector
  2235. // TdsParserStaticMethods.EncryptPassword operation is also done in the unmanaged buffer for the clear text later
  2236. //
  2237. internal void WriteSecureString(SecureString secureString) {
  2238. TdsParser.ReliabilitySection.Assert("unreliable call to WriteSecureString"); // you need to setup for a thread abort somewhere before you call this method
  2239. Debug.Assert(_securePasswords[0] == null || _securePasswords[1] == null, "There are more than two secure passwords");
  2240. int index = _securePasswords[0] != null ? 1 : 0;
  2241. _securePasswords[index] = secureString;
  2242. _securePasswordOffsetsInBuffer[index] = _outBytesUsed;
  2243. // loop through and write the entire array
  2244. int lengthInBytes = secureString.Length * 2;
  2245. // It is guaranteed both secure password and secure change password should fit into the first packet
  2246. // Given current TDS format and implementation it is not possible that one of secure string is the last item and exactly fill up the output buffer
  2247. // if this ever happens and it is correct situation, the packet needs to be written out after _outBytesUsed is update
  2248. Debug.Assert((_outBytesUsed + lengthInBytes) < _outBuff.Length, "Passwords cannot be splited into two different packet or the last item which fully fill up _outBuff!!!");
  2249. _outBytesUsed += lengthInBytes;
  2250. }
  2251. // ResetSecurePasswordsInformation: clears information regarding secure passwords when login is done; called from TdsParser.TdsLogin
  2252. internal void ResetSecurePasswordsInfomation() {
  2253. for (int i = 0; i < _securePasswords.Length; ++i) {
  2254. _securePasswords[i] = null;
  2255. _securePasswordOffsetsInBuffer[i] = 0;
  2256. }
  2257. }
  2258. internal Task WaitForAccumulatedWrites( ) {
  2259. // Checked for stored exceptions
  2260. #pragma warning disable 420 // A reference to a volatile field will not be treated as volatile - Disabling since the Interlocked APIs are volatile aware
  2261. var delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
  2262. if (delayedException != null) {
  2263. throw delayedException;
  2264. }
  2265. #pragma warning restore 420
  2266. if (_asyncWriteCount == 0) {
  2267. return null;
  2268. }
  2269. _writeCompletionSource = new TaskCompletionSource<object>();
  2270. Task task = _writeCompletionSource.Task;
  2271. // Ensure that _writeCompletionSource is set before checking state
  2272. Thread.MemoryBarrier();
  2273. // Now that we have set _writeCompletionSource, check if parser is closed or broken
  2274. if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken)) {
  2275. throw ADP.ClosedConnectionError();
  2276. }
  2277. // Check for stored exceptions
  2278. #pragma warning disable 420 // A reference to a volatile field will not be treated as volatile - Disabling since the Interlocked APIs are volatile aware
  2279. delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
  2280. if (delayedException != null) {
  2281. throw delayedException;
  2282. }
  2283. #pragma warning restore 420
  2284. // If there are no outstanding writes, see if we can shortcut and return null
  2285. if ((_asyncWriteCount == 0) && ((!task.IsCompleted) || (task.Exception == null))) {
  2286. task = null;
  2287. }
  2288. return task;
  2289. }
  2290. // Takes in a single byte and writes it to the buffer. If the buffer is full, it is flushed
  2291. // and then the buffer is re-initialized in flush() and then the byte is put in the buffer.
  2292. internal void WriteByte(byte b) {
  2293. TdsParser.ReliabilitySection.Assert("unreliable call to WriteByte"); // you need to setup for a thread abort somewhere before you call this method
  2294. Debug.Assert(_outBytesUsed <= _outBuff.Length, "ERROR - TDSParser: _outBytesUsed > _outBuff.Length");
  2295. // check to make sure we haven't used the full amount of space available in the buffer, if so, flush it
  2296. if (_outBytesUsed == _outBuff.Length) {
  2297. WritePacket(TdsEnums.SOFTFLUSH, canAccumulate:true);
  2298. }
  2299. // set byte in buffer and increment the counter for number of bytes used in the out buffer
  2300. _outBuff[_outBytesUsed++] = b;
  2301. }
  2302. //
  2303. // Takes a byte array and writes it to the buffer.
  2304. //
  2305. internal Task WriteByteArray(Byte[] b, int len, int offsetBuffer, bool canAccumulate=true, TaskCompletionSource<object> completion = null) {
  2306. try {
  2307. TdsParser.ReliabilitySection.Assert("unreliable call to WriteByteArray"); // you need to setup for a thread abort somewhere before you call this method
  2308. bool async = _parser._asyncWrite; // NOTE: We are capturing this now for the assert after the Task is returned, since WritePacket will turn off async if there is an exception
  2309. Debug.Assert(async || _asyncWriteCount == 0);
  2310. // Do we have to send out in packet size chunks, or can we rely on netlib layer to break it up?
  2311. // would prefer to to do something like:
  2312. //
  2313. // if (len > what we have room for || len > out buf)
  2314. // flush buffer
  2315. // UnsafeNativeMethods.Write(b)
  2316. //
  2317. int offset = offsetBuffer;
  2318. Debug.Assert(b.Length >= len, "Invalid length sent to WriteByteArray()!");
  2319. // loop through and write the entire array
  2320. do {
  2321. if ((_outBytesUsed + len) > _outBuff.Length) {
  2322. // If the remainder of the string won't fit into the buffer, then we have to put
  2323. // whatever we can into the buffer, and flush that so we can then put more into
  2324. // the buffer on the next loop of the while.
  2325. int remainder = _outBuff.Length - _outBytesUsed;
  2326. // write the remainder
  2327. Buffer.BlockCopy(b, offset, _outBuff, _outBytesUsed, remainder);
  2328. // handle counters
  2329. offset += remainder;
  2330. _outBytesUsed += remainder;
  2331. len -= remainder;
  2332. Task packetTask = WritePacket(TdsEnums.SOFTFLUSH, canAccumulate);
  2333. if (packetTask != null) {
  2334. Task task = null;
  2335. Debug.Assert(async, "Returned task in sync mode");
  2336. if (completion == null) {
  2337. completion = new TaskCompletionSource<object>();
  2338. task = completion.Task; // we only care about return from topmost call, so do not access Task property in other cases
  2339. }
  2340. WriteByteArraySetupContinuation(b, len, completion, offset, packetTask);
  2341. return task;
  2342. }
  2343. }
  2344. else { //((stateObj._outBytesUsed + len) <= stateObj._outBuff.Length )
  2345. // Else the remainder of the string will fit into the buffer, so copy it into the
  2346. // buffer and then break out of the loop.
  2347. Buffer.BlockCopy(b, offset, _outBuff, _outBytesUsed, len);
  2348. // handle out buffer bytes used counter
  2349. _outBytesUsed += len;
  2350. break;
  2351. }
  2352. } while (len > 0);
  2353. if (completion != null) {
  2354. completion.SetResult(null);
  2355. }
  2356. return null;
  2357. }
  2358. catch (Exception e) {
  2359. if (completion != null) {
  2360. completion.SetException(e);
  2361. return null;
  2362. }
  2363. else {
  2364. throw;
  2365. }
  2366. }
  2367. }
  2368. // This is in its own method to avoid always allocating the lambda in WriteByteArray
  2369. private void WriteByteArraySetupContinuation(Byte[] b, int len, TaskCompletionSource<object> completion, int offset, Task packetTask) {
  2370. AsyncHelper.ContinueTask(packetTask, completion,
  2371. () => WriteByteArray(b, len: len, offsetBuffer: offset, canAccumulate: false, completion: completion),
  2372. connectionToDoom: _parser.Connection);
  2373. }
  2374. // Dumps contents of buffer to SNI for network write.
  2375. internal Task WritePacket(byte flushMode, bool canAccumulate = false) {
  2376. if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken)) {
  2377. throw ADP.ClosedConnectionError();
  2378. }
  2379. if (_parser.IsYukonOrNewer && !_bulkCopyOpperationInProgress // ignore the condition checking for bulk copy (SQL BU 414551)
  2380. && _outBytesUsed == (_outputHeaderLen + BitConverter.ToInt32(_outBuff, _outputHeaderLen))
  2381. && _outputPacketNumber == 1
  2382. || _outBytesUsed == _outputHeaderLen
  2383. && _outputPacketNumber == 1) {
  2384. return null;
  2385. }
  2386. byte status;
  2387. byte packetNumber = _outputPacketNumber;
  2388. // Set Status byte based whether this is end of message or not
  2389. bool willCancel = (_cancelled) && (_parser._asyncWrite);
  2390. if (willCancel) {
  2391. status = TdsEnums.ST_EOM | TdsEnums.ST_IGNORE;
  2392. _outputPacketNumber = 1;
  2393. }
  2394. else if (TdsEnums.HARDFLUSH == flushMode) {
  2395. status = TdsEnums.ST_EOM;
  2396. _outputPacketNumber = 1; // end of message - reset to 1 - per ramas
  2397. }
  2398. else if (TdsEnums.SOFTFLUSH==flushMode) {
  2399. status = TdsEnums.ST_BATCH;
  2400. _outputPacketNumber++;
  2401. }
  2402. else {
  2403. status = TdsEnums.ST_EOM;
  2404. Debug.Assert (false, String.Format((IFormatProvider)null, "Unexpected argument {0,-2:x2} to WritePacket", flushMode));
  2405. }
  2406. _outBuff[0] = _outputMessageType; // Message Type
  2407. _outBuff[1] = status;
  2408. _outBuff[2] = (byte)(_outBytesUsed >> 8); // length - upper byte
  2409. _outBuff[3] = (byte)(_outBytesUsed&0xff); // length - lower byte
  2410. _outBuff[4] = 0; // channel
  2411. _outBuff[5] = 0;
  2412. _outBuff[6] = packetNumber; // packet
  2413. _outBuff[7] = 0; // window
  2414. Task task=null;
  2415. _parser.CheckResetConnection(this); // HAS SIDE EFFECTS - re-org at a later time if possible
  2416. task = WriteSni(canAccumulate);
  2417. AssertValidState();
  2418. if (willCancel) {
  2419. // If we have been cancelled, then ensure that we write the ATTN packet as well
  2420. task = AsyncHelper.CreateContinuationTask(task, CancelWritePacket, _parser.Connection);
  2421. }
  2422. return task;
  2423. }
  2424. private void CancelWritePacket() {
  2425. Debug.Assert(_cancelled, "Should not call CancelWritePacket if _cancelled is not set");
  2426. _parser.Connection.ThreadHasParserLockForClose = true; // In case of error, let the connection know that we are holding the lock
  2427. try {
  2428. // Send the attention and wait for the ATTN_ACK
  2429. SendAttention();
  2430. ResetCancelAndProcessAttention();
  2431. // Let the caller know that we've given up
  2432. throw SQL.OperationCancelled();
  2433. }
  2434. finally {
  2435. _parser.Connection.ThreadHasParserLockForClose = false;
  2436. }
  2437. }
  2438. #pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
  2439. private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out UInt32 sniError, bool canAccumulate, bool callerHasConnectionLock) {
  2440. // Check for a stored exception
  2441. var delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
  2442. if (delayedException != null) {
  2443. throw delayedException;
  2444. }
  2445. Task task = null;
  2446. _writeCompletionSource = null;
  2447. IntPtr packetPointer = IntPtr.Zero;
  2448. bool sync = !_parser._asyncWrite;
  2449. if (sync && _asyncWriteCount > 0) { // for example, SendAttention while there are writes pending
  2450. Task waitForWrites = WaitForAccumulatedWrites();
  2451. if (waitForWrites != null) {
  2452. try {
  2453. waitForWrites.Wait();
  2454. }
  2455. catch (AggregateException ae) {
  2456. throw ae.InnerException;
  2457. }
  2458. }
  2459. Debug.Assert(_asyncWriteCount == 0, "All async write should be finished");
  2460. }
  2461. if (!sync) {
  2462. // Add packet to the pending list (since the callback can happen any time after we call SNIWritePacket)
  2463. packetPointer = AddPacketToPendingList(packet);
  2464. }
  2465. // Async operation completion may be delayed (success pending).
  2466. RuntimeHelpers.PrepareConstrainedRegions();
  2467. try {
  2468. }
  2469. finally {
  2470. sniError = SNINativeMethodWrapper.SNIWritePacket(handle, packet, sync);
  2471. }
  2472. if (sniError == TdsEnums.SNI_SUCCESS_IO_PENDING) {
  2473. Debug.Assert(!sync, "Completion should be handled in SniManagedWwrapper");
  2474. Interlocked.Increment(ref _asyncWriteCount);
  2475. Debug.Assert(_asyncWriteCount >= 0);
  2476. if (!canAccumulate) {
  2477. // Create completion source (for callback to complete)
  2478. _writeCompletionSource = new TaskCompletionSource<object>();
  2479. task = _writeCompletionSource.Task;
  2480. // Ensure that setting _writeCompletionSource completes before checking _delayedWriteAsyncCallbackException
  2481. Thread.MemoryBarrier();
  2482. // Check for a stored exception
  2483. delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
  2484. if (delayedException != null) {
  2485. throw delayedException;
  2486. }
  2487. // If there are no outstanding writes, see if we can shortcut and return null
  2488. if ((_asyncWriteCount == 0) && ((!task.IsCompleted) || (task.Exception == null))) {
  2489. task = null;
  2490. }
  2491. }
  2492. }
  2493. #if DEBUG
  2494. else if (!sync && !canAccumulate && SqlCommand.DebugForceAsyncWriteDelay > 0) {
  2495. // Executed synchronously - callback will not be called
  2496. TaskCompletionSource<object> completion = new TaskCompletionSource<object>();
  2497. uint error = sniError;
  2498. new Timer(obj=>{
  2499. try {
  2500. if (_parser.MARSOn) { // Only take reset lock on MARS.
  2501. CheckSetResetConnectionState(error, CallbackType.Write);
  2502. }
  2503. if (error != TdsEnums.SNI_SUCCESS) {
  2504. Bid.Trace("<sc.TdsParser.WritePacket|Info> write async returned error code %d\n", (int)error);
  2505. AddError(_parser.ProcessSNIError(this));
  2506. ThrowExceptionAndWarning();
  2507. }
  2508. AssertValidState();
  2509. completion.SetResult(null);
  2510. }
  2511. catch (Exception e) {
  2512. completion.SetException(e);
  2513. }
  2514. },null,SqlCommand.DebugForceAsyncWriteDelay,Timeout.Infinite);
  2515. task = completion.Task;
  2516. }
  2517. #endif
  2518. else {
  2519. if (_parser.MARSOn) { // Only take reset lock on MARS.
  2520. CheckSetResetConnectionState(sniError, CallbackType.Write);
  2521. }
  2522. if (sniError == TdsEnums.SNI_SUCCESS) {
  2523. _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks;
  2524. if (!sync) {
  2525. // Since there will be no callback, remove the packet from the pending list
  2526. Debug.Assert(packetPointer != IntPtr.Zero, "Packet added to list has an invalid pointer, can not remove from pending list");
  2527. RemovePacketFromPendingList(packetPointer);
  2528. }
  2529. }
  2530. else {
  2531. Bid.Trace("<sc.TdsParser.WritePacket|Info> write async returned error code %d\n", (int)sniError);
  2532. AddError(_parser.ProcessSNIError(this));
  2533. ThrowExceptionAndWarning(callerHasConnectionLock);
  2534. }
  2535. AssertValidState();
  2536. }
  2537. return task;
  2538. }
  2539. #pragma warning restore 420
  2540. // Sends an attention signal - executing thread will consume attn.
  2541. internal void SendAttention(bool mustTakeWriteLock = false) {
  2542. if (!_attentionSent) {
  2543. // Dumps contents of buffer to OOB write (currently only used for
  2544. // attentions. There is no body for this message
  2545. // Doesn't touch this._outBytesUsed
  2546. if (_parser.State == TdsParserState.Closed || _parser.State == TdsParserState.Broken) {
  2547. return;
  2548. }
  2549. SNIPacket attnPacket = new SNIPacket(Handle);
  2550. _sniAsyncAttnPacket = attnPacket;
  2551. SNINativeMethodWrapper.SNIPacketSetData(attnPacket, SQL.AttentionHeader, TdsEnums.HEADER_LEN, null, null);
  2552. RuntimeHelpers.PrepareConstrainedRegions();
  2553. try {
  2554. // Dev11 #344723: SqlClient stress hang System_Data!Tcp::ReadSync via a call to SqlDataReader::Close
  2555. // Set _attentionSending to true before sending attention and reset after setting _attentionSent
  2556. // This prevents a race condition between receiving the attention ACK and setting _attentionSent
  2557. _attentionSending = true;
  2558. #if DEBUG
  2559. if (!_skipSendAttention)
  2560. {
  2561. #endif
  2562. // Take lock and send attention
  2563. bool releaseLock = false;
  2564. if ((mustTakeWriteLock) && (!_parser.Connection.ThreadHasParserLockForClose)) {
  2565. releaseLock = true;
  2566. _parser.Connection._parserLock.Wait(canReleaseFromAnyThread: false);
  2567. _parser.Connection.ThreadHasParserLockForClose = true;
  2568. }
  2569. try {
  2570. // Check again (just in case the connection was closed while we were waiting)
  2571. if (_parser.State == TdsParserState.Closed || _parser.State == TdsParserState.Broken) {
  2572. return;
  2573. }
  2574. UInt32 sniError;
  2575. _parser._asyncWrite = false; // stop async write
  2576. SNIWritePacket(Handle, attnPacket, out sniError, canAccumulate:false, callerHasConnectionLock: false);
  2577. Bid.Trace("<sc.TdsParser.SendAttention|Info> Send Attention ASync .\n");
  2578. }
  2579. finally {
  2580. if (releaseLock) {
  2581. _parser.Connection.ThreadHasParserLockForClose = false;
  2582. _parser.Connection._parserLock.Release();
  2583. }
  2584. }
  2585. #if DEBUG
  2586. }
  2587. #endif
  2588. SetTimeoutSeconds(AttentionTimeoutSeconds); // Initialize new attention timeout of 5 seconds.
  2589. _attentionSent = true;
  2590. }
  2591. finally {
  2592. _attentionSending = false;
  2593. }
  2594. if (Bid.AdvancedOn) {
  2595. Bid.TraceBin("<sc.TdsParser.WritePacket|INFO|ADV> Packet sent", _outBuff, (UInt16)_outBytesUsed);
  2596. }
  2597. Bid.Trace("<sc.TdsParser.SendAttention|Info> Attention sent to the server.\n");
  2598. AssertValidState();
  2599. }
  2600. }
  2601. private Task WriteSni(bool canAccumulate) {
  2602. // Prepare packet, and write to packet.
  2603. SNIPacket packet = GetResetWritePacket();
  2604. SNINativeMethodWrapper.SNIPacketSetData(packet, _outBuff, _outBytesUsed, _securePasswords, _securePasswordOffsetsInBuffer);
  2605. uint sniError;
  2606. Debug.Assert(Parser.Connection._parserLock.ThreadMayHaveLock(), "Thread is writing without taking the connection lock");
  2607. Task task = SNIWritePacket(Handle, packet, out sniError, canAccumulate, callerHasConnectionLock: true);
  2608. // Check to see if the timeout has occured. This time out code is special case code to allow BCP writes to timeout to fix bug 350558, eventually we should make all writes timeout.
  2609. if (_bulkCopyOpperationInProgress && 0 == GetTimeoutRemaining()) {
  2610. _parser.Connection.ThreadHasParserLockForClose = true;
  2611. try {
  2612. Debug.Assert(_parser.Connection != null, "SqlConnectionInternalTds handler can not be null at this point.");
  2613. AddError(new SqlError(TdsEnums.TIMEOUT_EXPIRED, (byte)0x00, TdsEnums.MIN_ERROR_CLASS, _parser.Server, _parser.Connection.TimeoutErrorInternal.GetErrorMessage(), "", 0, TdsEnums.SNI_WAIT_TIMEOUT));
  2614. _bulkCopyWriteTimeout = true;
  2615. SendAttention();
  2616. _parser.ProcessPendingAck(this);
  2617. ThrowExceptionAndWarning();
  2618. }
  2619. finally {
  2620. _parser.Connection.ThreadHasParserLockForClose = false;
  2621. }
  2622. }
  2623. // Special case logic for encryption removal.
  2624. //
  2625. if (_parser.State == TdsParserState.OpenNotLoggedIn &&
  2626. _parser.EncryptionOptions == EncryptionOptions.LOGIN) {
  2627. // If no error occurred, and we are Open but not logged in, and
  2628. // our encryptionOption state is login, remove the SSL Provider.
  2629. // We only need encrypt the very first packet of the login message to the server.
  2630. // SQL BU DT 332481 - we wanted to encrypt entire login channel, but there is
  2631. // currently no mechanism to communicate this. Removing encryption post 1st packet
  2632. // is a hard-coded agreement between client and server. We need some mechanism or
  2633. // common change to be able to make this change in a non-breaking fasion.
  2634. _parser.RemoveEncryption(); // Remove the SSL Provider.
  2635. _parser.EncryptionOptions = EncryptionOptions.OFF; // Turn encryption off.
  2636. // Since this packet was associated with encryption, dispose and re-create.
  2637. ClearAllWritePackets();
  2638. }
  2639. SniWriteStatisticsAndTracing();
  2640. ResetBuffer();
  2641. AssertValidState();
  2642. return task;
  2643. }
  2644. internal SNIPacket GetResetWritePacket() {
  2645. if (_sniPacket != null) {
  2646. SNINativeMethodWrapper.SNIPacketReset(Handle, SNINativeMethodWrapper.IOType.WRITE, _sniPacket, SNINativeMethodWrapper.ConsumerNumber.SNI_Consumer_SNI);
  2647. }
  2648. else {
  2649. lock (_writePacketLockObject) {
  2650. _sniPacket = _writePacketCache.Take(Handle);
  2651. }
  2652. }
  2653. return _sniPacket;
  2654. }
  2655. internal void ClearAllWritePackets() {
  2656. if (_sniPacket != null) {
  2657. _sniPacket.Dispose();
  2658. _sniPacket = null;
  2659. }
  2660. lock (_writePacketLockObject) {
  2661. Debug.Assert(_pendingWritePackets.Count == 0 && _asyncWriteCount == 0, "Should not clear all write packets if there are packets pending");
  2662. _writePacketCache.Clear();
  2663. }
  2664. }
  2665. private IntPtr AddPacketToPendingList(SNIPacket packet) {
  2666. Debug.Assert(packet == _sniPacket, "Adding a packet other than the current packet to the pending list");
  2667. _sniPacket = null;
  2668. IntPtr pointer = packet.DangerousGetHandle();
  2669. lock (_writePacketLockObject) {
  2670. _pendingWritePackets.Add(pointer, packet);
  2671. }
  2672. return pointer;
  2673. }
  2674. private void RemovePacketFromPendingList(IntPtr pointer) {
  2675. SNIPacket recoveredPacket;
  2676. lock (_writePacketLockObject) {
  2677. if (_pendingWritePackets.TryGetValue(pointer, out recoveredPacket)) {
  2678. _pendingWritePackets.Remove(pointer);
  2679. _writePacketCache.Add(recoveredPacket);
  2680. }
  2681. #if DEBUG
  2682. else {
  2683. Debug.Assert(false, "Removing a packet from the pending list that was never added to it");
  2684. }
  2685. #endif
  2686. }
  2687. }
  2688. //////////////////////////////////////////////
  2689. // Statistics, Tracing, and related methods //
  2690. //////////////////////////////////////////////
  2691. private void SniReadStatisticsAndTracing() {
  2692. SqlStatistics statistics = Parser.Statistics;
  2693. if (null != statistics) {
  2694. if (statistics.WaitForReply) {
  2695. statistics.SafeIncrement(ref statistics._serverRoundtrips);
  2696. statistics.ReleaseAndUpdateNetworkServerTimer();
  2697. }
  2698. statistics.SafeAdd(ref statistics._bytesReceived, _inBytesRead);
  2699. statistics.SafeIncrement(ref statistics._buffersReceived);
  2700. }
  2701. }
  2702. private void SniWriteStatisticsAndTracing() {
  2703. SqlStatistics statistics = _parser.Statistics;
  2704. if (null != statistics) {
  2705. statistics.SafeIncrement(ref statistics._buffersSent);
  2706. statistics.SafeAdd(ref statistics._bytesSent, _outBytesUsed);
  2707. statistics.RequestNetworkServerTimer();
  2708. }
  2709. if (Bid.AdvancedOn) {
  2710. // If we have tracePassword variables set, we are flushing TDSLogin and so we need to
  2711. // blank out password in buffer. Buffer has already been sent to netlib, so no danger
  2712. // of losing info.
  2713. if (_tracePasswordOffset != 0) {
  2714. for (int i = _tracePasswordOffset; i < _tracePasswordOffset +
  2715. _tracePasswordLength; i++) {
  2716. _outBuff[i] = 0;
  2717. }
  2718. // Reset state.
  2719. _tracePasswordOffset = 0;
  2720. _tracePasswordLength = 0;
  2721. }
  2722. if (_traceChangePasswordOffset != 0) {
  2723. for (int i = _traceChangePasswordOffset; i < _traceChangePasswordOffset +
  2724. _traceChangePasswordLength; i++) {
  2725. _outBuff[i] = 0;
  2726. }
  2727. // Reset state.
  2728. _traceChangePasswordOffset = 0;
  2729. _traceChangePasswordLength = 0;
  2730. }
  2731. Bid.TraceBin("<sc.TdsParser.WritePacket|INFO|ADV> Packet sent", _outBuff, (UInt16)_outBytesUsed);
  2732. }
  2733. }
  2734. [Conditional("DEBUG")]
  2735. void AssertValidState() {
  2736. string assertMessage = null;
  2737. if (_inBytesUsed < 0 || _inBytesRead < 0) {
  2738. assertMessage = string.Format(
  2739. CultureInfo.InvariantCulture,
  2740. "either _inBytesUsed or _inBytesRead is negative: {0}, {1}",
  2741. _inBytesUsed, _inBytesRead);
  2742. }
  2743. else if (_inBytesUsed > _inBytesRead) {
  2744. assertMessage = string.Format(
  2745. CultureInfo.InvariantCulture,
  2746. "_inBytesUsed > _inBytesRead: {0} > {1}",
  2747. _inBytesUsed, _inBytesRead);
  2748. }
  2749. //
  2750. if (assertMessage != null) {
  2751. Debug.Assert(false, "Invalid TDS Parser State: " + assertMessage);
  2752. }
  2753. Debug.Assert(_inBytesPacket >= 0, "Packet must not be negative");
  2754. }
  2755. //////////////////////////////////////////////
  2756. // Errors and Warnings //
  2757. //////////////////////////////////////////////
  2758. /// <summary>
  2759. /// True if there is at least one error or warning (not counting the pre-attention errors\warnings)
  2760. /// </summary>
  2761. internal bool HasErrorOrWarning {
  2762. get {
  2763. return _hasErrorOrWarning;
  2764. }
  2765. }
  2766. /// <summary>
  2767. /// Adds an error to the error collection
  2768. /// </summary>
  2769. /// <param name="error"></param>
  2770. internal void AddError(SqlError error) {
  2771. Debug.Assert(error != null, "Trying to add a null error");
  2772. // Switch to [....] once we see an error
  2773. _syncOverAsync = true;
  2774. lock (_errorAndWarningsLock) {
  2775. _hasErrorOrWarning = true;
  2776. if (_errors == null) {
  2777. _errors = new SqlErrorCollection();
  2778. }
  2779. _errors.Add(error);
  2780. }
  2781. }
  2782. /// <summary>
  2783. /// Gets the number of errors currently in the error collection
  2784. /// </summary>
  2785. internal int ErrorCount {
  2786. get {
  2787. int count = 0;
  2788. lock (_errorAndWarningsLock){
  2789. if (_errors != null) {
  2790. count = _errors.Count;
  2791. }
  2792. }
  2793. return count;
  2794. }
  2795. }
  2796. /// <summary>
  2797. /// Adds an warning to the warning collection
  2798. /// </summary>
  2799. /// <param name="error"></param>
  2800. internal void AddWarning(SqlError error) {
  2801. Debug.Assert(error != null, "Trying to add a null error");
  2802. // Switch to [....] once we see a warning
  2803. _syncOverAsync = true;
  2804. lock (_errorAndWarningsLock){
  2805. _hasErrorOrWarning = true;
  2806. if (_warnings == null) {
  2807. _warnings = new SqlErrorCollection();
  2808. }
  2809. _warnings.Add(error);
  2810. }
  2811. }
  2812. /// <summary>
  2813. /// Gets the number of warnings currently in the warning collection
  2814. /// </summary>
  2815. internal int WarningCount {
  2816. get {
  2817. int count = 0;
  2818. lock (_errorAndWarningsLock){
  2819. if (_warnings != null) {
  2820. count = _warnings.Count;
  2821. }
  2822. }
  2823. return count;
  2824. }
  2825. }
  2826. /// <summary>
  2827. /// Gets the number of errors currently in the pre-attention error collection
  2828. /// </summary>
  2829. internal int PreAttentionErrorCount {
  2830. get {
  2831. int count = 0;
  2832. lock (_errorAndWarningsLock){
  2833. if (_preAttentionErrors != null) {
  2834. count = _preAttentionErrors.Count;
  2835. }
  2836. }
  2837. return count;
  2838. }
  2839. }
  2840. /// <summary>
  2841. /// Gets the number of errors currently in the pre-attention warning collection
  2842. /// </summary>
  2843. internal int PreAttentionWarningCount {
  2844. get {
  2845. int count = 0;
  2846. lock (_errorAndWarningsLock){
  2847. if (_preAttentionWarnings != null) {
  2848. count = _preAttentionWarnings.Count;
  2849. }
  2850. }
  2851. return count;
  2852. }
  2853. }
  2854. /// <summary>
  2855. /// Gets the full list of errors and warnings (including the pre-attention ones), then wipes all error and warning lists
  2856. /// </summary>
  2857. /// <param name="broken">If true, the connection should be broken</param>
  2858. /// <returns>An array containing all of the errors and warnings</returns>
  2859. internal SqlErrorCollection GetFullErrorAndWarningCollection(out bool broken) {
  2860. SqlErrorCollection allErrors = new SqlErrorCollection();
  2861. broken = false;
  2862. lock (_errorAndWarningsLock){
  2863. _hasErrorOrWarning = false;
  2864. // Merge all error lists, then reset them
  2865. AddErrorsToCollection(_errors, ref allErrors, ref broken);
  2866. AddErrorsToCollection(_warnings, ref allErrors, ref broken);
  2867. _errors = null;
  2868. _warnings = null;
  2869. // We also process the pre-attention error lists here since, if we are here and they are populated, then an error occured while sending attention so we should show the errors now (otherwise they'd be lost)
  2870. AddErrorsToCollection(_preAttentionErrors, ref allErrors, ref broken);
  2871. AddErrorsToCollection(_preAttentionWarnings, ref allErrors, ref broken);
  2872. _preAttentionErrors = null;
  2873. _preAttentionWarnings = null;
  2874. }
  2875. return allErrors;
  2876. }
  2877. private void AddErrorsToCollection(SqlErrorCollection inCollection, ref SqlErrorCollection collectionToAddTo, ref bool broken) {
  2878. if (inCollection != null) {
  2879. foreach (SqlError error in inCollection) {
  2880. collectionToAddTo.Add(error);
  2881. broken |= (error.Class >= TdsEnums.FATAL_ERROR_CLASS);
  2882. }
  2883. }
  2884. }
  2885. /// <summary>
  2886. /// Stores away current errors and warnings so that an attention can be processed
  2887. /// </summary>
  2888. internal void StoreErrorAndWarningForAttention() {
  2889. lock (_errorAndWarningsLock){
  2890. Debug.Assert(_preAttentionErrors == null && _preAttentionWarnings == null, "Can't store errors for attention because there are already errors stored");
  2891. _hasErrorOrWarning = false;
  2892. _preAttentionErrors = _errors;
  2893. _preAttentionWarnings = _warnings;
  2894. _errors = null;
  2895. _warnings = null;
  2896. }
  2897. }
  2898. /// <summary>
  2899. /// Restores errors and warnings that were stored in order to process an attention
  2900. /// </summary>
  2901. internal void RestoreErrorAndWarningAfterAttention() {
  2902. lock (_errorAndWarningsLock){
  2903. Debug.Assert(_errors == null && _warnings == null, "Can't restore errors after attention because there are already other errors");
  2904. _hasErrorOrWarning = (((_preAttentionErrors != null) && (_preAttentionErrors.Count > 0)) || ((_preAttentionWarnings != null) && (_preAttentionWarnings.Count > 0)));
  2905. _errors = _preAttentionErrors;
  2906. _warnings = _preAttentionWarnings;
  2907. _preAttentionErrors = null;
  2908. _preAttentionWarnings = null;
  2909. }
  2910. }
  2911. /// <summary>
  2912. /// Checks if an error is stored in _error and, if so, throws an error
  2913. /// </summary>
  2914. internal void CheckThrowSNIException() {
  2915. if (HasErrorOrWarning) {
  2916. ThrowExceptionAndWarning();
  2917. }
  2918. }
  2919. /// <summary>
  2920. /// Debug Only: Ensures that the TdsParserStateObject has no lingering state and can safely be re-used
  2921. /// </summary>
  2922. [Conditional("DEBUG")]
  2923. internal void AssertStateIsClean() {
  2924. // If our TdsParser is closed or broken, then we don't really care about our state
  2925. var parser = _parser;
  2926. if ((parser != null) && (parser.State != TdsParserState.Closed) && (parser.State != TdsParserState.Broken)) {
  2927. // Async reads
  2928. Debug.Assert(_snapshot == null && !_snapshotReplay, "StateObj has leftover snapshot state");
  2929. Debug.Assert(!_asyncReadWithoutSnapshot, "StateObj has AsyncReadWithoutSnapshot still enabled");
  2930. Debug.Assert(_executionContext == null, "StateObj has a stored execution context from an async read");
  2931. // Async writes
  2932. Debug.Assert(_asyncWriteCount == 0, "StateObj still has outstanding async writes");
  2933. Debug.Assert(_delayedWriteAsyncCallbackException == null, "StateObj has an unobserved exceptions from an async write");
  2934. // Attention\Cancellation\Timeouts
  2935. Debug.Assert(!_attentionReceived && !_attentionSent && !_attentionSending, string.Format("StateObj is still dealing with attention: Sent: {0}, Received: {1}, Sending: {2}", _attentionSent, _attentionReceived, _attentionSending));
  2936. Debug.Assert(!_cancelled, "StateObj still has cancellation set");
  2937. Debug.Assert(!_internalTimeout, "StateObj still has internal timeout set");
  2938. // Errors and Warnings
  2939. Debug.Assert(!_hasErrorOrWarning, "StateObj still has stored errors or warnings");
  2940. }
  2941. }
  2942. #if DEBUG
  2943. internal void CompletePendingReadWithSuccess(bool resetForcePendingReadsToWait) {
  2944. var realNetworkPacketTaskSource = _realNetworkPacketTaskSource;
  2945. var networkPacketTaskSource = _networkPacketTaskSource;
  2946. Debug.Assert(_forcePendingReadsToWaitForUser, "Not forcing pends to wait for user - can't force complete");
  2947. Debug.Assert(networkPacketTaskSource != null, "No pending read to complete");
  2948. try {
  2949. if (realNetworkPacketTaskSource != null) {
  2950. // Wait for the real read to complete
  2951. realNetworkPacketTaskSource.Task.Wait();
  2952. }
  2953. }
  2954. finally {
  2955. if (networkPacketTaskSource != null) {
  2956. if (resetForcePendingReadsToWait) {
  2957. _forcePendingReadsToWaitForUser = false;
  2958. }
  2959. networkPacketTaskSource.TrySetResult(null);
  2960. }
  2961. }
  2962. }
  2963. internal void CompletePendingReadWithFailure(int errorCode, bool resetForcePendingReadsToWait) {
  2964. var realNetworkPacketTaskSource = _realNetworkPacketTaskSource;
  2965. var networkPacketTaskSource = _networkPacketTaskSource;
  2966. Debug.Assert(_forcePendingReadsToWaitForUser, "Not forcing pends to wait for user - can't force complete");
  2967. Debug.Assert(networkPacketTaskSource != null, "No pending read to complete");
  2968. try {
  2969. if (realNetworkPacketTaskSource != null) {
  2970. // Wait for the real read to complete
  2971. realNetworkPacketTaskSource.Task.Wait();
  2972. }
  2973. }
  2974. finally {
  2975. if (networkPacketTaskSource != null) {
  2976. if (resetForcePendingReadsToWait) {
  2977. _forcePendingReadsToWaitForUser = false;
  2978. }
  2979. AddError(new SqlError(errorCode, 0x00, TdsEnums.FATAL_ERROR_CLASS, _parser.Server, string.Empty, string.Empty, 0));
  2980. try {
  2981. ThrowExceptionAndWarning();
  2982. }
  2983. catch (Exception ex) {
  2984. networkPacketTaskSource.TrySetException(ex);
  2985. }
  2986. }
  2987. }
  2988. }
  2989. #endif
  2990. internal void CloneCleanupAltMetaDataSetArray()
  2991. {
  2992. if (_snapshot != null) {
  2993. _snapshot.CloneCleanupAltMetaDataSetArray();
  2994. }
  2995. }
  2996. class PacketData {
  2997. public byte[] Buffer;
  2998. public int Read;
  2999. #if DEBUG
  3000. public StackTrace Stack;
  3001. #endif
  3002. }
  3003. class StateSnapshot
  3004. {
  3005. private List<PacketData> _snapshotInBuffs;
  3006. private int _snapshotInBuffCurrent = 0;
  3007. private int _snapshotInBytesUsed = 0;
  3008. private int _snapshotInBytesPacket = 0;
  3009. private bool _snapshotPendingData = false;
  3010. private bool _snapshotErrorTokenReceived = false;
  3011. private bool _snapshotHasOpenResult = false;
  3012. private bool _snapshotReceivedColumnMetadata = false;
  3013. private bool _snapshotAttentionReceived;
  3014. private byte _snapshotMessageStatus;
  3015. private NullBitmap _snapshotNullBitmapInfo;
  3016. private ulong _snapshotLongLen;
  3017. private ulong _snapshotLongLenLeft;
  3018. private _SqlMetaDataSet _snapshotCleanupMetaData;
  3019. private _SqlMetaDataSetCollection _snapshotCleanupAltMetaDataSetArray;
  3020. private readonly TdsParserStateObject _stateObj;
  3021. public StateSnapshot(TdsParserStateObject state) {
  3022. _snapshotInBuffs = new List<PacketData>();
  3023. _stateObj = state;
  3024. }
  3025. #if DEBUG
  3026. private int _rollingPend = 0;
  3027. private int _rollingPendCount = 0;
  3028. internal bool DoPend() {
  3029. if (_failAsyncPends || !_forceAllPends) {
  3030. return false;
  3031. }
  3032. if (_rollingPendCount == _rollingPend) {
  3033. _rollingPend++;
  3034. _rollingPendCount = 0;
  3035. return true;
  3036. }
  3037. _rollingPendCount++;
  3038. return false;
  3039. }
  3040. #endif
  3041. internal void CloneNullBitmapInfo() {
  3042. if (_stateObj._nullBitmapInfo.ReferenceEquals(_snapshotNullBitmapInfo)) {
  3043. _stateObj._nullBitmapInfo = _stateObj._nullBitmapInfo.Clone();
  3044. }
  3045. }
  3046. internal void CloneCleanupAltMetaDataSetArray() {
  3047. if (_stateObj._cleanupAltMetaDataSetArray != null && object.ReferenceEquals(_snapshotCleanupAltMetaDataSetArray, _stateObj._cleanupAltMetaDataSetArray)) {
  3048. _stateObj._cleanupAltMetaDataSetArray = (_SqlMetaDataSetCollection)_stateObj._cleanupAltMetaDataSetArray.Clone();
  3049. }
  3050. }
  3051. internal void PushBuffer(byte[] buffer, int read) {
  3052. Debug.Assert(!_snapshotInBuffs.Any(b => object.ReferenceEquals(b, buffer)));
  3053. PacketData packetData = new PacketData();
  3054. packetData.Buffer = buffer;
  3055. packetData.Read = read;
  3056. #if DEBUG
  3057. packetData.Stack = _stateObj._lastStack;
  3058. #endif
  3059. _snapshotInBuffs.Add(packetData);
  3060. }
  3061. #if DEBUG
  3062. internal void AssertCurrent()
  3063. {
  3064. Debug.Assert(_snapshotInBuffCurrent == _snapshotInBuffs.Count, "Should not be reading new packets when not replaying last packet");
  3065. }
  3066. internal void CheckStack(StackTrace trace)
  3067. {
  3068. PacketData prev = _snapshotInBuffs[_snapshotInBuffCurrent - 1];
  3069. if (prev.Stack == null)
  3070. {
  3071. prev.Stack = trace;
  3072. }
  3073. else
  3074. {
  3075. Debug.Assert(_stateObj._permitReplayStackTraceToDiffer || prev.Stack.ToString() == trace.ToString(), "The stack trace on subsequent replays should be the same");
  3076. }
  3077. }
  3078. #endif
  3079. internal bool Replay() {
  3080. if (_snapshotInBuffCurrent < _snapshotInBuffs.Count) {
  3081. PacketData next = _snapshotInBuffs[_snapshotInBuffCurrent];
  3082. _stateObj._inBuff = next.Buffer;
  3083. _stateObj._inBytesUsed = 0;
  3084. _stateObj._inBytesRead = next.Read;
  3085. _snapshotInBuffCurrent++;
  3086. return true;
  3087. }
  3088. return false;
  3089. }
  3090. internal void Snap()
  3091. {
  3092. _snapshotInBuffs.Clear();
  3093. _snapshotInBuffCurrent = 0;
  3094. _snapshotInBytesUsed = _stateObj._inBytesUsed;
  3095. _snapshotInBytesPacket = _stateObj._inBytesPacket;
  3096. _snapshotPendingData = _stateObj._pendingData;
  3097. _snapshotErrorTokenReceived = _stateObj._errorTokenReceived;
  3098. _snapshotMessageStatus = _stateObj._messageStatus;
  3099. // _nullBitmapInfo must be cloned before it is updated
  3100. _snapshotNullBitmapInfo = _stateObj._nullBitmapInfo;
  3101. _snapshotLongLen = _stateObj._longlen;
  3102. _snapshotLongLenLeft = _stateObj._longlenleft;
  3103. _snapshotCleanupMetaData = _stateObj._cleanupMetaData;
  3104. // _cleanupAltMetaDataSetArray must be cloned bofore it is updated
  3105. _snapshotCleanupAltMetaDataSetArray = _stateObj._cleanupAltMetaDataSetArray;
  3106. _snapshotHasOpenResult = _stateObj._hasOpenResult;
  3107. _snapshotReceivedColumnMetadata = _stateObj._receivedColMetaData;
  3108. _snapshotAttentionReceived = _stateObj._attentionReceived;
  3109. #if DEBUG
  3110. _rollingPend = 0;
  3111. _rollingPendCount = 0;
  3112. _stateObj._lastStack = null;
  3113. Debug.Assert(_stateObj._bTmpRead == 0, "Has partially read data when snapshot taken");
  3114. Debug.Assert(_stateObj._partialHeaderBytesRead == 0, "Has partially read header when shapshot taken");
  3115. #endif
  3116. PushBuffer(_stateObj._inBuff, _stateObj._inBytesRead);
  3117. }
  3118. internal void ResetSnapshotState() {
  3119. // go back to the beginning
  3120. _snapshotInBuffCurrent = 0;
  3121. Replay();
  3122. _stateObj._inBytesUsed = _snapshotInBytesUsed;
  3123. _stateObj._inBytesPacket = _snapshotInBytesPacket;
  3124. _stateObj._pendingData = _snapshotPendingData;
  3125. _stateObj._errorTokenReceived = _snapshotErrorTokenReceived;
  3126. _stateObj._messageStatus = _snapshotMessageStatus;
  3127. _stateObj._nullBitmapInfo = _snapshotNullBitmapInfo;
  3128. _stateObj._cleanupMetaData = _snapshotCleanupMetaData;
  3129. _stateObj._cleanupAltMetaDataSetArray = _snapshotCleanupAltMetaDataSetArray;
  3130. _stateObj._hasOpenResult = _snapshotHasOpenResult;
  3131. _stateObj._receivedColMetaData = _snapshotReceivedColumnMetadata;
  3132. _stateObj._attentionReceived = _snapshotAttentionReceived;
  3133. // Reset partially read state (these only need to be maintained if doing async without snapshot)
  3134. _stateObj._bTmpRead = 0;
  3135. _stateObj._partialHeaderBytesRead = 0;
  3136. // reset plp state
  3137. _stateObj._longlen = _snapshotLongLen;
  3138. _stateObj._longlenleft = _snapshotLongLenLeft;
  3139. _stateObj._snapshotReplay = true;
  3140. _stateObj.AssertValidState();
  3141. }
  3142. internal void PrepareReplay() {
  3143. ResetSnapshotState();
  3144. }
  3145. }
  3146. /*
  3147. // leave this in. comes handy if you have to do Console.WriteLine style debugging ;)
  3148. private void DumpBuffer() {
  3149. Console.WriteLine("dumping buffer");
  3150. Console.WriteLine("_inBytesRead = {0}", _inBytesRead);
  3151. Console.WriteLine("_inBytesUsed = {0}", _inBytesUsed);
  3152. int cc = 0; // character counter
  3153. int i;
  3154. Console.WriteLine("used buffer:");
  3155. for (i=0; i< _inBytesUsed; i++) {
  3156. if (cc==16) {
  3157. Console.WriteLine();
  3158. cc = 0;
  3159. }
  3160. Console.Write("{0,-2:X2} ", _inBuff[i]);
  3161. cc++;
  3162. }
  3163. if (cc>0) {
  3164. Console.WriteLine();
  3165. }
  3166. cc = 0;
  3167. Console.WriteLine("unused buffer:");
  3168. for (i=_inBytesUsed; i<_inBytesRead; i++) {
  3169. if (cc==16) {
  3170. Console.WriteLine();
  3171. cc = 0;
  3172. }
  3173. Console.Write("{0,-2:X2} ", _inBuff[i]);
  3174. cc++;
  3175. }
  3176. if (cc>0) {
  3177. Console.WriteLine();
  3178. }
  3179. }
  3180. */
  3181. }
  3182. }