public void Listen(IPEndPoint localEP, int backlog) { in_addr inAddress = new in_addr(); inAddress.s_b1 = localEP.Address.GetAddressBytes()[0]; inAddress.s_b2 = localEP.Address.GetAddressBytes()[1]; inAddress.s_b3 = localEP.Address.GetAddressBytes()[2]; inAddress.s_b4 = localEP.Address.GetAddressBytes()[3]; sockaddr_in sa = new sockaddr_in(); sa.sin_family = ADDRESS_FAMILIES.AF_INET; sa.sin_port = WinSock.htons((ushort)localEP.Port); //Imports.ThrowLastWSAError(); sa.sin_addr = inAddress; unsafe { if (WinSock.bind(_listenerSocket, ref sa, sizeof(sockaddr_in)) == WinSock.SOCKET_ERROR) WinSock.ThrowLastWSAError(); } if (WinSock.listen(_listenerSocket, backlog) == WinSock.SOCKET_ERROR) WinSock.ThrowLastWSAError(); foreach (var s in allSockets) BeginAccept(s); }
public unsafe Task<RioConnectionOrientedSocket> Connect(Uri adress) { var adr = Dns.GetHostAddressesAsync(adress.Host).Result.First(i => i.AddressFamily == AddressFamily.InterNetwork); in_addr inAddress = new in_addr(); inAddress.s_b1 = adr.GetAddressBytes()[0]; inAddress.s_b2 = adr.GetAddressBytes()[1]; inAddress.s_b3 = adr.GetAddressBytes()[2]; inAddress.s_b4 = adr.GetAddressBytes()[3]; sockaddr_in sa = new sockaddr_in(); sa.sin_family = ADDRESS_FAMILIES.AF_INET; sa.sin_port = WinSock.htons((ushort)adress.Port); //Imports.ThrowLastWSAError(); sa.sin_addr = inAddress; RioConnectionOrientedSocket s; _freeSockets.TryDequeue(out s); var tcs = new TaskCompletionSource<RioConnectionOrientedSocket>(); _ongoingConnections.TryAdd(s, tcs); uint gurka; unsafe { s.ResetOverlapped(); s._overlapped->Status = 2; if (!RioStatic.ConnectEx(s.Socket, sa, sizeof(sockaddr_in), IntPtr.Zero, 0, out gurka, s._overlapped)) if (WinSock.WSAGetLastError() != 997) // error_io_pending WinSock.ThrowLastWSAError(); } return tcs.Task; }
public RioTcpClientPool(RioFixedBufferPool sendPool, RioFixedBufferPool revicePool, uint socketCount, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024) : base(sendPool, revicePool, socketCount, maxOutstandingReceive, maxOutstandingSend, (maxOutstandingReceive + maxOutstandingSend) * socketCount) { foreach (var s in allSockets) { _freeSockets.Enqueue(s); in_addr inAddress = new in_addr(); inAddress.s_b1 = 0; inAddress.s_b2 = 0; inAddress.s_b3 = 0; inAddress.s_b4 = 0; sockaddr_in sa = new sockaddr_in(); sa.sin_family = ADDRESS_FAMILIES.AF_INET; sa.sin_port = 0; //Imports.ThrowLastWSAError(); sa.sin_addr = inAddress; unsafe { if (WinSock.bind(s.Socket, ref sa, sizeof(sockaddr_in)) == WinSock.SOCKET_ERROR) WinSock.ThrowLastWSAError(); } } }
internal override void InitializeSocket(RioConnectionOrientedSocket socket) { socket.SetLoopbackFastPath(true); socket.SetTcpNoDelay(true); sockaddr_in sa = new sockaddr_in(); sa.sin_family = adressFam; unsafe { if (WinSock.bind(socket.Socket, ref sa, sizeof(sockaddr_in)) == WinSock.SOCKET_ERROR) WinSock.ThrowLastWSAError(); } }
/// <summary> /// Binds a socket to a local ip and port, /// for multicast applications sending to a destinations on the same machine, use port 0 /// </summary> /// <param name="localEP"></param> /// <returns></returns> public unsafe RioConnectionlessSocket Bind(IPEndPoint localEP) { var socket = new RioConnectionlessSocket(this, SendBufferPool, ReceiveBufferPool, adressBufferPool, MaxOutstandingReceive, MaxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol); sockaddr_in sa = new sockaddr_in(); sa.sin_family = ADDRESS_FAMILIES.AF_INET; sa.sin_port = WinSock.htons((ushort)localEP.Port); var ipBytes = localEP.Address.GetAddressBytes(); fixed (byte* a = ipBytes) Unsafe.CopyBlock(sa.sin_addr.Address, a, (uint)ipBytes.Length); unsafe { if (WinSock.bind(socket.Socket, ref sa, sizeof(sockaddr_in)) == WinSock.SOCKET_ERROR) WinSock.ThrowLastWSAError(); } socket.SetInUse(true); return socket; }
public async Task<RioSocket> Connect(Uri adress) { var ip = (await Dns.GetHostAddressesAsync(adress.Host)).First(i => i.AddressFamily == AddressFamily.InterNetwork); sockaddr_in sa = new sockaddr_in(); sa.sin_family = adressFam; sa.sin_port = WinSock.htons((ushort)adress.Port); var ipBytes = ip.GetAddressBytes(); unsafe { fixed (byte* a = ipBytes) Unsafe.CopyBlock(sa.sin_addr.Address, a, (uint)ipBytes.Length); } RioConnectionOrientedSocket s; if (_freeSockets.TryDequeue(out s)) { s.SetInUse(true); var tcs = new TaskCompletionSource<RioSocket>(); _ongoingConnections.TryAdd(s, tcs); uint bytesSent; unsafe { s.ResetOverlapped(); s._overlapped->Status = 2; if (!RioStatic.ConnectEx(s.Socket, sa, sizeof(sockaddr_in), IntPtr.Zero, 0, out bytesSent, s._overlapped)) WinSock.ThrowLastWSAError(); } return await tcs.Task; } else return await Task.FromException<RioConnectionOrientedSocket>(new ArgumentException("No sockets available in pool")); }
internal static extern int bind(IntPtr s, ref sockaddr_in name, int namelen);
internal static extern IntPtr accept(IntPtr s, ref sockaddr_in addr, ref int addrlen);