public override void write(ConnectionHandlerContext ctx, object msg) { if (msg is ByteBuf) { tempBuf.Clear(); ByteBuf buf = (ByteBuf)msg; if (LengthBytes == 2) { ushort len = (ushort)buf.remainBytes(); tempBuf.writeUInt16(len); } else if (LengthBytes == 4) { uint len = (uint)buf.remainBytes(); tempBuf.writeUInt32(len); } else if (LengthBytes == 8) { ulong len = (ulong)buf.remainBytes(); tempBuf.writeUInt64(len); } tempBuf.writeBytes(buf); ctx.fireWrite(tempBuf); } }
public void setPreHandler(ConnectionHandlerContext ctx) { ctx.next = this; ctx.pre = this.pre; if (this.pre != null) { this.pre.next = ctx; } this.pre = ctx; }
public void setNextHandler(ConnectionHandlerContext ctx) { ctx.next = this.next; ctx.pre = this; if (this.next != null) { this.next.pre = ctx; } this.next = ctx; }
public void fireWrite(Object message) { ConnectionHandlerContext ctx = nextOutboundContext(); if (ctx == null) { return; } ctx.invokeWrite(message); }
public void fireDeactive() { ConnectionHandlerContext ctx = tail; while (ctx != null) { ctx.Deactive(); ctx = ctx.getPreHandler(); } }
public void fireActive() { ConnectionHandlerContext ctx = header; while (ctx != null) { ctx.Active(); ctx = ctx.getNextHandler(); } }
public ConnectionHandlerContext nextInboundContext() { ConnectionHandlerContext ctx = this; do { ctx = ctx.next; } while (ctx != null && !ctx.isInbound); return(ctx); }
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); }
public void fireRead(Object message) { ConnectionHandlerContext ctx = nextInboundContext(); if (ctx == null) { return; } // System.Console.WriteLine ("receive bytes fire read"); ctx.invokeRead(message); }
public override void read(ConnectionHandlerContext ctx, object msg) { ByteBuf buf = (ByteBuf)msg; //System.Console.WriteLine("receive bytes " + buf.remainBytes()); MemoryStream ms = new MemoryStream(buf.getBuffer(), buf.getReaderIndex(), buf.remainBytes()); T obj = ProtoBuf.Serializer.Deserialize <T>(ms); buf.skipReaderIndex((int)ms.Position); if (receiveObjFunc != null) { receiveObjFunc(obj); } }
public override void deactive(ConnectionHandlerContext ctx) { }
public override void deactive(ConnectionHandlerContext ctx) { System.Console.WriteLine("Connection is Closed!"); }
public override void read(ConnectionHandlerContext ctx, object msg) { ByteBuf inputBuf = (ByteBuf)msg; System.Console.WriteLine(inputBuf.ToString()); }
public void addLast(string name, ConnectionHandler handler) { ConnectionHandlerContext ctx = new ConnectionHandlerContext(name, handler); tail.setPreHandler(ctx); }
public virtual void read(ConnectionHandlerContext ctx, object msg) { throw new NotImplementedException(); }
public void readComplete() { ConnectionHandlerContext ctx = nextInboundContext(); ctx.invokeReadComplete(); }
public virtual void deactive(ConnectionHandlerContext ctx) { }
public override void read(ConnectionHandlerContext ctx, object msg) { ByteBuf inputBuf = (ByteBuf)msg; //UnityEngine.Debug.LogError("Read the Raw Message!!!! size = " + inputBuf.remainBytes()); while (true) { // UnityEngine.Debug.LogError("Loop Read Raw Message!!!! size = " + inputBuf.remainBytes()); if (targetSize <= 0) { targetSize = 0; // The target size is not read complete int nowsize = receivedBuf.remainBytes(); if (nowsize < LengthBytes) { int needSizeForLength = LengthBytes - nowsize; for (int i = 0; i < needSizeForLength; ++i) { if (inputBuf.remainBytes() > 0) { receivedBuf.writeByte(inputBuf.readByte()); } else { break; } } } nowsize = receivedBuf.remainBytes(); // System.Console.WriteLine("nowSize " + nowsize); if (nowsize >= LengthBytes) { if (LengthBytes == 2) { targetSize = receivedBuf.readUInt16(); } else if (LengthBytes == 4) { targetSize = (int)receivedBuf.readUInt32(); } else if (LengthBytes == 8) { targetSize = (int)receivedBuf.readUInt64(); } else { throw new Exception("Big Errror!!!!!!! , not engouh size bytes!!!"); } } else { // note read the length complete ,wait for more bytes if (inputBuf.remainBytes() > 0) { throw new Exception("Big Errror!!!!!!!"); } return; } } // the targetSize must be ready if (targetSize <= 0) { throw new Exception("the target size ERROR,that's must be a bug!"); } //receivedBuf.writeBytes(inputBuf); for (int i = 0; i < targetSize && inputBuf.remainBytes() > 0; ++i) { receivedBuf.writeByte(inputBuf.readByte()); } // UnityEngine.Debug.LogError("write the Receive Message!!!! now size " + receivedBuf.remainBytes() + "target Size " + targetSize); if (receivedBuf.remainBytes() >= targetSize) { // UnityEngine.Debug.LogError("write the Receive Message!!!! Begin"); ctx.fireRead(receivedBuf); // UnityEngine.Debug.LogError("write the Receive Message!!!! End"); receivedBuf.DiscardReadedBytes(); // UnityEngine.Debug.LogError("Deal after Receive Message!!!! now size " + receivedBuf.remainBytes()); targetSize = 0; } // System.Console.WriteLine("TargetSize " + targetSize); if (inputBuf.remainBytes() == 0) { break; } } }
public virtual void readComplete(ConnectionHandlerContext ctx) { }
public void addFirst(string name, ConnectionHandler handler) { ConnectionHandlerContext ctx = new ConnectionHandlerContext(name, handler); header.setNextHandler(ctx); }