public NetPackage ReadPackage(NetPackage package) { int length = br.ReadInt32(); int id = br.ReadInt32(); byte[] data = br.ReadBytes(length - 8); package.Deserialize(data); return(package); }
public void DoWork() { while (true) { // Debug.Log(this is ClientConnectionWorker ? "ServerDetailWorker:work" : "ClientDetailWorker"); int length = 0; //这里要检测是否有一段可读数据的时候只读一部分仍然检测为可读 if (conn.CanRead()) { length = conn.Read(buffer); if (length > 0) { readBuffer.Write(buffer, 0, length); } } if (conn.CanWrite()) { byte[] tempBuffer = writeBuffer.GetDataBuffer(); int readPos = writeBuffer.GetReadPos(); length = writeBuffer.GetDataLength(); if (length > 0) { int writeLen = conn.Write(tempBuffer, readPos, length); if (length > 0) { writeBuffer.SetReadPos(length, SeekOrigin.Current); } } } if (conn.HasError()) { //Debug.Log ("HasError~~~~~~~~~~~"); stop = true; break; } //从buffer中读package while (!stop) { int dataLength = readBuffer.GetDataLength(); if (dataLength < 8) { break; } int packageLeng = readBuffer.ReadInt(); System.Diagnostics.Debug.Assert(packageLeng > 0); if (dataLength < packageLeng) { readBuffer.SetReadPos(-4, SeekOrigin.Current); break; } int packageId = readBuffer.ReadInt(); byte[] packageData = readBuffer.ReadBytes(packageLeng - 8); NetPackage package = PackageFactory.GetPackage(packageId); System.Diagnostics.Debug.Assert(package != null); package.Deserialize(packageData); package.DeserializeRemoteEndPoint(conn.RemoteEndPoint); lock (readPackageQueue) { readPackageQueue.Enqueue(package); } } if (stop) { break; } lock (writePackageQueue) { while (writePackageQueue.Count > 0) { NetPackage package = writePackageQueue.Dequeue(); lock (package) { package.Serialize(); MemoryStream ms = package.GetMemoryStream(); writeBuffer.Write(ms.GetBuffer(), 0, (int)ms.Length); } } } Thread.Sleep(1); } }