internal static void InicializujPackety(PacketReader pr, PacketWriter pw, Client client) { //mám rád reflexi :) foreach (Type t in Assembly.GetAssembly(typeof(Packeta)).GetTypes()) { if (t.IsSubclassOf(typeof(Packeta))) { ConstructorInfo c = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(PacketReader), typeof(PacketWriter), typeof(Client) }, null); Packeta pac = c.Invoke(new object[] { pr, pw, client }) as Packeta; m_Packety[(int)pac.ID] = pac; } } }
internal void OdesliPacketu(PacketID id) { Packeta pac = Packety.ZiskejPacketu(id); if (pac == null) { ZalogujChybu("Neznámá packeta k odeslání: " + id.ToString()); UkonciSpojeni(); return; } if (OdeslanaPacketaUdalost != null) { OdeslanaPacketaUdalost(id); } m_RizeniOdesilani.WaitOne(); try { pac.ZapisPacketu(); } catch (IndexOutOfRangeException ex) { ZalogujChybu(ex.ToString()); UkonciSpojeni(); return; } try { m_Socket.BeginSend(m_Writer.Packeta, 0, m_Writer.Delka, SocketFlags.None, m_OdesliData, null); } catch (SocketException ex) { ZalogujChybu(ex.ToString()); UkonciSpojeni(); } catch (ObjectDisposedException) { } }
private void PrectiData(IAsyncResult ar) { try { m_PocetPrijatychBytu = m_Socket.EndReceive(ar); } catch (SocketException ex) { ZalogujChybu(ex.ToString()); UkonciSpojeni(); } catch (ObjectDisposedException) { return; } catch (ArgumentException) { return; } if (m_PocetPrijatychBytu > 0) { NastavAktvitu(); if (!m_PacketBuffer.VlozData(m_ReadBuffer, m_PocetPrijatychBytu)) { ZalogujChybu("Přetekl PacketBuffer"); UkonciSpojeni(); return; } } else { // ZalogujChybu("Obdržena data o nulové délce"); // UkonciSpojeni(); return; } while (m_PacketBuffer.ObsahujeData) { switch (m_PacketBuffer.ZiskejPacketu(ref m_DelkaPakety, ref m_PocatecniPozice)) { case StavPackety.NeplatnaHlavicka: { ZalogujChybu("Neplatná hlavička (!= 0xFF)"); UkonciSpojeni(); return; } case StavPackety.PrilisKratkaPacketa: { ZalogujChybu("Příliš krátká packeta"); UkonciSpojeni(); return; } case StavPackety.NedostatekDat: { try { m_Socket.BeginReceive(m_ReadBuffer, 0, m_ReadBuffer.Length, SocketFlags.None, m_PrectiData, null); } catch (SocketException ex) { ZalogujChybu(ex.ToString()); UkonciSpojeni(); } return; } case StavPackety.OK: { Packeta pc = Packety.ZiskejPacketu(m_PacketBuffer.Packeta[m_PocatecniPozice + 1]); if (pc == null) { ZalogujChybu("Neznamá packeta ID: " + m_PacketBuffer.Packeta[m_PocatecniPozice + 1].ToString("X2")); UkonciSpojeni(); return; } m_Reader.NactiPacketu(m_DelkaPakety, m_PocatecniPozice); try { pc.PrectiPacketu(); if (PrijataPacketaUdalost != null) { PrijataPacketaUdalost(pc.ID); } } catch (Exception ex) { ZalogujChybu(ex.ToString()); UkonciSpojeni(); return; } break; } } } try { m_Socket.BeginReceive(m_ReadBuffer, 0, m_ReadBuffer.Length, SocketFlags.None, m_PrectiData, null); } catch (SocketException ex) { ZalogujChybu(ex.ToString()); UkonciSpojeni(); } }