19
19
using System . Net . Sockets ;
20
20
using System . Security . Authentication ;
21
21
using System . Security . Cryptography . X509Certificates ;
22
- using MongoDB . Bson ;
23
22
using MongoDB . Bson . IO ;
24
23
using MongoDB . Bson . Serialization ;
25
- using MongoDB . Bson . Serialization . Serializers ;
26
24
using MongoDB . Driver . Communication . Security ;
27
25
28
26
namespace MongoDB . Driver . Internal
@@ -242,63 +240,6 @@ internal void Open()
242
240
. Authenticate ( ) ;
243
241
}
244
242
245
- // this is a low level method that doesn't require a MongoServer
246
- // so it can be used while connecting to a MongoServer
247
- internal TCommandResult RunCommandAs < TCommandResult > (
248
- string databaseName ,
249
- QueryFlags queryFlags ,
250
- CommandDocument command ,
251
- bool throwOnError ) where TCommandResult : CommandResult
252
- {
253
- var commandResultSerializer = BsonSerializer . LookupSerializer ( typeof ( TCommandResult ) ) ;
254
- IBsonSerializationOptions commandResultSerializationOptions = null ;
255
- return RunCommandAs < TCommandResult > ( databaseName , queryFlags , command , commandResultSerializer , commandResultSerializationOptions , throwOnError ) ;
256
- }
257
-
258
- internal TCommandResult RunCommandAs < TCommandResult > (
259
- string databaseName ,
260
- QueryFlags queryFlags ,
261
- CommandDocument command ,
262
- IBsonSerializer commandResultSerializer ,
263
- IBsonSerializationOptions commandResultSerializationOptions ,
264
- bool throwOnError ) where TCommandResult : CommandResult
265
- {
266
- var commandName = command . GetElement ( 0 ) . Name ;
267
-
268
- var writerSettings = new BsonBinaryWriterSettings
269
- {
270
- Encoding = _serverInstance . Settings . WriteEncoding ?? MongoDefaults . WriteEncoding ,
271
- GuidRepresentation = GuidRepresentation . Unspecified ,
272
- MaxDocumentSize = _serverInstance . MaxDocumentSize
273
- } ;
274
-
275
- var queryMessage = new MongoQueryMessage ( writerSettings , databaseName + ".$cmd" , queryFlags , 0 , 1 , command , null ) ;
276
- SendMessage ( queryMessage , null , databaseName ) ; // write concern doesn't apply to queries
277
-
278
- var readerSettings = new BsonBinaryReaderSettings
279
- {
280
- Encoding = _serverInstance . Settings . ReadEncoding ?? MongoDefaults . ReadEncoding ,
281
- GuidRepresentation = GuidRepresentation . Unspecified ,
282
- MaxDocumentSize = _serverInstance . MaxDocumentSize
283
- } ;
284
- var reply = ReceiveMessage < TCommandResult > ( readerSettings , commandResultSerializer , commandResultSerializationOptions ) ;
285
- if ( reply . NumberReturned == 0 )
286
- {
287
- var message = string . Format ( "Command '{0}' failed. No response returned." , commandName ) ;
288
- throw new MongoCommandException ( message ) ;
289
- }
290
-
291
- var commandResult = reply . Documents [ 0 ] ;
292
- commandResult . Command = command ;
293
-
294
- if ( throwOnError && ! commandResult . Ok )
295
- {
296
- throw new MongoCommandException ( commandResult ) ;
297
- }
298
-
299
- return commandResult ;
300
- }
301
-
302
243
internal MongoReplyMessage < TDocument > ReceiveMessage < TDocument > (
303
244
BsonBinaryReaderSettings readerSettings ,
304
245
IBsonSerializer serializer ,
@@ -334,35 +275,13 @@ internal MongoReplyMessage<TDocument> ReceiveMessage<TDocument>(
334
275
}
335
276
}
336
277
337
- internal WriteConcernResult SendMessage ( BsonBuffer buffer , MongoRequestMessage message , WriteConcern writeConcern , string databaseName )
278
+ internal void SendMessage ( BsonBuffer buffer , int requestId )
338
279
{
339
280
if ( _state == MongoConnectionState . Closed ) { throw new InvalidOperationException ( "Connection is closed." ) ; }
340
281
lock ( _connectionLock )
341
282
{
342
283
_lastUsedAt = DateTime . UtcNow ;
343
- _requestId = message . RequestId ;
344
-
345
- CommandDocument getLastErrorCommand = null ;
346
- if ( writeConcern != null && writeConcern . Enabled )
347
- {
348
- var fsync = ( writeConcern . FSync == null ) ? null : ( BsonValue ) writeConcern . FSync ;
349
- var journal = ( writeConcern . Journal == null ) ? null : ( BsonValue ) writeConcern . Journal ;
350
- var w = ( writeConcern . W == null ) ? null : writeConcern . W . ToGetLastErrorWValue ( ) ;
351
- var wTimeout = ( writeConcern . WTimeout == null ) ? null : ( BsonValue ) ( int ) writeConcern . WTimeout . Value . TotalMilliseconds ;
352
-
353
- getLastErrorCommand = new CommandDocument
354
- {
355
- { "getlasterror" , 1 } , // use all lowercase for backward compatibility
356
- { "fsync" , fsync , fsync != null } ,
357
- { "j" , journal , journal != null } ,
358
- { "w" , w , w != null } ,
359
- { "wtimeout" , wTimeout , wTimeout != null }
360
- } ;
361
-
362
- // piggy back on network transmission for message
363
- var getLastErrorMessage = new MongoQueryMessage ( message . WriterSettings , databaseName + ".$cmd" , QueryFlags . None , 0 , 1 , getLastErrorCommand , null ) ;
364
- getLastErrorMessage . WriteToBuffer ( buffer ) ;
365
- }
284
+ _requestId = requestId ;
366
285
367
286
try
368
287
{
@@ -380,46 +299,15 @@ internal WriteConcernResult SendMessage(BsonBuffer buffer, MongoRequestMessage m
380
299
HandleException ( ex ) ;
381
300
throw ;
382
301
}
383
-
384
- WriteConcernResult writeConcernResult = null ;
385
- if ( writeConcern != null && writeConcern . Enabled )
386
- {
387
- var readerSettings = new BsonBinaryReaderSettings
388
- {
389
- Encoding = _serverInstance . Settings . ReadEncoding ?? MongoDefaults . ReadEncoding ,
390
- GuidRepresentation = message . WriterSettings . GuidRepresentation ,
391
- MaxDocumentSize = _serverInstance . MaxDocumentSize
392
- } ;
393
- var writeConcernResultSerializer = BsonSerializer . LookupSerializer ( typeof ( WriteConcernResult ) ) ;
394
- var replyMessage = ReceiveMessage < WriteConcernResult > ( readerSettings , writeConcernResultSerializer , null ) ;
395
- writeConcernResult = replyMessage . Documents [ 0 ] ;
396
- writeConcernResult . Command = getLastErrorCommand ;
397
- if ( ! writeConcernResult . Ok )
398
- {
399
- var errorMessage = string . Format (
400
- "WriteConcern detected an error '{0}'. (response was {1})." ,
401
- writeConcernResult . ErrorMessage , writeConcernResult . Response . ToJson ( ) ) ;
402
- throw new WriteConcernException ( errorMessage , writeConcernResult ) ;
403
- }
404
- if ( writeConcernResult . HasLastErrorMessage )
405
- {
406
- var errorMessage = string . Format (
407
- "WriteConcern detected an error '{0}'. (Response was {1})." ,
408
- writeConcernResult . LastErrorMessage , writeConcernResult . Response . ToJson ( ) ) ;
409
- throw new WriteConcernException ( errorMessage , writeConcernResult ) ;
410
- }
411
- }
412
-
413
- return writeConcernResult ;
414
302
}
415
303
}
416
304
417
- internal WriteConcernResult SendMessage ( MongoRequestMessage message , WriteConcern writeConcern , string databaseName )
305
+ internal void SendMessage ( MongoRequestMessage message )
418
306
{
419
307
using ( var buffer = new BsonBuffer ( new MultiChunkBuffer ( BsonChunkPool . Default ) , true ) )
420
308
{
421
309
message . WriteToBuffer ( buffer ) ;
422
- return SendMessage ( buffer , message , writeConcern , databaseName ) ;
310
+ SendMessage ( buffer , message . RequestId ) ;
423
311
}
424
312
}
425
313
0 commit comments