static void server_OnDataReceived(eSClient client, byte[] buffer) { PacketReader pr = new PacketReader(buffer); Byte ID = pr.ReadByte(); if(ID == 1) { string message = pr.ReadString(); PacketWriter pw = new PacketWriter(); pw.WriteByte(ID); pw.WriteInt32(server.GetIDFromTcpClient(client.GetTcpClient())); pw.WriteString(message); server.SendToAll(pw.ToArray()); Console.WriteLine("Message received from " + client.GetID() + " : " + message); } }
/// <summary> /// Listen for client /// </summary> /// <returns></returns> private async Task ListenAsync() { try { while (true) { TcpClient client = await Listener.AcceptTcpClientAsync(); Int32 id = (Int32)randomID.Next(10000, 99999); eSClient sClient = new eSClient(id, client); byte[] idBuffer; using (PacketWriter pw = new PacketWriter()) { pw.WriteInt32(id); idBuffer = pw.ToArray(); } client.Send(idBuffer); ClientList.Add(sClient); if (OnClientConnected != null) OnClientConnected(sClient); StartHandleClient(sClient); } } catch (Exception ex) { DebugMessage("Failed to listen for new client : " + ex.Message); } finally { Stop(); } }