Exemplo n.º 1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            client = new WebClient();
            client.Encoding = Encoding.UTF8;

            var info = new OBBInfo();
            info.ip = IPAddress.Loopback.ToString();
            info.notifyPort = Settings.Default.notifyPort;
            info.servicePort = Settings.Default.servicePort;
            info.serviceRoot = Settings.Default.serviceRoot;
             
            if (String.IsNullOrWhiteSpace(info.serviceRoot))
                info.serviceRoot = Environment.CurrentDirectory;

            OBBContext.Current.Info = info;
            OBBContext.Current.Mode = Settings.Default.mode;

            server = new HttpServer.HttpServer();
            var fileModule = new FileModule("/", OBBContext.Current.Info.serviceRoot);
            var myModule = new MyModule();
            fileModule.AddDefaultMimeTypes();
            server.Add(myModule);
            server.Add(fileModule);
            server.Start(IPAddress.Any, OBBContext.Current.Info.servicePort);

            if (OBBContext.Current.IsMaster)
            {
                OBBContext.Current.MasterInfo = OBBContext.Current.Info;

                notifyIcon1.Icon = Resources.master;
                var port = IPAddress.HostToNetworkOrder(OBBContext.Current.Info.servicePort);
                byte[] portData = BitConverter.GetBytes(port);
                notify = new UdpNotify(OBBContext.Current.Info.notifyPort, portData);
            }
            else
            {
                OBBContext.Current.LoadGameConfig();

                notifyIcon1.Icon = Resources.slave;
                notify = new UdpNotify(OBBContext.Current.Info.notifyPort);
                notify.OnData += Notify_OnData;
            }

            this.Text = OBBContext.Current.Mode.ToString();
            notifyIcon1.Text = OBBContext.Current.Mode.ToString();
            refreshTimer.Start();
            button1.Enabled = OBBContext.Current.IsMaster;
        }
Exemplo n.º 2
0
        private void Notify_OnData(System.Net.IPEndPoint endPoint, byte[] data)
        {
            try
            {
                if (OBBContext.Current.IsMaster)
                    return;

                if (OBBContext.Current.MasterInfo != null)
                    return;

                var info = new OBBInfo();
                info.ip = endPoint.Address.ToString();
                info.notifyPort = endPoint.Port;
                info.servicePort = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 0));

                OBBContext.Current.MasterInfo = info;

                Register();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }