Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

169 lines (141 sloc) 4.81 kb
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using MongoDB.Driver.Bson;
using MongoDB.Driver.IO;
namespace MongoDB.Driver
{
public enum ConnectionState{
Closed = 0,
Opened = 1,
}
/// <summary>
/// Description of Connection.
/// </summary>
public class Connection : IDisposable
{
public const string DEFAULTHOST = "localhost";
public const int DEFAULTPORT = 27017;
private bool _disposed;
protected TcpClient tcpclnt;
#if DEBUG
public TcpClient Tcpclnt {
get { return tcpclnt; }
}
#endif
protected String _host;
public string Host {
get { return _host; }
}
protected int _port;
public int Port {
get { return _port; }
}
private ConnectionState state;
public ConnectionState State {
get { return state; }
}
public Connection():this(DEFAULTHOST,DEFAULTPORT){
}
public Connection(String host):this(host,DEFAULTPORT){
}
public Connection(String host, int port){
this._host = host;
this._port = port;
this.state = ConnectionState.Closed;
_disposed = false;
}
~Connection()
{
this.Dispose(false);
}
/// <summary>
/// Used for sending a message that gets a reply such as a query.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
/// <exception cref="IOException">A reconnect will be issued but it is up to the caller to handle the error.</exception>
public ReplyMessage SendTwoWayMessage(RequestMessage msg){
if (this.State != ConnectionState.Opened){
throw new MongoCommException("Operation cannot be performed on a closed connection.", this);
}
try{
msg.Write(tcpclnt.GetStream());
ReplyMessage reply = new ReplyMessage();
reply.Read(tcpclnt.GetStream());
return reply;
}catch(IOException){
this.Reconnect();
throw;
}
}
/// <summary>
/// Used for sending a message that gets no reply such as insert or update.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
/// <exception cref="IOException">A reconnect will be issued but it is up to the caller to handle the error.</exception>
public void SendMessage(RequestMessage msg){
if (this.State != ConnectionState.Opened){
throw new MongoCommException("Operation cannot be performed on a closed connection.", this);
}
try{
msg.Write(tcpclnt.GetStream());
}catch(IOException){//Sending doesn't seem to always trigger the detection of a closed socket.
this.Reconnect();
throw;
}
}
/// <summary>
/// Just sends a simple message string to the database.
/// </summary>
/// <param name="message">
/// A <see cref="System.String"/>
/// </param>
public void SendMsgMessage(String message){
MsgMessage msg = new MsgMessage();
msg.Message = message;
this.SendMessage(msg);
}
public void Reconnect(){
Debug.WriteLine("Reconnecting", "Connection");
this.Open();
}
public virtual void Open(){
if (this.State != ConnectionState.Opened)
{
tcpclnt = new TcpClient();
tcpclnt.Connect(this.Host, this.Port);
this.state = ConnectionState.Opened;
}
}
public void Close(){
if (this.State != ConnectionState.Closed)
{
tcpclnt.Close();
this.state = ConnectionState.Closed;
}
}
#region IDisposable Membri di
protected void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
this.Close();
}
_disposed = true;
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
Jump to Line
Something went wrong with that request. Please try again.