DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TcpClientConnector.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using System;
17 using System.Collections.Generic;
18 using System.IO;
19 using System.Net;
20 using System.Net.Sockets;
21 using System.Text;
22 
23 namespace Deveel.Data.Protocol {
25  public const int DefaultPort = 6669;
26 
27  public override ConnectionEndPoint LocalEndPoint {
28  get { return new ConnectionEndPoint(KnownConnectionProtocols.TcpIp, Dns.GetHostName()); }
29  }
30 
31  public override ConnectionEndPoint MakeEndPoint(IDictionary<string, object> properties) {
32  object hostObj, portObj;
33  if (!properties.TryGetValue("Host", out hostObj))
34  throw new InvalidOperationException("Could not find the host address.");
35 
36  var host = (string) hostObj;
37  var port = DefaultPort;
38 
39  if (properties.TryGetValue("Port", out portObj))
40  port = (int) portObj;
41 
42  var address = new StringBuilder(host);
43  if (port > 0 && port != DefaultPort)
44  address.AppendFormat(":{0}", port);
45 
46  return new ConnectionEndPoint(KnownConnectionProtocols.TcpIp, address.ToString());
47  }
48 
49  private EndPoint ParseEndPoint(string s) {
50  string address = s;
51  int port = DefaultPort;
52 
53  var sepIndex = address.IndexOf(':');
54  if (sepIndex != -1) {
55  string sPort = address.Substring(sepIndex + 1);
56  if (!Int32.TryParse(sPort, out port))
57  throw new FormatException();
58 
59  address = address.Substring(0, sepIndex);
60  }
61 
62  return new IPEndPoint(IPAddress.Parse(address), port);
63  }
64 
65  protected override NetworkStream CreateNetworkStream(ConnectionEndPoint remoteEndPoint, FileAccess access) {
66  if (remoteEndPoint == null)
67  throw new ArgumentNullException("remoteEndPoint");
68  if (remoteEndPoint.Protocol != KnownConnectionProtocols.TcpIp)
69  throw new ArgumentException();
70 
71  var endPoint = ParseEndPoint(remoteEndPoint.Address);
72 
73  var sockect = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
74  sockect.SendTimeout = Timeout;
75  sockect.ReceiveTimeout = Timeout;
76  sockect.Connect(endPoint);
77 
78  return new NetworkStream(sockect, access, true);
79  }
80  }
81 }
override NetworkStream CreateNetworkStream(ConnectionEndPoint remoteEndPoint, FileAccess access)
override ConnectionEndPoint MakeEndPoint(IDictionary< string, object > properties)