-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoConnection.cs
205 lines (156 loc) · 5.95 KB
/
MongoConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using CSMongo.Requests;
using CSMongo.Exceptions;
using CSMongo.Responses;
namespace CSMongo {
/// <summary>
/// Creates a new connection to a Mongo database
/// </summary>
public class MongoConnection : IDisposable {
#region Constructors
/// <summary>
/// Creates a new Mongo connection
/// </summary>
public MongoConnection(string host)
: this (host, Mongo.DefaultPort, true) {
}
/// <summary>
/// Creates a new Mongo connection
/// </summary>
public MongoConnection(string host, bool autoConnect)
: this(host, Mongo.DefaultPort, autoConnect) {
}
/// <summary>
/// Creates a new Mongo connection
/// </summary>
public MongoConnection(string host, int port)
: this(host, port, true) {
}
/// <summary>
/// Creates a new Mongo connection
/// </summary>
public MongoConnection(string host, int port, bool autoConnect) {
this.Host = (host ?? string.Empty).Trim();
this.Port = port;
this.AutoConnect = autoConnect;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the port to connect on
/// </summary>
public int Port { get; set; }
/// <summary>
/// Gets or sets the host to connect to
/// </summary>
public string Host { get; set; }
/// <summary>
/// Gets or sets if this connection should automatically open
/// </summary>
public bool AutoConnect { get; set; }
/// <summary>
/// Returns of this connection is currently open or not
/// </summary>
public bool Connected {
get { return this._Client is TcpClient && this._Client.Connected; }
}
//the current connection to the host
private TcpClient _Client;
private BufferedStream _Buffer;
private BinaryWriter _Writer;
#endregion
#region Events
/// <summary>
/// Event raised just before the database is closed
/// </summary>
public event Action<MongoConnection> BeforeConnectionOpened = (connection) => { };
/// <summary>
/// Event raised when right after the database is closed
/// </summary>
public event Action<MongoConnection> AfterConnectionOpen = (connection) => { };
/// <summary>
/// Event raised just before the database is closed
/// </summary>
public event Action<MongoConnection> BeforeConnectionClosed = (connection) => { };
/// <summary>
/// Event raised when right after the database is closed
/// </summary>
public event Action<MongoConnection> AfterConnectionClosed = (connection) => { };
#endregion
#region Methods
/// <summary>
/// Opens the connection to the database
/// </summary>
public void Open() {
if (this.Connected) { return; }
//notify any event handlers this is opening
if (this.BeforeConnectionOpened != null) { this.BeforeConnectionOpened(this); }
//and then try and open the connection
this._Client = new TcpClient();
this._Client.Connect(this.Host, this.Port);
this._Buffer = new BufferedStream(this._Client.GetStream());
this._Writer = new BinaryWriter(this._Buffer);
//notify this has been connected
if (this.AfterConnectionOpen != null) { this.AfterConnectionOpen(this); }
}
/// <summary>
/// Handles disconnecting from the client
/// </summary>
public void Close() {
//notify any event handlers
if (this.BeforeConnectionClosed != null) { this.BeforeConnectionClosed(this); }
//close up all of the streams
if (this._Buffer is BufferedStream) { this._Buffer.Dispose(); }
if (this._Writer is BinaryWriter) { this._Writer.Close(); }
if (this._Client is TcpClient) { this._Client.Close(); }
//and then finally any event handling
if (this.AfterConnectionClosed != null) { this.AfterConnectionClosed(this); }
}
/// <summary>
/// Sends a request to the server
/// </summary>
public ResponseBase SendRequest(RequestBase request) {
//manage the connection state automatically if needed
if (this.AutoConnect) { this.Open(); }
//attempt to perform the request
try {
//perform normal checking
if (!this.Connected) {
throw new ConnectionNotOpenedException("Connection isn't open yet!");
}
//send the header first
this._Writer.Write(request.GetHeader());
this._Writer.Flush();
//then the rest of the content
this._Writer.Write(request.GetBody());
this._Writer.Flush();
//next, read for the response
return request.OnResponse(this._Buffer);
}
//forward the exception onto the caller
catch (Exception up) {
//attempt to kill the connection
//ignore any problems since we are
//already forwarding an exception
try { this.Dispose(); }
catch { }
//and then forward the error for handling
throw up;
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Handles disconnecting and disposing a connection
/// </summary>
public virtual void Dispose() {
this.Close();
}
#endregion
}
}