public OutgoingPacket CreateOutgoing(Socket socket, SocketIOEventTypes socketIOEvent, int id, string name, object[] args) { var memBuffer = BufferPool.Get(256, true); var stream = new BestHTTP.Extensions.BufferPoolMemoryStream(memBuffer, 0, memBuffer.Length, true, true, false, true); var buffer = BufferPool.Get(MsgPackWriter.DEFAULT_BUFFER_SIZE, true); var context = new SerializationContext { Options = SerializationOptions.SuppressTypeInformation, EnumSerializerFactory = (enumType) => new EnumNumberSerializer(enumType)/*, * ExtensionTypeHandler = CustomMessagePackExtensionTypeHandler.Instance*/ }; var writer = new MsgPackWriter(stream, context, buffer); switch (socketIOEvent) { case SocketIOEventTypes.Connect: // No Data | Object if (args != null && args.Length > 0 && args[0] != null) { writer.WriteObjectBegin(id >= 0 ? 4 : 3); } else { writer.WriteObjectBegin(id >= 0 ? 3 : 2); } break; case SocketIOEventTypes.Disconnect: writer.WriteObjectBegin(id > 0 ? 3 : 2); break; case SocketIOEventTypes.Error: writer.WriteObjectBegin(id > 0 ? 4 : 3); break; default: writer.WriteObjectBegin(id >= 0 ? 4 : 3); break; } writer.WriteMember("type"); writer.Write((int)socketIOEvent); writer.WriteMember("nsp"); writer.Write(socket.Namespace); if (id >= 0) { writer.WriteMember("id"); writer.Write(id); } switch (socketIOEvent) { case SocketIOEventTypes.Connect: // No Data | Object if (args != null && args.Length > 0 && args[0] != null) { writer.WriteMember("data"); writer.WriteValue(args[0], args[0].GetType()); } break; case SocketIOEventTypes.Disconnect: // No Data break; case SocketIOEventTypes.Error: writer.WriteMember("data"); // String | Object if (args != null && args.Length > 0) { writer.WriteValue(args[0], args[0].GetType()); } else { writer.WriteObjectBegin(0); writer.WriteObjectEnd(); } break; default: writer.WriteMember("data"); // Array int argCount = (args != null ? args.Length : 0); writer.WriteArrayBegin(!string.IsNullOrEmpty(name) ? 1 + argCount : argCount); if (!string.IsNullOrEmpty(name)) { writer.Write(name); } foreach (var arg in args) { writer.WriteValue(arg, arg.GetType()); } writer.WriteArrayEnd(); break; } writer.WriteObjectEnd(); writer.Flush(); BufferPool.Release(buffer); // get how much bytes got written to the buffer int length = (int)stream.Position; buffer = stream.GetBuffer(); return(new OutgoingPacket { PayloadData = new BufferSegment(buffer, 0, length) }); }
public void BeginContainer(int size) { DecSpace(); space.Push(size); //special 'unspecified' case if (size == -1) { io.Write(-1); } else { io.WriteArrayHeader(size); } }