public unsafe RioConnectionOrientedSocketPool(RioFixedBufferPool sendPool, RioFixedBufferPool revicePool, uint socketCount, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024, uint maxConnections = 1024) : base(sendPool, revicePool, adressFam, sockType, protocol, maxOutstandingReceive, maxOutstandingSend, maxConnections) { var adrSize = (sizeof(sockaddr_in) + 16) * 2; var overlapped = Marshal.AllocHGlobal(new IntPtr(socketCount * Marshal.SizeOf<RioNativeOverlapped>())); var adressBuffer = Marshal.AllocHGlobal(new IntPtr(socketCount * adrSize)); allSockets = new RioConnectionOrientedSocket[socketCount]; for (int i = 0; i < socketCount; i++) { allSockets[i] = new RioConnectionOrientedSocket(overlapped + (i * Marshal.SizeOf<RioNativeOverlapped>()), adressBuffer + (i * adrSize), this, SendBufferPool, ReceiveBufferPool, maxOutstandingReceive, maxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol); allSockets[i]._overlapped->SocketIndex = i; } if ((socketIocp = Kernel32.CreateIoCompletionPort((IntPtr)(-1), IntPtr.Zero, 0, 1)) == IntPtr.Zero) Kernel32.ThrowLastError(); foreach (var s in allSockets) { if ((Kernel32.CreateIoCompletionPort(s.Socket, socketIocp, 0, 1)) == IntPtr.Zero) Kernel32.ThrowLastError(); } Thread SocketIocpThread = new Thread(SocketIocpComplete); SocketIocpThread.IsBackground = true; SocketIocpThread.Start(); }
internal RioSocket(RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) { if ((Socket = WinSock.WSASocket(adressFam, sockType, protocol, IntPtr.Zero, 0, SOCKET_FLAGS.REGISTERED_IO | SOCKET_FLAGS.WSA_FLAG_OVERLAPPED)) == IntPtr.Zero) { WinSock.ThrowLastWSAError(); } SendBufferPool = sendBufferPool; ReceiveBufferPool = receiveBufferPool; _requestQueue = RioStatic.CreateRequestQueue(Socket, maxOutstandingReceive, 1, maxOutstandingSend, 1, ReceiveCompletionQueue, SendCompletionQueue, GetHashCode()); WinSock.ThrowLastWSAError(); onIncommingSegmentWrapper = (socket, segment) => { onIncommingSegmentSafe(segment); if (segment.CurrentContentLength > 0) { socket.BeginReceive(); } else { socket.Dispose(); } segment.Dispose(); }; }
//ADDRESS_FAMILIES.AF_INET, SOCKET_TYPE.SOCK_DGRAM, PROTOCOL.IPPROTO_UDP internal RioConnectionlessSocket(RioSocketPool pool, RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, RioFixedBufferPool adressBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) : base(sendBufferPool, receiveBufferPool, adressBufferPool, maxOutstandingReceive, maxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol) { }
internal RioConnectionlessSocket(RioSocketPool pool, RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) : base(sendBufferPool, receiveBufferPool, maxOutstandingReceive, maxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol) //ADDRESS_FAMILIES.AF_INET, SOCKET_TYPE.SOCK_DGRAM, PROTOCOL.IPPROTO_UDP { }
internal RioSocketBase(RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) { if ((Socket = WinSock.WSASocket(adressFam, sockType, protocol, IntPtr.Zero, 0, SOCKET_FLAGS.REGISTERED_IO | SOCKET_FLAGS.WSA_FLAG_OVERLAPPED)) == IntPtr.Zero) WinSock.ThrowLastWSAError(); SendBufferPool = sendBufferPool; ReceiveBufferPool = receiveBufferPool; _requestQueue = RioStatic.CreateRequestQueue(Socket, maxOutstandingReceive, 1, maxOutstandingSend, 1, ReceiveCompletionQueue, SendCompletionQueue, GetHashCode()); WinSock.ThrowLastWSAError(); }
internal RioSocket(RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) { if ((Socket = WinSock.WSASocket(adressFam, sockType, protocol, IntPtr.Zero, 0, SOCKET_FLAGS.REGISTERED_IO | SOCKET_FLAGS.WSA_FLAG_OVERLAPPED)) == IntPtr.Zero) { WinSock.ThrowLastWSAError(); } SendBufferPool = sendBufferPool; ReceiveBufferPool = receiveBufferPool; _requestQueue = RioStatic.CreateRequestQueue(Socket, maxOutstandingReceive, 1, maxOutstandingSend, 1, ReceiveCompletionQueue, SendCompletionQueue, GetHashCode()); WinSock.ThrowLastWSAError(); }
internal RioConnectionOrientedSocket(IntPtr overlapped, IntPtr adressBuffer, RioConnectionOrientedSocketPool pool, RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) : base(sendBufferPool, receiveBufferPool, maxOutstandingReceive, maxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol) //ADDRESS_FAMILIES.AF_INET, SOCKET_TYPE.SOCK_STREAM, PROTOCOL.IPPROTO_TCP { _overlapped = (RioNativeOverlapped*)overlapped.ToPointer(); _eventHandle = Kernel32.CreateEvent(IntPtr.Zero, false, false, null); _adressBuffer = adressBuffer; _pool = pool; unsafe { var n = (NativeOverlapped*)overlapped.ToPointer(); n->EventHandle = _eventHandle; } }
internal RioConnectionOrientedSocket(IntPtr overlapped, IntPtr adressBuffer, RioConnectionOrientedSocketPool pool, RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) : base(sendBufferPool, receiveBufferPool, maxOutstandingReceive, maxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol) //ADDRESS_FAMILIES.AF_INET, SOCKET_TYPE.SOCK_STREAM, PROTOCOL.IPPROTO_TCP { _overlapped = (RioNativeOverlapped *)overlapped.ToPointer(); _eventHandle = Kernel32.CreateEvent(IntPtr.Zero, false, false, null); _adressBuffer = adressBuffer; _pool = pool; unsafe { var n = (NativeOverlapped *)overlapped.ToPointer(); n->EventHandle = _eventHandle; } }
internal RioSocket(RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, RioFixedBufferPool adressBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) { this.adressFam = adressFam; this.sockType = sockType; this.protocol = protocol; this.maxOutstandingReceive = maxOutstandingReceive; this.maxOutstandingSend = maxOutstandingSend; this.ReceiveCompletionQueue = ReceiveCompletionQueue; this.SendCompletionQueue = SendCompletionQueue; SendBufferPool = sendBufferPool; ReceiveBufferPool = receiveBufferPool; AdressPool = adressBufferPool; ResetSocket(); }
internal RioSocket(RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) { if ((Socket = WinSock.WSASocket(adressFam, sockType, protocol, IntPtr.Zero, 0, SOCKET_FLAGS.REGISTERED_IO | SOCKET_FLAGS.WSA_FLAG_OVERLAPPED)) == IntPtr.Zero) WinSock.ThrowLastWSAError(); SendBufferPool = sendBufferPool; ReceiveBufferPool = receiveBufferPool; _requestQueue = RioStatic.CreateRequestQueue(Socket, maxOutstandingReceive, 1, maxOutstandingSend, 1, ReceiveCompletionQueue, SendCompletionQueue, GetHashCode()); WinSock.ThrowLastWSAError(); onIncommingSegmentWrapper = (socket, segment) => { onIncommingSegmentSafe(segment); if (segment.CurrentContentLength > 0) socket.BeginReceive(); else socket.Dispose(); segment.Dispose(); }; }
internal RioConnectionOrientedSocket(ulong socketid, IntPtr overlapped, IntPtr adressBuffer, RioConnectionOrientedSocketPool pool, RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool, RioFixedBufferPool adressBufferPool, uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol) : base(sendBufferPool, receiveBufferPool, adressBufferPool, maxOutstandingReceive, maxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol) { _overlapped = (RioNativeOverlapped *)overlapped.ToPointer(); _adressBuffer = adressBuffer; _pool = pool; onreadCompletion = id => { if (id == currentId) { Interlocked.Decrement(ref pendingRecives); } }; onSendCompletion = id => { if (id == currentId) { Interlocked.Decrement(ref pendingSends); } }; sendTimeout = Stopwatch.Frequency * 5; receiveTimeout = Stopwatch.Frequency * 5; currentId = socketid; }
internal static extern IntPtr WSASocket([In] ADDRESS_FAMILIES af, [In] SOCKET_TYPE type, [In] PROTOCOL protocol, [In] IntPtr lpProtocolInfo, [In] int group, [In] SOCKET_FLAGS dwFlags);
public static extern IntPtr socket([In] ADDRESS_FAMILIES af, [In] SOCKET_TYPE socket_type, [In] PROTOCOL protocol);
public RioConnectionlessSocketPool(RioFixedBufferPool sendPool, RioFixedBufferPool revicePool, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024, uint maxSockets = 128, int adressBufferSize = 1024) : base(sendPool, revicePool, adressFam, sockType, protocol, maxOutstandingReceive, maxOutstandingSend, maxSockets, adressBufferSize) { }
public static extern IntPtr WSASocketW(ADDRESS_FAMILIES af, SOCKET_TYPE socket_type, PROTOCOL protocol, IntPtr lpProtocolInfo, Int32 group, OPTION_FLAGS_PER_SOCKET dwFlags);
public unsafe RioSocketPool(RioFixedBufferPool sendPool, RioFixedBufferPool receivePool, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024, uint maxOutsandingCompletions = 2048) { MaxOutstandingReceive = maxOutstandingReceive; MaxOutstandingSend = maxOutstandingSend; MaxOutsandingCompletions = maxOutsandingCompletions; SendBufferPool = sendPool; ReceiveBufferPool = receivePool; this.adressFam = adressFam; this.sockType = sockType; this.protocol = protocol; var version = new Version(2, 2); WSAData data; var result = WinSock.WSAStartup((short)version.Raw, out data); if (result != 0) { WinSock.ThrowLastWSAError(); } RioStatic.Initalize(); if ((ReceiveCompletionPort = Kernel32.CreateIoCompletionPort((IntPtr)(-1), IntPtr.Zero, 0, 1)) == IntPtr.Zero) { Kernel32.ThrowLastError(); } if ((SendCompletionPort = Kernel32.CreateIoCompletionPort((IntPtr)(-1), IntPtr.Zero, 0, 1)) == IntPtr.Zero) { Kernel32.ThrowLastError(); } _sendBufferId = RioStatic.RegisterBuffer(SendBufferPool.BufferPointer, (uint)SendBufferPool.TotalLength); WinSock.ThrowLastWSAError(); SendBufferPool.SetBufferId(_sendBufferId); _reciveBufferId = RioStatic.RegisterBuffer(ReceiveBufferPool.BufferPointer, (uint)ReceiveBufferPool.TotalLength); WinSock.ThrowLastWSAError(); ReceiveBufferPool.SetBufferId(_reciveBufferId); var sendCompletionMethod = new RIO_NOTIFICATION_COMPLETION() { Type = RIO_NOTIFICATION_COMPLETION_TYPE.IOCP_COMPLETION, Iocp = new RIO_NOTIFICATION_COMPLETION_IOCP() { IocpHandle = SendCompletionPort, QueueCorrelation = 0, Overlapped = (NativeOverlapped *)-1 } }; if ((SendCompletionQueue = RioStatic.CreateCompletionQueue(MaxOutsandingCompletions, sendCompletionMethod)) == IntPtr.Zero) { WinSock.ThrowLastWSAError(); } var receiveCompletionMethod = new RIO_NOTIFICATION_COMPLETION() { Type = RIO_NOTIFICATION_COMPLETION_TYPE.IOCP_COMPLETION, Iocp = new RIO_NOTIFICATION_COMPLETION_IOCP() { IocpHandle = ReceiveCompletionPort, QueueCorrelation = 0, Overlapped = (NativeOverlapped *)-1 } }; if ((ReceiveCompletionQueue = RioStatic.CreateCompletionQueue(MaxOutsandingCompletions, receiveCompletionMethod)) == IntPtr.Zero) { WinSock.ThrowLastWSAError(); } Thread receiveThread = new Thread(ProcessReceiveCompletes); receiveThread.IsBackground = true; receiveThread.Start(); Thread sendThread = new Thread(ProcessSendCompletes); sendThread.IsBackground = true; sendThread.Start(); }
public unsafe RioConnectionOrientedSocketPool(RioFixedBufferPool sendPool, RioFixedBufferPool revicePool, uint socketCount, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024) : base(sendPool, revicePool, adressFam, sockType, protocol, maxOutstandingReceive, maxOutstandingSend, socketCount) { var adrSize = (sizeof(sockaddr_in) + 16) * 2; var overlapped = Marshal.AllocHGlobal(new IntPtr(socketCount * Marshal.SizeOf <RioNativeOverlapped>())); var adressBuffer = Marshal.AllocHGlobal(new IntPtr(socketCount * adrSize)); allSockets = new RioConnectionOrientedSocket[socketCount]; var inc = ulong.MaxValue / socketCount; for (int i = 0; i < socketCount; i++) { allSockets[i] = new RioConnectionOrientedSocket(inc * (ulong)i, overlapped + (i * Marshal.SizeOf <RioNativeOverlapped>()), adressBuffer + (i * adrSize), this, SendBufferPool, ReceiveBufferPool, adressBufferPool, maxOutstandingReceive, maxOutstandingSend, SendCompletionQueue, ReceiveCompletionQueue, adressFam, sockType, protocol); allSockets[i]._overlapped->SocketIndex = i; } if ((socketIocp = Kernel32.CreateIoCompletionPort((IntPtr)(-1), IntPtr.Zero, 0, 1)) == IntPtr.Zero) { Kernel32.ThrowLastError(); } foreach (var s in allSockets) { if ((Kernel32.CreateIoCompletionPort(s.Socket, socketIocp, 0, 1)) == IntPtr.Zero) { Kernel32.ThrowLastError(); } } Thread SocketIocpThread = new Thread(SocketIocpComplete); SocketIocpThread.IsBackground = true; SocketIocpThread.Start(); Timeout(); }
public unsafe RioSocketPool(RioFixedBufferPool sendPool, RioFixedBufferPool receivePool, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024, uint maxOutsandingCompletions = 2048) { MaxOutstandingReceive = maxOutstandingReceive; MaxOutstandingSend = maxOutstandingSend; MaxOutsandingCompletions = maxOutsandingCompletions; SendBufferPool = sendPool; ReceiveBufferPool = receivePool; this.adressFam = adressFam; this.sockType = sockType; this.protocol = protocol; var version = new Version(2, 2); WSAData data; var result = WinSock.WSAStartup((short)version.Raw, out data); if (result != 0) WinSock.ThrowLastWSAError(); RioStatic.Initalize(); if ((ReceiveCompletionPort = Kernel32.CreateIoCompletionPort((IntPtr)(-1), IntPtr.Zero, 0, 1)) == IntPtr.Zero) Kernel32.ThrowLastError(); if ((SendCompletionPort = Kernel32.CreateIoCompletionPort((IntPtr)(-1), IntPtr.Zero, 0, 1)) == IntPtr.Zero) Kernel32.ThrowLastError(); _sendBufferId = RioStatic.RegisterBuffer(SendBufferPool.BufferPointer, (uint)SendBufferPool.TotalLength); WinSock.ThrowLastWSAError(); SendBufferPool.SetBufferId(_sendBufferId); _reciveBufferId = RioStatic.RegisterBuffer(ReceiveBufferPool.BufferPointer, (uint)ReceiveBufferPool.TotalLength); WinSock.ThrowLastWSAError(); ReceiveBufferPool.SetBufferId(_reciveBufferId); var sendCompletionMethod = new RIO_NOTIFICATION_COMPLETION() { Type = RIO_NOTIFICATION_COMPLETION_TYPE.IOCP_COMPLETION, Iocp = new RIO_NOTIFICATION_COMPLETION_IOCP() { IocpHandle = SendCompletionPort, QueueCorrelation = 0, Overlapped = (NativeOverlapped*)-1 } }; if ((SendCompletionQueue = RioStatic.CreateCompletionQueue(MaxOutsandingCompletions, sendCompletionMethod)) == IntPtr.Zero) WinSock.ThrowLastWSAError(); var receiveCompletionMethod = new RIO_NOTIFICATION_COMPLETION() { Type = RIO_NOTIFICATION_COMPLETION_TYPE.IOCP_COMPLETION, Iocp = new RIO_NOTIFICATION_COMPLETION_IOCP() { IocpHandle = ReceiveCompletionPort, QueueCorrelation = 0, Overlapped = (NativeOverlapped*)-1 } }; if ((ReceiveCompletionQueue = RioStatic.CreateCompletionQueue(MaxOutsandingCompletions, receiveCompletionMethod)) == IntPtr.Zero) WinSock.ThrowLastWSAError(); Thread receiveThread = new Thread(ProcessReceiveCompletes); receiveThread.IsBackground = true; receiveThread.Start(); Thread sendThread = new Thread(ProcessSendCompletes); sendThread.IsBackground = true; sendThread.Start(); }
public RioConnectionlessSocketPool(RioFixedBufferPool sendPool, RioFixedBufferPool revicePool, ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024, uint maxOutsandingCompletions = 1024) : base(sendPool, revicePool, adressFam, sockType, protocol, maxOutstandingReceive, maxOutstandingSend, maxOutsandingCompletions) { }