Beispiel #1
0
        private void button_connect_Click(object sender, EventArgs e)
        {
            if (client != null)
            {
                client.Close();
            }
            else
            {
                client = new light.net.Client();
            }

            System.Random r = new System.Random();
            user_index = r.Next(1, 999);

            if (false == client.Connect(textBox_ip.Text, int.Parse(textBox_port.Text)))
            {
                log("cannot connect server. server[" + textBox_ip.Text + ":" + textBox_port.Text + "]");
                return;
            }

            // 타이머 생성 및 시작
            timer          = new System.Windows.Forms.Timer();
            timer.Interval = 1;
            timer.Tick    += new EventHandler(timer_Elapsed);
            timer.Start();

            log("connected server. server[" + textBox_ip.Text + ":" + textBox_port.Text + "]");
        }
        void relay_start_callback(JObject json)
        {
            if (null != json)
            {
                relay_info = json;
                string[] address = relay_info["server_address"].ToString().Split(':');

                if (client != null)
                {
                    if (true == client.IsConnected())
                    {
                        log("already connected server. server[" + address[0] + ":" + address[1] + "]");
                        return;
                    }
                }
                else
                {
                    client = new light.net.Client();
                }

                if (false == client.Connect(address[0], int.Parse(address[1])))
                {
                    log("cannot connect server. server[" + address[0] + ":" + address[1] + "]");
                    return;
                }

                log("connect server. server[" + address[0] + ":" + address[1] + "]");

                JArray array = JArray.Parse(relay_info["members"].ToString());
                foreach (var member in array)
                {
                    if (member["id"].ToString() == user_index.ToString())
                    {
                        FlatBuffers.FlatBufferBuilder builder = new FlatBuffers.FlatBufferBuilder(1);
                        var offset = PACKET.REQ_JOIN.CreateREQ_JOIN(builder, builder.CreateString(login_info["UserID"].ToString()), builder.CreateString(member["hash"].ToString()), user_index);
                        builder.Finish(offset.Value);

                        if (false == client.Send((int)PACKET_C2S.JOIN, builder.SizedByteArray(), uint.Parse(relay_info["roomnum"].ToString())))
                        {
                            log("can't enter.");
                        }
                        return;
                    }
                }

                log("not find my information from relay/start.");
            }
            else
            {
                log("failed relay_start.");
            }
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (packet_timer != null)
            {
                packet_timer.Stop();
                packet_timer = null;
            }

            if (client != null)
            {
                client.Close();
                client = null;
            }

            base.OnFormClosing(e);
        }