Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SocketPal.cs
Go to the documentation of this file.
9
10namespace System.Net.Sockets;
11
12internal static class SocketPal
13{
15
16 private static void MicrosecondsToTimeValue(long microseconds, ref global::Interop.Winsock.TimeValue socketTime)
17 {
18 socketTime.Seconds = (int)(microseconds / 1000000);
19 socketTime.Microseconds = (int)(microseconds % 1000000);
20 }
21
23 {
25 }
26
28 {
29 global::Interop.Winsock.EnsureInitialized();
30 IntPtr preexistingHandle = global::Interop.Winsock.WSASocketW(addressFamily, socketType, protocolType, IntPtr.Zero, 0u, global::Interop.Winsock.SocketConstructorFlags.WSA_FLAG_OVERLAPPED | global::Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
32 if (socket.IsInvalid)
33 {
35 if (System.Net.NetEventSource.Log.IsEnabled())
36 {
37 System.Net.NetEventSource.Error(null, $"WSASocketW failed with error {lastSocketError}", "CreateSocket");
38 }
39 socket.Dispose();
40 return lastSocketError;
41 }
42 if (System.Net.NetEventSource.Log.IsEnabled())
43 {
44 System.Net.NetEventSource.Info(null, socket, "CreateSocket");
45 }
46 return SocketError.Success;
47 }
48
50 {
51 if (socketInformation.ProtocolInformation == null || socketInformation.ProtocolInformation.Length < sizeof(global::Interop.Winsock.WSAPROTOCOL_INFOW))
52 {
54 }
55 global::Interop.Winsock.EnsureInitialized();
56 fixed (byte* ptr = socketInformation.ProtocolInformation)
57 {
58 IntPtr preexistingHandle = global::Interop.Winsock.WSASocketW(AddressFamily.Unknown, SocketType.Unknown, ProtocolType.Unknown, (IntPtr)ptr, 0u, global::Interop.Winsock.SocketConstructorFlags.WSA_FLAG_OVERLAPPED | global::Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
60 if (socket.IsInvalid)
61 {
63 if (System.Net.NetEventSource.Log.IsEnabled())
64 {
65 System.Net.NetEventSource.Error(null, $"WSASocketW failed with error {lastSocketError}", "CreateSocket");
66 }
67 socket.Dispose();
68 return lastSocketError;
69 }
70 if (!global::Interop.Kernel32.SetHandleInformation(socket, global::Interop.Kernel32.HandleFlags.HANDLE_FLAG_INHERIT, global::Interop.Kernel32.HandleFlags.None))
71 {
73 if (System.Net.NetEventSource.Log.IsEnabled())
74 {
75 System.Net.NetEventSource.Error(null, $"SetHandleInformation failed with error {lastSocketError2}", "CreateSocket");
76 }
77 socket.Dispose();
78 return lastSocketError2;
79 }
80 if (System.Net.NetEventSource.Log.IsEnabled())
81 {
82 System.Net.NetEventSource.Info(null, socket, "CreateSocket");
83 }
84 global::Interop.Winsock.WSAPROTOCOL_INFOW* ptr2 = (global::Interop.Winsock.WSAPROTOCOL_INFOW*)ptr;
85 addressFamily = ptr2->iAddressFamily;
86 socketType = ptr2->iSocketType;
87 protocolType = ptr2->iProtocol;
88 return SocketError.Success;
89 }
90 }
91
93 {
94 int argp = ((!shouldBlock) ? (-1) : 0);
95 SocketError socketError = global::Interop.Winsock.ioctlsocket(handle, -2147195266, ref argp);
96 if (socketError == SocketError.SocketError)
97 {
99 }
100 willBlock = argp == 0;
101 return socketError;
102 }
103
104 public unsafe static SocketError GetSockName(SafeSocketHandle handle, byte* buffer, int* nameLen)
105 {
106 SocketError socketError = global::Interop.Winsock.getsockname(handle, buffer, nameLen);
107 if (socketError != SocketError.SocketError)
108 {
109 return SocketError.Success;
110 }
111 return GetLastSocketError();
112 }
113
115 {
116 int argp = 0;
117 SocketError socketError = global::Interop.Winsock.ioctlsocket(handle, 1074030207, ref argp);
118 available = argp;
119 if (socketError != SocketError.SocketError)
120 {
121 return SocketError.Success;
122 }
123 return GetLastSocketError();
124 }
125
127 {
128 fixed (byte* socketAddress = buffer)
129 {
130 SocketError socketError = global::Interop.Winsock.getpeername(handle, socketAddress, ref nameLen);
131 if (socketError != SocketError.SocketError)
132 {
133 return SocketError.Success;
134 }
135 return GetLastSocketError();
136 }
137 }
138
140 {
141 SocketError socketError = global::Interop.Winsock.bind(handle, buffer, nameLen);
142 if (socketError != SocketError.SocketError)
143 {
144 return SocketError.Success;
145 }
146 return GetLastSocketError();
147 }
148
150 {
151 SocketError socketError = global::Interop.Winsock.listen(handle, backlog);
152 if (socketError != SocketError.SocketError)
153 {
154 return SocketError.Success;
155 }
156 return GetLastSocketError();
157 }
158
160 {
161 IntPtr preexistingHandle = global::Interop.Winsock.accept(listenSocket, socketAddress, ref socketAddressSize);
163 if (System.Net.NetEventSource.Log.IsEnabled())
164 {
165 System.Net.NetEventSource.Info(null, socket, "Accept");
166 }
167 if (!socket.IsInvalid)
168 {
169 return SocketError.Success;
170 }
171 return GetLastSocketError();
172 }
173
175 {
176 SocketError socketError = global::Interop.Winsock.WSAConnect(handle, peerAddress, peerAddressLen, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
177 if (socketError != SocketError.SocketError)
178 {
179 return SocketError.Success;
180 }
181 return GetLastSocketError();
182 }
183
185 {
186 int count = buffers.Count;
187 bool flag = count <= 16;
188 WSABuffer[] array = null;
189 GCHandle[] array2 = null;
192 if (flag)
193 {
196 }
197 else
198 {
199 span = (array = ArrayPool<WSABuffer>.Shared.Rent(count));
200 span2 = (array2 = ArrayPool<GCHandle>.Shared.Rent(count));
201 }
202 span2 = span2.Slice(0, count);
203 span2.Clear();
204 try
205 {
206 for (int i = 0; i < count; i++)
207 {
210 span2[i] = GCHandle.Alloc(segment.Array, GCHandleType.Pinned);
211 span[i].Length = segment.Count;
212 span[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(segment.Array, segment.Offset);
213 }
214 SocketError socketError = global::Interop.Winsock.WSASend(handle, span, count, out bytesTransferred, socketFlags, null, IntPtr.Zero);
215 if (socketError == SocketError.SocketError)
216 {
218 }
219 return socketError;
220 }
221 finally
222 {
223 for (int j = 0; j < count; j++)
224 {
225 if (span2[j].IsAllocated)
226 {
227 span2[j].Free();
228 }
229 }
230 if (!flag)
231 {
232 ArrayPool<WSABuffer>.Shared.Return(array);
233 ArrayPool<GCHandle>.Shared.Return(array2);
234 }
235 }
236 }
237
242
244 {
245 int num;
246 fixed (byte* pinnedBuffer = &MemoryMarshal.GetReference(buffer))
247 {
248 num = global::Interop.Winsock.send(handle, pinnedBuffer, buffer.Length, socketFlags);
249 }
250 if (num == -1)
251 {
253 return GetLastSocketError();
254 }
255 bytesTransferred = num;
256 return SocketError.Success;
257 }
258
260 {
261 fixed (byte* ptr = preBuffer)
262 {
263 fixed (byte* ptr2 = postBuffer)
264 {
265 if (!TransmitFileHelper(handle, fileHandle, null, (IntPtr)ptr, preBuffer.Length, (IntPtr)ptr2, postBuffer.Length, flags))
266 {
267 return GetLastSocketError();
268 }
269 return SocketError.Success;
270 }
271 }
272 }
273
278
280 {
281 int num;
282 fixed (byte* pinnedBuffer = &MemoryMarshal.GetReference(buffer))
283 {
284 num = global::Interop.Winsock.sendto(handle, pinnedBuffer, buffer.Length, socketFlags, peerAddress, peerAddressSize);
285 }
286 if (num == -1)
287 {
289 return GetLastSocketError();
290 }
291 bytesTransferred = num;
292 return SocketError.Success;
293 }
294
296 {
297 int count = buffers.Count;
298 bool flag = count <= 16;
299 WSABuffer[] array = null;
300 GCHandle[] array2 = null;
303 if (flag)
304 {
307 }
308 else
309 {
310 span = (array = ArrayPool<WSABuffer>.Shared.Rent(count));
311 span2 = (array2 = ArrayPool<GCHandle>.Shared.Rent(count));
312 }
313 span2 = span2.Slice(0, count);
314 span2.Clear();
315 try
316 {
317 for (int i = 0; i < count; i++)
318 {
321 span2[i] = GCHandle.Alloc(segment.Array, GCHandleType.Pinned);
322 span[i].Length = segment.Count;
323 span[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(segment.Array, segment.Offset);
324 }
325 SocketError socketError = global::Interop.Winsock.WSARecv(handle, span, count, out bytesTransferred, ref socketFlags, null, IntPtr.Zero);
326 if (socketError == SocketError.SocketError)
327 {
329 }
330 return socketError;
331 }
332 finally
333 {
334 for (int j = 0; j < count; j++)
335 {
336 if (span2[j].IsAllocated)
337 {
338 span2[j].Free();
339 }
340 }
341 if (!flag)
342 {
343 ArrayPool<WSABuffer>.Shared.Return(array);
344 ArrayPool<GCHandle>.Shared.Return(array2);
345 }
346 }
347 }
348
353
355 {
356 int num;
357 fixed (byte* pinnedBuffer = &MemoryMarshal.GetReference(buffer))
358 {
359 num = global::Interop.Winsock.recv(handle, pinnedBuffer, buffer.Length, socketFlags);
360 }
361 if (num == -1)
362 {
364 return GetLastSocketError();
365 }
366 bytesTransferred = num;
367 return SocketError.Success;
368 }
369
370 public unsafe static IPPacketInformation GetIPPacketInformation(global::Interop.Winsock.ControlData* controlBuffer)
371 {
372 IPAddress address = ((controlBuffer->length == UIntPtr.Zero) ? IPAddress.None : new IPAddress(controlBuffer->address));
373 return new IPPacketInformation(address, (int)controlBuffer->index);
374 }
375
376 public unsafe static IPPacketInformation GetIPPacketInformation(global::Interop.Winsock.ControlDataIPv6* controlBuffer)
377 {
378 if (controlBuffer->length == (UIntPtr)(ulong)sizeof(global::Interop.Winsock.ControlData))
379 {
380 return GetIPPacketInformation((global::Interop.Winsock.ControlData*)controlBuffer);
381 }
382 IPAddress address = ((controlBuffer->length != UIntPtr.Zero) ? new IPAddress(new ReadOnlySpan<byte>(controlBuffer->address, 16)) : IPAddress.IPv6None);
383 return new IPPacketInformation(address, (int)controlBuffer->index);
384 }
385
390
392 {
393 Socket.GetIPProtocolInformation(socket.AddressFamily, socketAddress, out var isIPv, out var isIPv2);
395 receiveAddress = socketAddress;
397 fixed (byte* ptr2 = &MemoryMarshal.GetReference(buffer))
398 {
399 fixed (byte* ptr = socketAddress.Buffer)
400 {
401 Unsafe.SkipInit(out global::Interop.Winsock.WSAMsg wSAMsg);
402 wSAMsg.socketAddress = (IntPtr)ptr;
403 wSAMsg.addressLength = (uint)socketAddress.Size;
404 wSAMsg.flags = socketFlags;
405 Unsafe.SkipInit(out WSABuffer wSABuffer);
406 wSABuffer.Length = buffer.Length;
407 wSABuffer.Pointer = (IntPtr)ptr2;
408 wSAMsg.buffers = (IntPtr)(&wSABuffer);
409 wSAMsg.count = 1u;
410 if (isIPv)
411 {
412 Unsafe.SkipInit(out global::Interop.Winsock.ControlData controlData);
413 wSAMsg.controlBuffer.Pointer = (IntPtr)(&controlData);
414 wSAMsg.controlBuffer.Length = sizeof(global::Interop.Winsock.ControlData);
415 if (socket.WSARecvMsgBlocking(handle, (IntPtr)(&wSAMsg), out bytesTransferred) == SocketError.SocketError)
416 {
417 return GetLastSocketError();
418 }
420 }
421 else if (isIPv2)
422 {
423 Unsafe.SkipInit(out global::Interop.Winsock.ControlDataIPv6 controlDataIPv);
424 wSAMsg.controlBuffer.Pointer = (IntPtr)(&controlDataIPv);
425 wSAMsg.controlBuffer.Length = sizeof(global::Interop.Winsock.ControlDataIPv6);
426 if (socket.WSARecvMsgBlocking(handle, (IntPtr)(&wSAMsg), out bytesTransferred) == SocketError.SocketError)
427 {
428 return GetLastSocketError();
429 }
431 }
432 else
433 {
434 wSAMsg.controlBuffer.Pointer = IntPtr.Zero;
435 wSAMsg.controlBuffer.Length = 0;
436 if (socket.WSARecvMsgBlocking(handle, (IntPtr)(&wSAMsg), out bytesTransferred) == SocketError.SocketError)
437 {
438 return GetLastSocketError();
439 }
440 }
441 socketFlags = wSAMsg.flags;
442 }
443 }
444 return SocketError.Success;
445 }
446
447 public static SocketError ReceiveFrom(SafeSocketHandle handle, byte[] buffer, int offset, int size, SocketFlags socketFlags, byte[] socketAddress, ref int addressLength, out int bytesTransferred)
448 {
449 return ReceiveFrom(handle, buffer.AsSpan(offset, size), SocketFlags.None, socketAddress, ref addressLength, out bytesTransferred);
450 }
451
452 public unsafe static SocketError ReceiveFrom(SafeSocketHandle handle, Span<byte> buffer, SocketFlags socketFlags, byte[] socketAddress, ref int addressLength, out int bytesTransferred)
453 {
454 int num;
455 fixed (byte* pinnedBuffer = &MemoryMarshal.GetReference(buffer))
456 {
457 num = global::Interop.Winsock.recvfrom(handle, pinnedBuffer, buffer.Length, socketFlags, socketAddress, ref addressLength);
458 }
459 if (num == -1)
460 {
462 return GetLastSocketError();
463 }
464 bytesTransferred = num;
465 return SocketError.Success;
466 }
467
469 {
470 if (ioControlCode == -2147195266)
471 {
473 }
474 SocketError socketError = global::Interop.Winsock.WSAIoctl_Blocking(handle, ioControlCode, optionInValue, (optionInValue != null) ? optionInValue.Length : 0, optionOutValue, (optionOutValue != null) ? optionOutValue.Length : 0, out optionLength, IntPtr.Zero, IntPtr.Zero);
475 if (socketError != SocketError.SocketError)
476 {
477 return SocketError.Success;
478 }
479 return GetLastSocketError();
480 }
481
483 {
484 SocketError socketError = ((optionLevel != SocketOptionLevel.Tcp || (optionName != SocketOptionName.TypeOfService && optionName != SocketOptionName.BlockSource) || !IOControlKeepAlive.IsNeeded) ? global::Interop.Winsock.setsockopt(handle, optionLevel, optionName, ref optionValue, 4) : IOControlKeepAlive.Set(handle, optionName, optionValue));
485 if (socketError != SocketError.SocketError)
486 {
487 return SocketError.Success;
488 }
489 return GetLastSocketError();
490 }
491
493 {
495 {
497 }
499 {
500 SocketError socketError = global::Interop.Winsock.setsockopt(handle, optionLevel, optionName, optionValue2, (optionValue != null) ? optionValue.Length : 0);
501 if (socketError != SocketError.SocketError)
502 {
503 return SocketError.Success;
504 }
505 return GetLastSocketError();
506 }
507 }
508
510 {
512 {
514 if (socketError != SocketError.SocketError)
515 {
516 return SocketError.Success;
517 }
518 return GetLastSocketError();
519 }
520 }
521
523 {
524 socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, optionValue: true);
525 }
526
528 {
529 global::Interop.Winsock.IPMulticastRequest mreq = default(global::Interop.Winsock.IPMulticastRequest);
530 mreq.MulticastAddress = (int)optionValue.Group.Address;
531 if (optionValue.LocalAddress != null)
532 {
533 mreq.InterfaceAddress = (int)optionValue.LocalAddress.Address;
534 }
535 else
536 {
538 mreq.InterfaceAddress = interfaceAddress;
539 }
540 SocketError socketError = global::Interop.Winsock.setsockopt(handle, SocketOptionLevel.IP, optionName, ref mreq, global::Interop.Winsock.IPMulticastRequest.Size);
541 if (socketError != SocketError.SocketError)
542 {
543 return SocketError.Success;
544 }
545 return GetLastSocketError();
546 }
547
549 {
550 global::Interop.Winsock.IPv6MulticastRequest mreq = default(global::Interop.Winsock.IPv6MulticastRequest);
551 mreq.MulticastAddress = optionValue.Group.GetAddressBytes();
552 mreq.InterfaceIndex = (int)optionValue.InterfaceIndex;
553 SocketError socketError = global::Interop.Winsock.setsockopt(handle, SocketOptionLevel.IPv6, optionName, ref mreq, global::Interop.Winsock.IPv6MulticastRequest.Size);
554 if (socketError != SocketError.SocketError)
555 {
556 return SocketError.Success;
557 }
558 return GetLastSocketError();
559 }
560
562 {
563 global::Interop.Winsock.Linger linger = default(global::Interop.Winsock.Linger);
564 linger.OnOff = (ushort)(optionValue.Enabled ? 1 : 0);
565 linger.Time = (ushort)optionValue.LingerTime;
566 SocketError socketError = global::Interop.Winsock.setsockopt(handle, SocketOptionLevel.Socket, SocketOptionName.Linger, ref linger, 4);
567 if (socketError != SocketError.SocketError)
568 {
569 return SocketError.Success;
570 }
571 return GetLastSocketError();
572 }
573
575 {
576 socket.SetSocketOption(optionLevel, SocketOptionName.IPProtectionLevel, protectionLevel);
577 }
578
580 {
582 {
584 return SocketError.Success;
585 }
586 int optionLength = 4;
587 int num = 0;
588 SocketError socketError = global::Interop.Winsock.getsockopt(handle, optionLevel, optionName, (byte*)(&num), ref optionLength);
589 optionValue = num;
590 if (socketError != SocketError.SocketError)
591 {
592 return SocketError.Success;
593 }
594 return GetLastSocketError();
595 }
596
598 {
600 {
602 }
604 {
605 SocketError socketError = global::Interop.Winsock.getsockopt(handle, optionLevel, optionName, optionValue2, ref optionLength);
606 if (socketError != SocketError.SocketError)
607 {
608 return SocketError.Success;
609 }
610 return GetLastSocketError();
611 }
612 }
613
615 {
617 {
619 if (socketError != SocketError.SocketError)
620 {
621 return SocketError.Success;
622 }
623 return GetLastSocketError();
624 }
625 }
626
628 {
629 global::Interop.Winsock.IPMulticastRequest optionValue2 = default(global::Interop.Winsock.IPMulticastRequest);
630 int optionLength = global::Interop.Winsock.IPMulticastRequest.Size;
631 SocketError socketError = global::Interop.Winsock.getsockopt(handle, SocketOptionLevel.IP, optionName, out optionValue2, ref optionLength);
632 if (socketError == SocketError.SocketError)
633 {
634 optionValue = null;
635 return GetLastSocketError();
636 }
637 IPAddress group = new IPAddress(optionValue2.MulticastAddress);
638 IPAddress mcint = new IPAddress(optionValue2.InterfaceAddress);
640 return SocketError.Success;
641 }
642
644 {
645 global::Interop.Winsock.IPv6MulticastRequest optionValue2 = default(global::Interop.Winsock.IPv6MulticastRequest);
646 int optionLength = global::Interop.Winsock.IPv6MulticastRequest.Size;
647 SocketError socketError = global::Interop.Winsock.getsockopt(handle, SocketOptionLevel.IP, optionName, out optionValue2, ref optionLength);
648 if (socketError == SocketError.SocketError)
649 {
650 optionValue = null;
651 return GetLastSocketError();
652 }
653 optionValue = new IPv6MulticastOption(new IPAddress(optionValue2.MulticastAddress), optionValue2.InterfaceIndex);
654 return SocketError.Success;
655 }
656
658 {
659 global::Interop.Winsock.Linger optionValue2 = default(global::Interop.Winsock.Linger);
660 int optionLength = 4;
661 SocketError socketError = global::Interop.Winsock.getsockopt(handle, SocketOptionLevel.Socket, SocketOptionName.Linger, out optionValue2, ref optionLength);
662 if (socketError == SocketError.SocketError)
663 {
664 optionValue = null;
665 return GetLastSocketError();
666 }
667 optionValue = new LingerOption(optionValue2.OnOff != 0, optionValue2.Time);
668 return SocketError.Success;
669 }
670
671 public unsafe static SocketError Poll(SafeSocketHandle handle, int microseconds, SelectMode mode, out bool status)
672 {
673 bool success = false;
674 try
675 {
676 handle.DangerousAddRef(ref success);
677 IntPtr intPtr = handle.DangerousGetHandle();
679 {
680 (IntPtr)1,
681 intPtr
682 };
683 global::Interop.Winsock.TimeValue socketTime = default(global::Interop.Winsock.TimeValue);
684 int num;
685 if (microseconds != -1)
686 {
688 num = global::Interop.Winsock.select(0, (mode == SelectMode.SelectRead) ? ptr : null, (mode == SelectMode.SelectWrite) ? ptr : null, (mode == SelectMode.SelectError) ? ptr : null, ref socketTime);
689 }
690 else
691 {
692 num = global::Interop.Winsock.select(0, (mode == SelectMode.SelectRead) ? ptr : null, (mode == SelectMode.SelectWrite) ? ptr : null, (mode == SelectMode.SelectError) ? ptr : null, IntPtr.Zero);
693 }
694 if (num == -1)
695 {
696 status = false;
697 return GetLastSocketError();
698 }
699 status = (int)(*ptr) != 0 && ptr[1] == intPtr;
700 return SocketError.Success;
701 }
702 finally
703 {
704 if (success)
705 {
706 handle.DangerousRelease();
707 }
708 }
709 }
710
712 {
713 IntPtr[] lease2 = null;
714 IntPtr[] lease3 = null;
715 IntPtr[] lease4 = null;
716 int refsAdded = 0;
717 try
718 {
729 int num;
730 fixed (IntPtr* readfds = &MemoryMarshal.GetReference(span4))
731 {
732 fixed (IntPtr* writefds = &MemoryMarshal.GetReference(span6))
733 {
734 fixed (IntPtr* exceptfds = &MemoryMarshal.GetReference(span8))
735 {
736 if (microseconds != -1)
737 {
738 global::Interop.Winsock.TimeValue socketTime = default(global::Interop.Winsock.TimeValue);
740 num = global::Interop.Winsock.select(0, readfds, writefds, exceptfds, ref socketTime);
741 }
742 else
743 {
744 num = global::Interop.Winsock.select(0, readfds, writefds, exceptfds, IntPtr.Zero);
745 }
746 }
747 }
748 }
749 if (System.Net.NetEventSource.Log.IsEnabled())
750 {
751 System.Net.NetEventSource.Info(null, $"Interop.Winsock.select returns socketCount:{num}", "Select");
752 }
753 if (num == -1)
754 {
755 return GetLastSocketError();
756 }
760 return SocketError.Success;
761 }
762 finally
763 {
764 if (lease2 != null)
765 {
766 ArrayPool<IntPtr>.Shared.Return(lease2);
767 }
768 if (lease3 != null)
769 {
770 ArrayPool<IntPtr>.Shared.Return(lease3);
771 }
772 if (lease4 != null)
773 {
774 ArrayPool<IntPtr>.Shared.Return(lease4);
775 }
779 }
781 {
782 int count;
783 if (list == null || (count = list.Count) == 0)
784 {
785 span = default(Span<IntPtr>);
786 return false;
787 }
788 if (count >= 64)
789 {
790 span = (lease = ArrayPool<IntPtr>.Shared.Rent(count + 1));
791 return false;
792 }
793 span = default(Span<IntPtr>);
794 return true;
795 }
796 }
797
799 {
800 SocketError socketError = global::Interop.Winsock.shutdown(handle, (int)how);
801 if (socketError != SocketError.SocketError)
802 {
803 handle.TrackShutdown(how);
804 return SocketError.Success;
805 }
806 return GetLastSocketError();
807 }
808
810 {
811 bool flag = false;
812 global::Interop.Mswsock.TransmitFileBuffers transmitFileBuffers = default(global::Interop.Mswsock.TransmitFileBuffers);
813 if (preBufferLength > 0)
814 {
815 flag = true;
816 transmitFileBuffers.Head = pinnedPreBuffer;
817 transmitFileBuffers.HeadLength = preBufferLength;
818 }
819 if (postBufferLength > 0)
820 {
821 flag = true;
822 transmitFileBuffers.Tail = pinnedPostBuffer;
823 transmitFileBuffers.TailLength = postBufferLength;
824 }
825 bool success = false;
827 try
828 {
829 if (fileHandle != null)
830 {
831 fileHandle.DangerousAddRef(ref success);
832 fileHandle2 = fileHandle.DangerousGetHandle();
833 }
834 return global::Interop.Mswsock.TransmitFile(socket, fileHandle2, 0, 0, overlapped, flag ? (&transmitFileBuffers) : null, flags);
835 }
836 finally
837 {
838 if (success)
839 {
840 fileHandle.DangerousRelease();
841 }
842 }
843 }
844
846 {
847 }
848
850 {
851 SocketError result = SocketError.Success;
852 if (!socket.DisconnectExBlocking(handle, reuseSocket ? 2 : 0, 0))
853 {
854 result = GetLastSocketError();
855 }
856 return result;
857 }
858
860 {
862 {
863 ProtocolInformation = new byte[sizeof(global::Interop.Winsock.WSAPROTOCOL_INFOW)]
864 };
865 fixed (byte* ptr = socketInformation.ProtocolInformation)
866 {
867 global::Interop.Winsock.WSAPROTOCOL_INFOW* lpProtocolInfo = (global::Interop.Winsock.WSAPROTOCOL_INFOW*)ptr;
868 if (global::Interop.Winsock.WSADuplicateSocket(handle, (uint)targetProcessId, lpProtocolInfo) != 0)
869 {
870 return GetLastSocketError();
871 }
872 return SocketError.Success;
873 }
874 }
875}
static readonly IPAddress IPv6None
Definition IPAddress.cs:39
static readonly IPAddress None
Definition IPAddress.cs:25
static long HostToNetworkOrder(long host)
Definition IPAddress.cs:408
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static void ValidateSegment(ArraySegment< byte > segment)
static SocketError Set(SafeSocketHandle handle, SocketOptionName optionName, byte[] optionValueSeconds)
static SocketError Get(SafeSocketHandle handle, SocketOptionName optionName, byte[] optionValueSeconds, ref int optionLength)
static unsafe bool TransmitFileHelper(SafeHandle socket, SafeHandle fileHandle, NativeOverlapped *overlapped, IntPtr pinnedPreBuffer, int preBufferLength, IntPtr pinnedPostBuffer, int postBufferLength, TransmitFileOptions flags)
Definition SocketPal.cs:809
static unsafe SocketError Receive(SafeSocketHandle handle, IList< ArraySegment< byte > > buffers, SocketFlags socketFlags, out int bytesTransferred)
Definition SocketPal.cs:295
static unsafe SocketError GetPeerName(SafeSocketHandle handle, Span< byte > buffer, ref int nameLen)
Definition SocketPal.cs:126
static unsafe SocketError Send(SafeSocketHandle handle, ReadOnlySpan< byte > buffer, SocketFlags socketFlags, out int bytesTransferred)
Definition SocketPal.cs:243
static SocketError CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, out SafeSocketHandle socket)
Definition SocketPal.cs:27
static unsafe SocketError Select(IList checkRead, IList checkWrite, IList checkError, int microseconds)
Definition SocketPal.cs:711
static SocketError Connect(SafeSocketHandle handle, byte[] peerAddress, int peerAddressLen)
Definition SocketPal.cs:174
static SocketError GetLingerOption(SafeSocketHandle handle, out LingerOption optionValue)
Definition SocketPal.cs:657
static SocketError WindowsIoctl(SafeSocketHandle handle, int ioControlCode, byte[] optionInValue, byte[] optionOutValue, out int optionLength)
Definition SocketPal.cs:468
static readonly int MaximumAddressSize
Definition SocketPal.cs:14
static unsafe SocketError GetSockOpt(SafeSocketHandle handle, SocketOptionLevel optionLevel, SocketOptionName optionName, out int optionValue)
Definition SocketPal.cs:579
static SocketError SetSockOpt(SafeSocketHandle handle, SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
Definition SocketPal.cs:482
static SocketError Accept(SafeSocketHandle listenSocket, byte[] socketAddress, ref int socketAddressSize, out SafeSocketHandle socket)
Definition SocketPal.cs:159
static SocketError Disconnect(Socket socket, SafeSocketHandle handle, bool reuseSocket)
Definition SocketPal.cs:849
static void MicrosecondsToTimeValue(long microseconds, ref global::Interop.Winsock.TimeValue socketTime)
Definition SocketPal.cs:16
static unsafe SocketError SendFile(SafeSocketHandle handle, SafeFileHandle fileHandle, ReadOnlySpan< byte > preBuffer, ReadOnlySpan< byte > postBuffer, TransmitFileOptions flags)
Definition SocketPal.cs:259
static SocketError ReceiveFrom(SafeSocketHandle handle, byte[] buffer, int offset, int size, SocketFlags socketFlags, byte[] socketAddress, ref int addressLength, out int bytesTransferred)
Definition SocketPal.cs:447
static void SetReceivingDualModeIPv4PacketInformation(Socket socket)
Definition SocketPal.cs:522
static unsafe IPPacketInformation GetIPPacketInformation(global::Interop.Winsock.ControlDataIPv6 *controlBuffer)
Definition SocketPal.cs:376
static SocketError SetMulticastOption(SafeSocketHandle handle, SocketOptionName optionName, MulticastOption optionValue)
Definition SocketPal.cs:527
static SocketError SendTo(SafeSocketHandle handle, byte[] buffer, int offset, int size, SocketFlags socketFlags, byte[] peerAddress, int peerAddressSize, out int bytesTransferred)
Definition SocketPal.cs:274
static void CheckDualModeReceiveSupport(Socket socket)
Definition SocketPal.cs:845
static unsafe SocketError SetRawSockOpt(SafeSocketHandle handle, int optionLevel, int optionName, ReadOnlySpan< byte > optionValue)
Definition SocketPal.cs:509
static unsafe SocketError Poll(SafeSocketHandle handle, int microseconds, SelectMode mode, out bool status)
Definition SocketPal.cs:671
static unsafe SocketError Receive(SafeSocketHandle handle, Span< byte > buffer, SocketFlags socketFlags, out int bytesTransferred)
Definition SocketPal.cs:354
static SocketError Send(SafeSocketHandle handle, byte[] buffer, int offset, int size, SocketFlags socketFlags, out int bytesTransferred)
Definition SocketPal.cs:238
static SocketError SetIPv6MulticastOption(SafeSocketHandle handle, SocketOptionName optionName, IPv6MulticastOption optionValue)
Definition SocketPal.cs:548
static unsafe SocketError GetSockOpt(SafeSocketHandle handle, SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue, ref int optionLength)
Definition SocketPal.cs:597
static SocketError ReceiveMessageFrom(Socket socket, SafeSocketHandle handle, byte[] buffer, int offset, int size, ref SocketFlags socketFlags, System.Net.Internals.SocketAddress socketAddress, out System.Net.Internals.SocketAddress receiveAddress, out IPPacketInformation ipPacketInformation, out int bytesTransferred)
Definition SocketPal.cs:386
static SocketError Receive(SafeSocketHandle handle, byte[] buffer, int offset, int size, SocketFlags socketFlags, out int bytesTransferred)
Definition SocketPal.cs:349
static unsafe SocketError SendTo(SafeSocketHandle handle, ReadOnlySpan< byte > buffer, SocketFlags socketFlags, byte[] peerAddress, int peerAddressSize, out int bytesTransferred)
Definition SocketPal.cs:279
static SocketError Listen(SafeSocketHandle handle, int backlog)
Definition SocketPal.cs:149
static unsafe SocketError GetRawSockOpt(SafeSocketHandle handle, int optionLevel, int optionName, Span< byte > optionValue, ref int optionLength)
Definition SocketPal.cs:614
static unsafe SocketError CreateSocket(SocketInformation socketInformation, out SafeSocketHandle socket, ref AddressFamily addressFamily, ref SocketType socketType, ref ProtocolType protocolType)
Definition SocketPal.cs:49
static unsafe SocketError GetSockName(SafeSocketHandle handle, byte *buffer, int *nameLen)
Definition SocketPal.cs:104
static SocketError GetMulticastOption(SafeSocketHandle handle, SocketOptionName optionName, out MulticastOption optionValue)
Definition SocketPal.cs:627
static SocketError Bind(SafeSocketHandle handle, ProtocolType socketProtocolType, byte[] buffer, int nameLen)
Definition SocketPal.cs:139
static SocketError SetLingerOption(SafeSocketHandle handle, LingerOption optionValue)
Definition SocketPal.cs:561
static void SetIPProtectionLevel(Socket socket, SocketOptionLevel optionLevel, int protectionLevel)
Definition SocketPal.cs:574
static SocketError SetBlocking(SafeSocketHandle handle, bool shouldBlock, out bool willBlock)
Definition SocketPal.cs:92
static unsafe SocketError ReceiveFrom(SafeSocketHandle handle, Span< byte > buffer, SocketFlags socketFlags, byte[] socketAddress, ref int addressLength, out int bytesTransferred)
Definition SocketPal.cs:452
static SocketError GetLastSocketError()
Definition SocketPal.cs:22
static SocketError GetIPv6MulticastOption(SafeSocketHandle handle, SocketOptionName optionName, out IPv6MulticastOption optionValue)
Definition SocketPal.cs:643
static unsafe SocketError Send(SafeSocketHandle handle, IList< ArraySegment< byte > > buffers, SocketFlags socketFlags, out int bytesTransferred)
Definition SocketPal.cs:184
static unsafe IPPacketInformation GetIPPacketInformation(global::Interop.Winsock.ControlData *controlBuffer)
Definition SocketPal.cs:370
static unsafe SocketError DuplicateSocket(SafeSocketHandle handle, int targetProcessId, out SocketInformation socketInformation)
Definition SocketPal.cs:859
static SocketError Shutdown(SafeSocketHandle handle, bool isConnected, bool isDisconnected, SocketShutdown how)
Definition SocketPal.cs:798
static unsafe SocketError SetSockOpt(SafeSocketHandle handle, SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
Definition SocketPal.cs:492
static unsafe SocketError ReceiveMessageFrom(Socket socket, SafeSocketHandle handle, Span< byte > buffer, ref SocketFlags socketFlags, System.Net.Internals.SocketAddress socketAddress, out System.Net.Internals.SocketAddress receiveAddress, out IPPacketInformation ipPacketInformation, out int bytesTransferred)
Definition SocketPal.cs:391
static SocketError GetAvailable(SafeSocketHandle handle, out int available)
Definition SocketPal.cs:114
static void GetIPProtocolInformation(AddressFamily addressFamily, System.Net.Internals.SocketAddress socketAddress, out bool isIPv4, out bool isIPv6)
Definition Socket.cs:3208
static void SelectFileDescriptor(IList socketList, Span< IntPtr > fileDescriptorSet, ref int refsAdded)
Definition Socket.cs:4532
static void SocketListToFileDescriptorSet(IList socketList, Span< IntPtr > fileDescriptorSet, ref int refsAdded)
Definition Socket.cs:4511
static void SocketListDangerousReleaseRefs(IList socketList, ref int refsAdded)
Definition Socket.cs:3836
static unsafe IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index)
Definition Marshal.cs:775
void DangerousAddRef(ref bool success)
Definition SafeHandle.cs:76
static string net_sockets_invalid_socketinformation
Definition SR.cs:114
static string net_sockets_useblocking
Definition SR.cs:52
Definition SR.cs:7
static readonly IntPtr Zero
Definition IntPtr.cs:18
static GCHandle Alloc(object? value)
Definition GCHandle.cs:81
static readonly UIntPtr Zero
Definition UIntPtr.cs:19