private void ReceiveCallback(IAsyncResult ar) { StateObject state = null; int bytesRead = 0; try { state = (StateObject)ar.AsyncState; bytesRead = m_Connect.EndReceive(ar); } catch (SocketException se) { PostError(4, se.ErrorCode); return; } if (bytesRead > 0) { ByteBuffer ret = null; byte[] tempReceive = new byte[bytesRead]; Array.Copy(state.buffer, tempReceive, bytesRead); state.byteList.AddRange(tempReceive); ByteBuffer data = new ByteBuffer(state.byteList.ToArray()); int len = -1; while (data.available() > 0) { if (-1 == len) { if (data.available() < 4) { break; } len = data.readInt(); } if (data.available() < len) { break; } else if (data.available() == len) { ret = new ByteBuffer(data.readBytes(len)); state.byteList.RemoveRange(0, len + 4); len = -1; ServiceManager.PostMessageShort(this, ret); return; } else { Debug.LogError("Short connection too long"); return; } } Array.Clear(state.buffer, 0, state.buffer.Length); m_Connect.BeginReceive(state.buffer, 0, StateObject.bufferSize, 0, new AsyncCallback(ReceiveCallback), state); } else { //SocketHandler.socketResponse += "\nBase:bytesRead <= 0"; } }
void OnDestroy() { m_Inst = null; }