public bool HandleBundleMessage(FactorioNetMessage message) { if (needCount == bundleMessages.Count) { return(true); } if (message.isLastfragment) { needCount = message.fragmentId + 1; } bundleMessages.Add(message.fragmentId, message); return(needCount == bundleMessages.Count()); }
static void SendMessage(ClientMessage message) { currentMessageId++; byte[] messagedata = message.GetBytes(); FactorioNetMessage[] messages; if (messagedata.Length > 500) { messages = new FactorioNetMessage[messagedata.Length / 500 + 1]; for (int i = 0; i < messagedata.Length / 500 + 1; i++) { messages[i] = new FactorioNetMessage(); messages[i].isFragmented = true; messages[i].isLastfragment = (i == messagedata.Length / 500); messages[i].fragmentId = (short)(i + 1); messages[i].type = message.GetMessageType(); if (messages[i].isLastfragment) { messages[i].packetBytes = new byte[messagedata.Length % 500]; } else { messages[i].packetBytes = new byte[500]; } Array.Copy(messagedata, messages[i].packetBytes, messages[i].packetBytes.Length); } } else { messages = new FactorioNetMessage[1]; messages[0] = new FactorioNetMessage(); messages[0].type = message.GetMessageType(); messages[0].packetBytes = message.GetBytes(); } foreach (FactorioNetMessage msg in messages) { msg.currentMessageId = currentMessageId; msg.lastServerMessageId = (short)lastServerMessageId; socket.Send(msg.GetPacket()); } }
static void HandlePacket(byte[] packet) { FactorioNetMessage netmsg = new FactorioNetMessage(packet); lastServerMessageId = netmsg.messageId; if (netmsg.isFragmented) { if (!fragmentedpackets.ContainsKey(netmsg.messageId)) { fragmentedpackets.Add(netmsg.messageId, new FactorioNetMessageBundle()); } if (fragmentedpackets[netmsg.messageId].HandleBundleMessage(netmsg)) { HandleMessage(netmsg.type, fragmentedpackets[netmsg.messageId].GetOverallMessage().packetBytes); fragmentedpackets.Remove(netmsg.messageId); } } else { HandleMessage(netmsg.type, netmsg.packetBytes); } }