Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +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 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
| 22 | */ |
| 23 | |
| 24 | using System; |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 25 | using System.Collections.Generic; |
| 26 | using System.Linq; |
| 27 | using System.Net.Sockets; |
| 28 | using System.Reflection; |
| 29 | using System.Text; |
| 30 | #if NET45 |
| 31 | using System.Threading.Tasks; |
| 32 | #endif |
| 33 | |
| 34 | namespace Thrift.Transport |
| 35 | { |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 36 | /// <summary> |
| 37 | /// PropertyInfo for the DualMode property of the System.Net.Sockets.Socket class. Used to determine if the sockets are capable of |
| 38 | /// automatic IPv4 and IPv6 handling. If DualMode is present the sockets automatically handle IPv4 and IPv6 connections. |
| 39 | /// If the DualMode is not available the system configuration determines whether IPv4 or IPv6 is used. |
| 40 | /// </summary> |
| 41 | internal static class TSocketVersionizer |
| 42 | { |
| 43 | /// <summary> |
Vladimir Arkhipov | f1bc6ae | 2019-09-10 15:06:18 -0400 | [diff] [blame^] | 44 | /// Creates a TcpClient according to the capabilities of the used framework. |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 45 | /// </summary> |
| 46 | internal static TcpClient CreateTcpClient() |
| 47 | { |
| 48 | TcpClient client = null; |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 49 | |
| 50 | #if NET45 |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 51 | client = new TcpClient(AddressFamily.InterNetworkV6); |
| 52 | client.Client.DualMode = true; |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 53 | #else |
| 54 | client = new TcpClient(AddressFamily.InterNetwork); |
| 55 | #endif |
| 56 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 57 | return client; |
| 58 | } |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 59 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 60 | /// <summary> |
Vladimir Arkhipov | f1bc6ae | 2019-09-10 15:06:18 -0400 | [diff] [blame^] | 61 | /// Creates a TcpListener according to the capabilities of the used framework. |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 62 | /// </summary> |
| 63 | internal static TcpListener CreateTcpListener(Int32 port) |
| 64 | { |
| 65 | TcpListener listener = null; |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 66 | |
| 67 | #if NET45 |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 68 | listener = new TcpListener(System.Net.IPAddress.IPv6Any, port); |
| 69 | listener.Server.DualMode = true; |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 70 | #else |
| 71 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 72 | listener = new TcpListener(System.Net.IPAddress.Any, port); |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | return listener; |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame] | 76 | } |
| 77 | } |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 78 | } |