IEndPoint IEndPoint.CreateCopy() { // copy the inner endpoint EndPoint copy = inner.Create(inner.Serialize()); return(new SteamEndPointWrapper(copy)); }
private EndPoint GetEndPoint(bool fLocal) { if (m_Handle == -1) { throw new ObjectDisposedException(); } EndPoint ep = null; if (m_localEndPoint != null) { byte[] address = null; if (fLocal) { NativeSocket.getsockname(this, out address); } else { NativeSocket.getpeername(this, out address); } SocketAddress socketAddress = new SocketAddress(address); ep = m_localEndPoint.Create(socketAddress); } return(ep); }
public static EndPoint Create(this EndPoint thisObj, Internals.SocketAddress socketAddress) { AddressFamily family = socketAddress.Family; if (family != thisObj.AddressFamily) { throw new ArgumentException(SR.Format(SR.net_InvalidAddressFamily, family.ToString(), thisObj.GetType().FullName, thisObj.AddressFamily.ToString()), nameof(socketAddress)); } if (family == AddressFamily.InterNetwork || family == AddressFamily.InterNetworkV6) { if (socketAddress.Size < 8) { throw new ArgumentException(SR.Format(SR.net_InvalidSocketAddressSize, socketAddress.GetType().FullName, thisObj.GetType().FullName), nameof(socketAddress)); } return(socketAddress.GetIPEndPoint()); } else if (family == AddressFamily.Unknown) { return(thisObj); } System.Net.SocketAddress address = GetNetSocketAddress(socketAddress); return(thisObj.Create(address)); }
public void Create_Successful() { var endPoint = new EndPoint <TestModel>(SingleUseApi($@" {{ ""status"":""success"", ""messages"":""TestModel created successfully."", ""payload"": {TEST1_STRING} }} ")); var response = endPoint.Create(new TestModel { Name = "Test1" }); Assert.Equal(TestModel.Test1, response); }
public int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP) { if (m_Handle == -1) { throw new ObjectDisposedException(); } byte[] address = remoteEP.Serialize().m_Buffer; int len = 0; len = NativeSocket.recvfrom(this, buffer, offset, size, (int)socketFlags, m_recvTimeout, ref address); SocketAddress socketAddress = new SocketAddress(address); remoteEP = remoteEP.Create(socketAddress); return(len); }
private void OnSocketReceived(SocketAsyncEventArgs e) { var receivedData = e.Buffer.CloneRange(e.Offset, e.BytesTransferred); var address = e.RemoteEndPoint.Serialize(); m_UdpClientConnected.Set(); var ipAddress = EndPoint.Create(address) as IPEndPoint; if (m_InvalidEndPoint.Equals(ipAddress.ToString())) { return; } if (m_SessionKeyFromCommandInfo) { ProcessReceivedDataWithSessionKey(ipAddress, receivedData); } else { ProcessReceivedData(ipAddress, receivedData); } }
internal void FinishOperationSuccess(SocketError socketError, int bytesTransferred, SocketFlags flags) { SetResults(socketError, bytesTransferred, flags); switch (_completedOperation) { case SocketAsyncOperation.Accept: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } // Get the endpoint. Internals.SocketAddress remoteSocketAddress = IPEndPointExtensions.Serialize(_currentSocket._rightEndPoint); socketError = FinishOperationAccept(remoteSocketAddress); if (socketError == SocketError.Success) { _acceptSocket = _currentSocket.UpdateAcceptSocket(_acceptSocket, _currentSocket._rightEndPoint.Create(remoteSocketAddress)); if (s_loggingEnabled) { SocketsEventSource.Accepted(_acceptSocket, _acceptSocket.RemoteEndPoint, _acceptSocket.LocalEndPoint); } } else { SetResults(socketError, bytesTransferred, SocketFlags.None); _acceptSocket = null; } break; case SocketAsyncOperation.Connect: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } socketError = FinishOperationConnect(); // Mark socket connected. if (socketError == SocketError.Success) { if (s_loggingEnabled) { SocketsEventSource.Connected(_currentSocket, _currentSocket.LocalEndPoint, _currentSocket.RemoteEndPoint); } _currentSocket.SetToConnected(); _connectSocket = _currentSocket; } break; case SocketAsyncOperation.Disconnect: _currentSocket.SetToDisconnected(); _currentSocket._remoteEndPoint = null; break; case SocketAsyncOperation.Receive: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } break; case SocketAsyncOperation.ReceiveFrom: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); Internals.SocketAddress socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } break; case SocketAsyncOperation.ReceiveMessageFrom: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } FinishOperationReceiveMessageFrom(); break; case SocketAsyncOperation.Send: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } break; case SocketAsyncOperation.SendPackets: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogSendPacketsBuffers(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } FinishOperationSendPackets(); break; case SocketAsyncOperation.SendTo: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } break; } if (socketError != SocketError.Success) { // Asynchronous failure or something went wrong after async success. SetResults(socketError, bytesTransferred, flags); _currentSocket.UpdateStatusAfterSocketError(socketError); } // Complete the operation and raise completion event. Complete(); if (_contextCopy == null) { OnCompleted(this); } else { ExecutionContext.Run(_contextCopy, _executionCallback, null); } }
public void Create_Failure() { var endPoint = new EndPoint <TestModel>(SingleUseApi()); var ex = Assert.Throws <MissingRequiredFieldException <TestModel> >(() => endPoint.Create(new TestModel())); Assert.Equal("Missing required field \"Name\" in object of type \"SnipeSharp.Tests.TestModel\"", ex.Message); }
internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags) { SetResults(SocketError.Success, bytesTransferred, flags); if (NetEventSource.IsEnabled || Socket.s_perfCountersEnabled) { LogBytesTransferred(bytesTransferred, _completedOperation); } SocketError socketError = SocketError.Success; switch (_completedOperation) { case SocketAsyncOperation.Accept: // Get the endpoint. Internals.SocketAddress remoteSocketAddress = IPEndPointExtensions.Serialize(_currentSocket._rightEndPoint); socketError = FinishOperationAccept(remoteSocketAddress); if (socketError == SocketError.Success) { _acceptSocket = _currentSocket.UpdateAcceptSocket(_acceptSocket, _currentSocket._rightEndPoint.Create(remoteSocketAddress)); if (NetEventSource.IsEnabled) { NetEventSource.Accepted(_acceptSocket, _acceptSocket.RemoteEndPoint, _acceptSocket.LocalEndPoint); } } else { SetResults(socketError, bytesTransferred, flags); _acceptSocket = null; _currentSocket.UpdateStatusAfterSocketError(socketError); } break; case SocketAsyncOperation.Connect: socketError = FinishOperationConnect(); if (socketError == SocketError.Success) { if (NetEventSource.IsEnabled) { NetEventSource.Connected(_currentSocket, _currentSocket.LocalEndPoint, _currentSocket.RemoteEndPoint); } // Mark socket connected. _currentSocket.SetToConnected(); _connectSocket = _currentSocket; } else { SetResults(socketError, bytesTransferred, flags); _currentSocket.UpdateStatusAfterSocketError(socketError); } break; case SocketAsyncOperation.Disconnect: _currentSocket.SetToDisconnected(); _currentSocket._remoteEndPoint = null; break; case SocketAsyncOperation.ReceiveFrom: // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); Internals.SocketAddress socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } break; case SocketAsyncOperation.ReceiveMessageFrom: // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } FinishOperationReceiveMessageFrom(); break; case SocketAsyncOperation.SendPackets: FinishOperationSendPackets(); break; } Complete(); }
public static void Create_Invalid() { EndPoint ep = CreateEndPoint(); Assert.Throws <NotImplementedException>(() => ep.Create(null)); }
// Receive data on this socket and record where it came from. public int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP) { int result; byte[] addrReturn; // Validate the arguments. ValidateBuffer(buffer, offset, size); if(remoteEP == null) { throw new ArgumentNullException("remoteEP"); } else if(remoteEP.AddressFamily != family) { throw new SocketException(Errno.EINVAL); } // Create a sockaddr buffer to write the address into. addrReturn = remoteEP.Serialize().Array; Array.Clear(addrReturn, 0, addrReturn.Length); // Perform the receive operation. lock(readLock) { if(handle == InvalidHandle) { throw new ObjectDisposedException (S._("Exception_Disposed")); } result = SocketMethods.ReceiveFrom (handle, buffer, offset, size, (int)socketFlags, addrReturn); if(result < 0) { throw new SocketException(this.GetErrno()); } else { remoteEP = remoteEP.Create (new SocketAddress(addrReturn)); return result; } } }
/// <inheritdoc /> public IEndPoint Create(SocketAddress socketAddress) { return(_endpoint.Create(socketAddress).ToInterface()); }
/// <summary> /// Gets the <see cref="EndPoint"/> deep copy. /// </summary> /// <param name="endPoint">source</param> /// <returns>deep copy object</returns> private static EndPoint CopyEndPoint(EndPoint endPoint) { return(endPoint.Create(endPoint.Serialize())); }