//create packets to send public packet[] create_packets(byte[] content, short mode = 1) { //each packet can contain only 4082 bytes if (content.Length <= 4082) { return new packet[] { new packet(mode, get_serie_ID(), 0, (short)content.Length, content.Length, content) } } ; int packetscount = (content.Length - content.Length % 4082) / 4082; if (content.Length % 4082 != 0) { packetscount++; } packet[] packets = new packet[packetscount]; int serieid = get_serie_ID(); for (short i = 0; i < packetscount; i++) { if (i != packetscount - 1) { packets[i] = new packet(mode, serieid, i, 4082, content.Length, content, 4082 * i); } else { packets[i] = new packet(mode, serieid, i, (short)(content.Length - i * 4082), content.Length, content, i * 4082); } } return(packets); }
//todo : create start receiving function that receives bytes and create packets and ... public void start_receive(bool newthread = false) { if (!newthread) { Task.Run(new Action(() => start_receive(true))); return; } try { is_receiving = true; byte[] b = new byte[4096]; while (true) { try { socket.Receive(b); packet p = new packet(b, 0); handle_packets(p); } catch (Exception ex) { //todo : whrow if ex not equals a receive time out exception. if (!ex.Message.Contains("Timeout")) { throw ex; } } } } catch { is_receiving = false; } }
private void handle_packets(packet p) { for (int i = 0; i < _packets.Count; i++) { if (_packets[i][0].seriesID == p.seriesID) { _packets[i].Add(p); check_packet_series(i); return; } } _packets.Add(new List <packet>()); _packets[_packets.Count - 1].Add(p); }