/* Copyright 2012 Cedric Van Goethem This file is part of GM TCP Shout. GM TCP Shout is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. GM TCP Shout is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GM TCP Shout. If not, see http://www.gnu.org/licenses/. */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.IO; namespace GMMessage { public partial class Form1 : Form { Socket sock; byte[] buffer; private NotifyIcon trayIcon; private ContextMenu trayMenu; public Form1() { InitializeComponent(); trayMenu = new ContextMenu(); trayMenu.MenuItems.Add("Show", OnShow); trayMenu.MenuItems.Add("Exit", OnExit); // Create a tray icon. In this example we use a // standard system icon for simplicity, but you // can of course use your own custom icon too. trayIcon = new NotifyIcon(); trayIcon.Text = "GM Shout"; trayIcon.Icon = this.Icon; // Add menu to tray icon and show it. trayIcon.ContextMenu = trayMenu; trayIcon.Visible = false; } private void OnExit(object sender, EventArgs e) { Application.Exit(); } private void OnShow(object sender, EventArgs e) { this.Show(); trayIcon.Visible = false; } private void Form1_Load(object sender, EventArgs e) { lbltimer.ForeColor = Color.Red; } private void button1_Click(object sender, EventArgs e) { if (bxAuto.Checked) { bxAuto.Enabled = false; button1.Enabled = false; txtMessage.Enabled = false; timer1.Interval = (int.Parse(txtInterval.Text)) * 1000; txtInterval.Enabled = false; timer1.Enabled = true; btnStop.Enabled = true; lbltimer.Text = "WORKING"; lbltimer.ForeColor = Color.Green; timer1.Start(); } else { SendMessage(); } } void SendMessage() { try { IPEndPoint point = new IPEndPoint(IPAddress.Parse(txtIP.Text), 9116); sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Connect(point); txtStatus.Text = "Connected!"; sock.BeginReceive(buffer = new byte[1], 0, 1, SocketFlags.None, new AsyncCallback(ParseHeader), sock); } catch (Exception ex) { txtStatus.Text = "Could not connect to server."; } } void ParseHeader(IAsyncResult ar) { try { int len = sock.EndReceive(ar); if (len <= 0) { OnClose(); return; } byte size = buffer[0]; sock.BeginReceive(buffer = new byte[size], 0, size, SocketFlags.None, new AsyncCallback(ParseData), sock); } catch { OnClose(); } } void ParseData(IAsyncResult ar) { try { int len = sock.EndReceive(ar); if (len <= 0) { OnClose(); return; } byte opcode = buffer[0]; switch (opcode) { case 1: //09 02 08 00 05 08 00 00 0D 00 sock.Send(new byte[] { 0x9, 0x2, 0x8, 0x0, 0x5, 0x8, 0x00, 0x00, 0x0d, 0x00 }); //sayHello break; case 3: MemoryStream stream = new MemoryStream(); BinaryWriter lolz = new BinaryWriter(stream); lolz.Write((byte)(txtMessage.Text.Length + 4)); lolz.Write((byte)0x10); lolz.Write((byte)0x20); lolz.Write((byte)0x3C); lolz.Write(System.Text.Encoding.ASCII.GetBytes(txtMessage.Text)); lolz.Write((byte)0); byte[] array = new byte[lolz.BaseStream.Length]; lolz.BaseStream.Position = 0; lolz.BaseStream.Read(array, 0, (int)lolz.BaseStream.Length); sock.Send(array); txtStatus.Text = "Message sent!"; //sock.Close(); break; } sock.BeginReceive(buffer = new byte[1], 0, 1, SocketFlags.None, new AsyncCallback(ParseHeader), sock); } catch { OnClose(); } } private void OnClose() { try { sock.Close(); } catch { } } private void timer1_Tick(object sender, EventArgs e) { try { if (sock != null) sock.Close(); } catch { } if (txtMessage.Text.Length < 5) { btnStop_Click(null, null); return; } SendMessage(); } private void btnStop_Click(object sender, EventArgs e) { timer1.Stop(); timer1.Enabled = false; button1.Enabled = true; bxAuto.Enabled = true; txtMessage.Enabled = true; btnStop.Enabled = false; txtInterval.Enabled = true; lbltimer.Text = "DISABLED"; lbltimer.ForeColor = Color.Red; } private void button2_Click(object sender, EventArgs e) { this.Hide(); trayIcon.Visible = true; } private void label3_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("http://www.forum.kryptodev.com"); } } }