public RMSocket(int socketHandle) { SocketHandle = new IntPtr(socketHandle); StripLF = false; if (OSUtils.IsWindows) { NativeMethods.WSAData WSA = new NativeMethods.WSAData(); SocketError Result = NativeMethods.WSAStartup((short)0x0202, out WSA); if (Result != SocketError.Success) { SocketHandle = IntPtr.Zero; } } if (Connected) { // Start threads _ReadThread = new Thread(new ThreadStart(ReadFunc)); _ReadThread.IsBackground = true; _ReadThread.Start(); _WriteThread = new Thread(new ThreadStart(WriteFunc)); _WriteThread.IsBackground = true; _WriteThread.Start(); } }
public RMSocket(int socketHandle) { SocketHandle = new IntPtr(socketHandle); StripLF = false; if (OSUtils.IsWindows) { NativeMethods.WSAData WSA = new NativeMethods.WSAData(); SocketError Result = NativeMethods.WSAStartup((short)0x0202, out WSA); if (Result != SocketError.Success) { SocketHandle = IntPtr.Zero; } } if (Connected) { // Start threads _ReadThread = new Thread(new ThreadStart(ReadFunc)); _ReadThread.IsBackground = true; _ReadThread.Start(); _WriteThread = new Thread(new ThreadStart(WriteFunc)); _WriteThread.IsBackground = true; _WriteThread.Start(); } }
public bool Open(int ASocketHandle) { if (OSUtils.IsWindows) { try { NativeMethods.WSAData WSA = new NativeMethods.WSAData(); SocketError Result = NativeMethods.WSAStartup((short)0x0202, out WSA); if (Result != SocketError.Success) { throw new SocketException(NativeMethods.WSAGetLastError()); } SocketPermission SP = new SocketPermission(System.Security.Permissions.PermissionState.Unrestricted); SP.Demand(); SocketInformation SI = new SocketInformation() { Options = SocketInformationOptions.Connected, ProtocolInformation = new byte[Marshal.SizeOf(typeof(NativeMethods.WSAPROTOCOL_INFO))], }; Result = SocketError.Success; unsafe { fixed(byte *pinnedBuffer = SI.ProtocolInformation) { Result = NativeMethods.WSADuplicateSocket(new IntPtr(ASocketHandle), (uint)Process.GetCurrentProcess().Id, pinnedBuffer); } } if (Result != SocketError.Success) { throw new SocketException(NativeMethods.WSAGetLastError()); } return(Open(new Socket(SI))); } catch (SocketException sex) { RMLog.DebugException(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString()); return(false); } catch (Exception ex) { RMLog.DebugException(ex, "Exception in TcpConnection::Open()"); return(false); } } else { RMLog.Debug("MONO cannot open an existing socket handle"); return(false); //try //{ // SocketInformation SI = new SocketInformation(); // SI.Options = SocketInformationOptions.Connected; // SI.ProtocolInformation = new byte[24]; // // From Mono's Socket.cs DuplicateAndClose() SI.ProtocolInformation = Mono.DataConverter.Pack("iiiil", (int)address_family, (int)socket_type, (int)protocol_type, isbound ? 1 : 0, (long)socket); // byte[] B1 = BitConverter.GetBytes((int)AddressFamily.InterNetwork); // byte[] B2 = BitConverter.GetBytes((int)SocketType.Stream); // byte[] B3 = BitConverter.GetBytes((int)ProtocolType.Tcp); // byte[] B4 = BitConverter.GetBytes((int)1); // byte[] B5 = BitConverter.GetBytes((long)ASocketHandle); // Array.Copy(B1, 0, SI.ProtocolInformation, 0, B1.Length); // Array.Copy(B2, 0, SI.ProtocolInformation, 4, B2.Length); // Array.Copy(B3, 0, SI.ProtocolInformation, 8, B3.Length); // Array.Copy(B4, 0, SI.ProtocolInformation, 12, B4.Length); // Array.Copy(B5, 0, SI.ProtocolInformation, 16, B5.Length); // return Open(new Socket(SI)); //} //catch (SocketException sex) //{ // RMLog.DebugException(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString()); // return false; //} //catch (Exception ex) //{ // RMLog.DebugException(ex, "Exception in TcpConnection::Open()"); // return false; //} } }
public bool Open(int ASocketHandle) { if (OSUtils.IsWindows) { try { NativeMethods.WSAData WSA = new NativeMethods.WSAData(); SocketError Result = NativeMethods.WSAStartup((short)0x0202, out WSA); if (Result != SocketError.Success) throw new SocketException(NativeMethods.WSAGetLastError()); SocketPermission SP = new SocketPermission(System.Security.Permissions.PermissionState.Unrestricted); SP.Demand(); SocketInformation SI = new SocketInformation(); SI.Options = SocketInformationOptions.Connected; SI.ProtocolInformation = new byte[Marshal.SizeOf(typeof(NativeMethods.WSAPROTOCOL_INFO))]; Result = SocketError.Success; unsafe { fixed (byte* pinnedBuffer = SI.ProtocolInformation) { Result = NativeMethods.WSADuplicateSocket(new IntPtr(ASocketHandle), (uint)Process.GetCurrentProcess().Id, pinnedBuffer); } } if (Result != SocketError.Success) throw new SocketException(NativeMethods.WSAGetLastError()); return Open(new Socket(SI)); } catch (SocketException sex) { RMLog.Exception(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString()); return false; } catch (Exception ex) { RMLog.Exception(ex, "Exception in TcpConnection::Open()"); return false; } } else { try { SocketInformation SI = new SocketInformation(); SI.Options = SocketInformationOptions.Connected; SI.ProtocolInformation = new byte[24]; // From Mono's Socket.cs DuplicateAndClose() SI.ProtocolInformation = Mono.DataConverter.Pack("iiiil", (int)address_family, (int)socket_type, (int)protocol_type, isbound ? 1 : 0, (long)socket); byte[] B1 = BitConverter.GetBytes((int)AddressFamily.InterNetwork); byte[] B2 = BitConverter.GetBytes((int)SocketType.Stream); byte[] B3 = BitConverter.GetBytes((int)ProtocolType.Tcp); byte[] B4 = BitConverter.GetBytes((int)1); byte[] B5 = BitConverter.GetBytes((long)ASocketHandle); Array.Copy(B1, 0, SI.ProtocolInformation, 0, B1.Length); Array.Copy(B2, 0, SI.ProtocolInformation, 4, B2.Length); Array.Copy(B3, 0, SI.ProtocolInformation, 8, B3.Length); Array.Copy(B4, 0, SI.ProtocolInformation, 12, B4.Length); Array.Copy(B5, 0, SI.ProtocolInformation, 16, B5.Length); return Open(new Socket(SI)); } catch (SocketException sex) { RMLog.Exception(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString()); return false; } catch (Exception ex) { RMLog.Exception(ex, "Exception in TcpConnection::Open()"); return false; } } }