blob: 8c2f8e995e85f418570230327f0fe4ab0e72130f [file] [log] [blame]
Christian Weiss8fb719e2018-03-30 21:26:04 +02001/**
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
24using System;
Jens Geyerfeea4782017-01-28 19:53:28 +010025using System.Collections.Generic;
26using System.Linq;
27using System.Net.Sockets;
28using System.Reflection;
29using System.Text;
30#if NET45
31using System.Threading.Tasks;
32#endif
33
34namespace Thrift.Transport
35{
Christian Weiss8fb719e2018-03-30 21:26:04 +020036 /// <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 Arkhipovf1bc6ae2019-09-10 15:06:18 -040044 /// Creates a TcpClient according to the capabilities of the used framework.
Christian Weiss8fb719e2018-03-30 21:26:04 +020045 /// </summary>
46 internal static TcpClient CreateTcpClient()
47 {
48 TcpClient client = null;
Jens Geyerfeea4782017-01-28 19:53:28 +010049
50#if NET45
Christian Weiss8fb719e2018-03-30 21:26:04 +020051 client = new TcpClient(AddressFamily.InterNetworkV6);
52 client.Client.DualMode = true;
Jens Geyerfeea4782017-01-28 19:53:28 +010053#else
54 client = new TcpClient(AddressFamily.InterNetwork);
55#endif
56
Christian Weiss8fb719e2018-03-30 21:26:04 +020057 return client;
58 }
Jens Geyerfeea4782017-01-28 19:53:28 +010059
Christian Weiss8fb719e2018-03-30 21:26:04 +020060 /// <summary>
Vladimir Arkhipovf1bc6ae2019-09-10 15:06:18 -040061 /// Creates a TcpListener according to the capabilities of the used framework.
Christian Weiss8fb719e2018-03-30 21:26:04 +020062 /// </summary>
63 internal static TcpListener CreateTcpListener(Int32 port)
64 {
65 TcpListener listener = null;
Jens Geyerfeea4782017-01-28 19:53:28 +010066
67#if NET45
Christian Weiss8fb719e2018-03-30 21:26:04 +020068 listener = new TcpListener(System.Net.IPAddress.IPv6Any, port);
69 listener.Server.DualMode = true;
Jens Geyerfeea4782017-01-28 19:53:28 +010070#else
71
Christian Weiss8fb719e2018-03-30 21:26:04 +020072 listener = new TcpListener(System.Net.IPAddress.Any, port);
Jens Geyerfeea4782017-01-28 19:53:28 +010073#endif
74
75 return listener;
Christian Weiss8fb719e2018-03-30 21:26:04 +020076 }
77 }
Jens Geyerfeea4782017-01-28 19:53:28 +010078}