/// <summary> /// Accept callback! /// </summary> internal void BeginAcceptCallback(IAsyncResult ar) { if (!Disposed) { SocketListener listener = null; Socket acceptedSocket = null; ServerSocketConnection connection = null; try { listener = (SocketListener)ar.AsyncState; //----- Get accepted socket! acceptedSocket = listener.Socket.EndAccept(ar); //----- Adjust buffer size! acceptedSocket.ReceiveBufferSize = Host.SocketBufferSize; acceptedSocket.SendBufferSize = Host.SocketBufferSize; connection = new ServerSocketConnection(Host, listener, acceptedSocket); connection.Active = true; //----- Initialize! Host.AddSocketConnection(connection); ThreadPool.QueueUserWorkItem(new WaitCallback(InitializeConnection), connection); } catch (Exception ex) { Host.FireOnException(connection, ex); } //---- Continue to accept! listener.Socket.BeginAccept(new AsyncCallback(BeginAcceptCallback), listener); } }
internal override void BeginSendToAll(ServerSocketConnection connection, byte[] buffer, bool includeMe) { if (!Disposed) { BaseSocketConnection[] items = GetSocketConnections(); if (items != null) { int loopSleep = 0; foreach (BaseSocketConnection cnn in items) { if (Disposed) { break; } try { if (includeMe || connection != cnn) { byte[] localBuffer = new byte[buffer.Length]; Buffer.BlockCopy(buffer, 0, localBuffer, 0, buffer.Length); BeginSend(cnn, localBuffer, true); } } finally { ThreadEx.LoopSleep(ref loopSleep); } } } } }
internal abstract void BeginSendToAll(ServerSocketConnection connection, byte[] buffer, bool includeMe);
internal override void BeginSendToAll(ServerSocketConnection connection, byte[] buffer, bool includeMe) { }
/// <summary> /// Accept callback! /// </summary> internal void BeginAcceptCallback(object state) { SocketAsyncEventArgs e = (SocketAsyncEventArgs)state; if (!Disposed) { SocketListener listener = null; System.Net.Sockets.Socket acceptedSocket = null; BaseSocketConnection connection = null; listener = (SocketListener)e.UserToken; if (e.SocketError == SocketError.Success) { try { //----- Get accepted socket! acceptedSocket = e.AcceptSocket; //----- Adjust buffer size! acceptedSocket.ReceiveBufferSize = Host.SocketBufferSize; acceptedSocket.SendBufferSize = Host.SocketBufferSize; connection = new ServerSocketConnection(Host, listener, acceptedSocket); //----- Initialize! Host.AddSocketConnection(connection); connection.Active = true; Host.InitializeConnection(connection); } catch { if (connection != null) { if (Host != null) { Host.DisposeConnection(connection); Host.RemoveSocketConnection(connection); } connection = null; } } } //---- Continue to accept! SocketAsyncEventArgs e2 = new SocketAsyncEventArgs(); e2.UserToken = listener; e2.Completed += new EventHandler <SocketAsyncEventArgs>(BeginAcceptCallbackAsync); if (!listener.Socket.AcceptAsync(e2)) { BeginAcceptCallbackAsync(this, e2); } } e.UserToken = null; e.Dispose(); e = null; }