Merge pull request #1668 from alexanderkyte/bug1856
[mono.git] / mcs / class / System / ReferenceSources / Socket.cs
1 //
2 // Socket.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.IO;
29
30 namespace System.Net.Sockets
31 {
32         partial class Socket
33         {
34                 internal const int DefaultCloseTimeout = -1;
35                 // don't change for default, otherwise breaking change
36
37                 // this version does not throw.
38                 internal void InternalShutdown (SocketShutdown how)
39                 {
40                         if (!is_connected || is_disposed)
41                                 return;
42                         int error;
43                         Shutdown_internal (safe_handle, how, out error);
44                 }
45
46                 internal IAsyncResult UnsafeBeginConnect (EndPoint remoteEP, AsyncCallback callback, object state)
47                 {
48                         return BeginConnect (remoteEP, callback, state);
49                 }
50
51                 internal IAsyncResult UnsafeBeginSend (byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state)
52                 {
53                         return BeginSend (buffer, offset, size, socketFlags, callback, state);
54                 }
55
56                 internal IAsyncResult UnsafeBeginReceive (byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state)
57                 {
58                         return BeginReceive (buffer, offset, size, socketFlags, callback, state);
59                 }
60
61                 internal IAsyncResult BeginMultipleSend (BufferOffsetSize[] buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
62                 {
63                         var segments = new ArraySegment<byte> [buffers.Length];
64                         for (int i = 0; i < buffers.Length; i++)
65                                 segments [i] = new ArraySegment<byte> (buffers [i].Buffer, buffers [i].Offset, buffers [i].Size);
66                         return BeginSend (segments, socketFlags, callback, state);
67                 }
68
69                 internal IAsyncResult UnsafeBeginMultipleSend (BufferOffsetSize[] buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
70                 {
71                         return BeginMultipleSend (buffers, socketFlags, callback, state);
72                 }
73
74                 internal int EndMultipleSend (IAsyncResult asyncResult)
75                 {
76                         return EndSend (asyncResult);
77                 }
78
79                 internal void MultipleSend (BufferOffsetSize[] buffers, SocketFlags socketFlags)
80                 {
81                         var segments = new ArraySegment<byte> [buffers.Length];
82                         for (int i = 0; i < buffers.Length; i++)
83                                 segments [i] = new ArraySegment<byte> (buffers [i].Buffer, buffers [i].Offset, buffers [i].Size);
84                         Send (segments, socketFlags);
85                 }
86
87                 internal void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue, bool silent)
88                 {
89                         if (is_disposed && is_closed) {
90                                 if (silent)
91                                         return;
92                                 throw new ObjectDisposedException (GetType ().ToString ());
93                         }
94
95                         int error;
96
97                         SetSocketOption_internal (safe_handle, optionLevel, optionName, null,
98                                 null, optionValue, out error);
99
100                         if (!silent && error != 0)
101                                 throw new SocketException (error);
102                 }
103         }
104 }