public override void write(ConnectionHandlerContext ctx, object msg) { tempBuf.MakeBufferEmpty(); memStream.Position = 0; ProtoBuf.Serializer.Serialize(memStream, msg); int writeIndex = (int)memStream.Position; tempBuf.setWriteIndex(writeIndex); ctx.fireWrite(tempBuf); }
void ReadMessage() { ByteBuf buf = recvBuf; buf.Clear(); int bytes = 0; try { if (clientSocket.Poll(PollTimeDuration, SelectMode.SelectRead)) { bytes = clientSocket.Available; if (bytes == 0) { connected = false; UnityEngine.Debug.LogError("the connection disconnected!"); pipeLine.fireDeactive(); } else { bytes = clientSocket.Receive(buf.getBuffer()); } } else if (clientSocket.Poll(PollTimeDuration, SelectMode.SelectError)) { pipeLine.fireDeactive(); UnityEngine.Debug.Log("SelectError, the connection disconnected!"); } } catch (SocketException exp) { { UnityEngine.Debug.LogException(exp); } } //UnityEngine.Debug.LogError("Sleep ......"); Thread.Sleep(100); if (bytes > 0) { buf.setWriteIndex(bytes); pipeLine.fireRead(buf); pipeLine.readComplete(); } }
public bool UpdateLogic() { if (!connected) { return(false); } // Check Receive ByteBuf buf = recvBuf; buf.Clear(); int bytes = 0; try { if (clientSocket.Poll(0, SelectMode.SelectRead)) { bytes = clientSocket.Available; if (bytes == 0) { connected = false; pipeLine.fireDeactive(); } else { bytes = clientSocket.Receive(buf.getBuffer()); } } } catch (SocketException exp) { { UnityEngine.Debug.LogException(exp); } } if (bytes > 0) { buf.setWriteIndex(bytes); pipeLine.fireRead(buf); pipeLine.readComplete(); } if (!connected) { return(false); } // 写 线程!! TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - lastWritePingTicks); if (timeSpan.TotalSeconds > PingTime) { lastWritePingTicks = DateTime.Now.Ticks; if (PingProc != null) { PingProc(); } } object data = null; lock (this) { if (dataToWriteList.Count > 0) { data = dataToWriteList.Dequeue(); } } if (data != null) { pipeLine.write(data); } return(connected); }