private DevPacket InternalReceive() { DevClientPacket id = (DevClientPacket)m_reader.ReadByte(); int len = 0; if (!DevPacket.IsOneByte(id)) { if (DevPacket.IsLarge(id)) { len = m_reader.ReadInt32(); } else { len = m_reader.ReadInt16(); } } DevPacket packet; if (len > 0) { packet = new DevPacket((int)id, m_reader.ReadBytes(len)); } else { packet = new DevPacket((int)id); } return(packet); }
private void HandlePacket(DevPacket packet) { if (packet.Data == null) { return; } DevClientPacket id = (DevClientPacket)packet.Id; string data = Encoding.UTF8.GetString(packet.Data); m_behavior.OnPacket(id, data); }
private void InternalSend(DevPacket packet) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); writer.Write((byte)packet.Id); if (packet.Data != null) { writer.Write((short)packet.Data.Length); writer.Write(packet.Data); } byte[] data = ms.ToArray(); m_client.Client.Send(data); }
private void InternalTick() { lock (m_sendQueue) { while (m_sendQueue.Count > 0) { InternalSend(m_sendQueue.Dequeue()); } } while (m_client.Available > 0) { DevPacket packet = InternalReceive(); lock (m_receiveQueue) { m_receiveQueue.Enqueue(packet); } } }
public void Run() { ChatClient = new DevConnection(Server.Address, Server.ChatPort); ChatClient.Send(DevServerPacket.Login, GetJsonLoginString()); m_gameClients = new List <GameClient>(); m_addedClients = new List <GameClient>(); m_removedClients = new List <GameClient>(); m_behavior = new DevBehavior(this); while (ChatClient.IsConnected) { while (ChatClient.HasPacket()) { DevPacket packet = ChatClient.Receive(); HandlePacket(packet); } foreach (GameClient game in m_gameClients) { #if !DEBUG try { game.Tick(); } catch (Exception ex) { game.Connection.Close(); File.WriteAllText("game_" + DateTime.UtcNow.ToString("dd-MM-yyyy-hh-mm-ss") + ".log", ex.ToString()); } #else game.Tick(); #endif } foreach (GameClient game in m_addedClients) { m_gameClients.Add(game); } m_addedClients.Clear(); foreach (GameClient game in m_removedClients) { m_gameClients.Remove(game); } m_removedClients.Clear(); if (HostEnabled) { for (int i = 0; i < m_infClient.Length; ++i) { if (m_infClient[i] == null || !m_gameClients.Contains(m_infClient[i])) { Thread.Sleep(500); ServerInfo server = GetServer(); if (server != null) { m_infClient[i] = new GameClient(this, server, GetRandomDuelName(false)); m_infClient[i].Start(); m_gameClients.Add(m_infClient[i]); } else { HostEnabled = false; } break; } } } if ((DateTime.Now - m_latestfix).TotalSeconds > 30) { ChatClient.Send(DevServerPacket.RefuseDuel); m_latestfix = DateTime.Now; } Thread.Sleep(1); } }
private DevPacket InternalReceive() { DevClientPacket id = (DevClientPacket)m_reader.ReadByte(); int len = 0; if (!DevPacket.IsOneByte(id)) { if (DevPacket.IsLarge(id)) len = m_reader.ReadInt32(); else len = m_reader.ReadInt16(); } DevPacket packet; if (len > 0) packet = new DevPacket((int)id, m_reader.ReadBytes(len)); else packet = new DevPacket((int)id); return packet; }
public void Send(DevPacket packet) { lock (m_sendQueue) m_sendQueue.Enqueue(packet); }
private void InternalSend(DevPacket packet) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); writer.Write((byte)packet.Id); if (packet.Data != null) { writer.Write((short) packet.Data.Length); writer.Write(packet.Data); } byte[] data = ms.ToArray(); m_client.Client.Send(data); }