public void StartServer() { BindIps(); new Action(() => { while (true) { IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint remote = (EndPoint)sender; int len = 0; byte[] buffer = null; int offset = 0; var bufferindex = _buffermanager.GetBuffer(); if (bufferindex == -1) { throw new Exception("没有可用的接收缓存"); } if (bufferindex != -1) { buffer = _buffermanager.Buffer; offset = _buffermanager.GetOffset(bufferindex); len = __s.ReceiveFrom(buffer, offset, MAX_PACKAGE_LEN, SocketFlags.None, ref remote); } if (len > 8) { OnSocket(remote, bufferindex, len); } else { try { var locker = GetSendMsgLocker((IPEndPoint)remote); if (locker != null) { var bagid = BitConverter.ToInt64(buffer, offset); //Console.WriteLine(Environment.TickCount + ":收确认:" + segmentid); if (locker.BagId == bagid) { locker.Set(); } } } finally { _buffermanager.RealseBuffer(bufferindex); } } } }).BeginInvoke(null, null); }
public MemoryStreamWriter(BufferPollManager bufferpollmanger) { var bufferindex = bufferpollmanger.GetBuffer(); if (bufferindex == -1) { _ms = new MemoryStream(); } else { _bufferPollManager = bufferpollmanger; Bufferindex = bufferindex; _bufferoffset = bufferpollmanger.GetOffset(bufferindex); _ms = new MemoryStream(bufferpollmanger.Buffer, (int)_bufferoffset, bufferpollmanger.BlockSize); } //_ms.Position = 4; _ms.Position = 8; }
private void SetBuffer(IOCPSocketAsyncEventArgs e, int offset, int len) { if (offset > 0) { offset = (e.BufferIndex == -1 ? 0 : _bufferpoll.GetOffset(e.BufferIndex)) + offset; e.SetBuffer(offset, len); return; } byte[] buf = null; if (len <= _bufferpoll.BlockSize) { if (e.BufferIndex == -1) { var newindex = _bufferpoll.GetBuffer(); if (newindex == -1) { buf = new byte[len]; } else { e.BufferIndex = newindex; } } } else { if (e.BufferIndex > 0) { _bufferpoll.RealseBuffer(e.BufferIndex); e.BufferIndex = -1; } buf = new byte[len]; } if (buf != null) { e.SetBuffer(buf, offset, len); } else { e.SetBuffer(_bufferpoll.Buffer, _bufferpoll.GetOffset(e.BufferIndex) + offset, len); } }