Jens Geyer | 018c1b8 | 2015-04-17 20:55:05 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | using System; |
| 21 | using System.Collections.Generic; |
| 22 | using System.Linq; |
| 23 | using System.Net.Sockets; |
| 24 | using System.Reflection; |
| 25 | using System.Text; |
| 26 | |
| 27 | namespace Thrift.Transport |
| 28 | { |
| 29 | /** |
| 30 | * PropertyInfo for the DualMode property of the System.Net.Sockets.Socket class. Used to determine if the sockets are capable of |
| 31 | * automatic IPv4 and IPv6 handling. If DualMode is present the sockets automatically handle IPv4 and IPv6 connections. |
| 32 | * If the DualMode is not available the system configuration determines whether IPv4 or IPv6 is used. |
| 33 | */ |
| 34 | internal static class TSocketVersionizer |
| 35 | { |
| 36 | /* |
| 37 | * PropertyInfo for the DualMode property of System.Net.Sockets.Socket. |
| 38 | */ |
| 39 | private static PropertyInfo DualModeProperty = typeof(Socket).GetProperty("DualMode"); |
| 40 | |
| 41 | /* |
| 42 | * Indicates whether the used framework supports DualMode on sockets or not. |
| 43 | */ |
| 44 | internal static Boolean SupportsDualMode |
| 45 | { |
| 46 | get |
| 47 | { |
| 48 | return TSocketVersionizer.DualModeProperty != null; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Creates a TcpClient according to the capabilitites of the used framework |
| 54 | */ |
| 55 | internal static TcpClient CreateTcpClient() |
| 56 | { |
| 57 | TcpClient client = null; |
| 58 | |
| 59 | if (TSocketVersionizer.SupportsDualMode) |
| 60 | { |
| 61 | client = new TcpClient(AddressFamily.InterNetworkV6); |
| 62 | TSocketVersionizer.DualModeProperty.SetValue(client.Client, true); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | client = new TcpClient(AddressFamily.InterNetwork); |
| 67 | } |
| 68 | |
| 69 | return client; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Creates a TcpListener according to the capabilitites of the used framework |
| 74 | */ |
| 75 | internal static TcpListener CreateTcpListener(Int32 port) |
| 76 | { |
| 77 | TcpListener listener = null; |
| 78 | |
| 79 | if (TSocketVersionizer.SupportsDualMode) |
| 80 | { |
| 81 | listener = new TcpListener(System.Net.IPAddress.IPv6Any, port); |
| 82 | TSocketVersionizer.DualModeProperty.SetValue(listener.Server, true); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | listener = new TcpListener(System.Net.IPAddress.Any, port); |
| 87 | } |
| 88 | |
| 89 | return listener; |
| 90 | } |
| 91 | } |
| 92 | } |