public void Send(int nID, byte[] data) { Peer c = GetPeer(nID); if (c == null) { return; } byte[] len = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(data.Length)); byte[] buff = new byte[len.Length + data.Length]; Buffer.BlockCopy(len, 0, buff, 0, len.Length); Buffer.BlockCopy(data, 0, buff, len.Length, data.Length); SendBuff s = new SendBuff(); s.peer = c; s.buff = buff; sendbuffers.Enqueue(s); }
public void Pump() { while (server.Pending()) { try { long mem = GetTotalMemory(); if (mem > maxMemoryUsage) { break; } if (peers.Count >= count) { break; } server.BeginAcceptTcpClient(delegate(IAsyncResult ar) { try { long memtest = GetTotalMemory(); if (memtest > maxMemoryUsage) { server.EndAcceptTcpClient(ar).Client.Close(); return; } Peer c = new Peer(server.EndAcceptTcpClient(ar)); c.client.LingerState = new LingerOption(false, 0); c.client.NoDelay = true; c.client.Client.LingerState = new LingerOption(false, 0); c.client.Client.NoDelay = true; c.client.Client.Blocking = false; c.client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); locker.AcquireWriterLock(Timeout.Infinite); peers[c._id] = c; locker.ReleaseWriterLock(); f d = delegate() { handler.OnAccept((ushort)c._id); }; op.Enqueue(d); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } }, null); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } } if (peers.Count > 0) { Peer[] clients; locker.AcquireReaderLock(Timeout.Infinite); clients = new Peer[peers.Count]; peers.Values.CopyTo(clients, 0); locker.ReleaseReaderLock(); DateTime now = DateTime.Now; for (int i = 0; i < clients.Length; ++i) { Peer peer = clients[i]; bool closed = false; do { if (!peer.Connected) { closed = true; break; } if (now - peer._lastdisconnectcheck >= disconnectCheckSpan) { if (peer.client.Client.Poll(0, SelectMode.SelectWrite)) { byte[] buff = new byte[0]; try { peer.client.Client.Send(buff); } catch (SocketException se) { if (se.SocketErrorCode == SocketError.ConnectionReset) { } else if (se.SocketErrorCode == SocketError.ConnectionAborted) { } else { Debug.WriteLine(se.SocketErrorCode.ToString()); Debug.WriteLine(se.ErrorCode); } closed = true; break; } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); closed = true; break; } } } if (peer.client.Client.Poll(0, SelectMode.SelectError)) { Debug.WriteLine("ERROR CLOSE"); closed = true; break; } } while (false); if (closed) { Close((ushort)peer._id); } } } while (sendbuffers.Count > 0) { SendBuff s = sendbuffers.Dequeue(); if (!s.peer.Connected) { continue; } if (s.peer.client.Client.Poll(0, SelectMode.SelectWrite)) { try { s.peer.client.Client.BeginSend(s.buff, 0, s.buff.Length, SocketFlags.None, delegate(IAsyncResult ar) { try { s.peer.client.Client.EndSend(ar); } catch (ObjectDisposedException) { } catch (SocketException) { Close((ushort)s.peer._id); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); Close((ushort)s.peer._id); } }, null); } catch (SocketException) { Close((ushort)s.peer._id); } catch (ObjectDisposedException) { } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); Close((ushort)s.peer._id); } } } while (op.Count > 0) { (op.Dequeue() as f)(); } }