Пример #1
0
        public void ClientSend(byte[] byteArray)
        {
            try
            {
                socket.Send(byteArray);

                string str = "Send byte[], Lenght = " + byteArray.Length;
                Logger.socket.Info(str);

                APISocksEventArgs mye = new APISocksEventArgs();
                mye.msg = str;

                if (Sending != null)
                {
                    Sending(this, mye);
                }
            }
            catch (Exception ex)
            {
                Logger.Bomb.Error(ex.ToString());

                StopRecive = true;
                m_Status   = SocketStatus.DisConnected;

                APISocksEventArgs mye = new APISocksEventArgs();
                mye.msg    = "";
                mye.status = SocketStatus.DisConnected;
                if (StatusChange != null)
                {
                    StatusChange(this, mye);
                }

                Connect();
            }
        }
Пример #2
0
        public void ConnectToServer()
        {
            try
            {
                Logger.Bomb.Info("Connect to " + IP + ":" + Port.ToString());


                while (true)
                {
                    try
                    {
                        socket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        m_Status = SocketStatus.DisConnected;
                        socket.Connect(IP, Port); // 1.設定 IP:Port 2.連線至伺服器
                        if (socket.Connected)
                        {
                            m_Status = SocketStatus.Connected;


                            APISocksEventArgs mye = new APISocksEventArgs();
                            mye.msg    = "";
                            mye.status = SocketStatus.Connected;
                            if (StatusChange != null)
                            {
                                StatusChange(this, mye);
                            }


                            StopRecive = false;

                            ReciveThread = new Thread(Reciver);
                            ReciveThread.IsBackground = true;
                            ReciveThread.Name         = "Client " + IP.ToString();
                            ReciveThread.Start();

                            isConnecting = false;
                            break;
                        }

                        Thread.Sleep(3000);
                    }
                    catch (Exception ex)
                    {
                        StopRecive = true;
                        Thread.Sleep(3000);
                    }

                    if (StopConnect)
                    {
                        isConnecting = false;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                isConnecting = false;
                Logger.Bomb.Error(ex.ToString());
            }
        }
Пример #3
0
        private void ServerReciver(object obj)
        {
            Socket clsSocket = (Socket)obj;

            try
            {
                byte[] data   = new byte[100];
                int    length = 0;
                string str;

                while (true)
                {
                    length = clsSocket.Receive(data);

                    if (length != 0)
                    {
                        str = System.Text.Encoding.Default.GetString(data);

                        str = str.Substring(0, length);

                        APISocksEventArgs mye = new APISocksEventArgs();
                        mye.msg = str;
                        if (Receiving != null)
                        {
                            Receiving(this, mye);
                        }

                        Logger.Bomb.Info("Recive :" + str);
                    }
                    else
                    {
                        break;
                    }
                }

                APISocksEventArgs mye2 = new APISocksEventArgs();
                mye2.msg    = "";
                mye2.status = SocketStatus.DisConnected;
                if (StatusChange != null)
                {
                    StatusChange(this, mye2);
                }

                clsSocket.Disconnect(true);
                clsSocket.Close();
                CheckConnectSocket();
            }
            catch (Exception ex)
            {
                APISocksEventArgs mye2 = new APISocksEventArgs();
                mye2.msg    = "";
                mye2.status = SocketStatus.DisConnected;
                if (StatusChange != null)
                {
                    StatusChange(this, mye2);
                }

                CheckConnectSocket();
            }
        }
Пример #4
0
        public void ServertSend(string str)
        {
            try
            {
                byte[] byteArray;
                bool   SendOK = false;

                byteArray = System.Text.Encoding.Default.GetBytes(str);
                int i = 0;
                for (i = 0; i < Connected_Sockets.Count; i++)
                {
                    try
                    {
                        // if (Connected_Sockets[i].Connected)
                        {
                            Connected_Sockets[i].Send(byteArray);
                            SendOK = true;
                        }
                    }
                    catch
                    {
                    }
                }


                if (SendOK)
                {
                    Logger.socket.Info("Send :" + str);
                    APISocksEventArgs mye = new APISocksEventArgs();
                    mye.msg = str;

                    if (Sending != null)
                    {
                        Sending(this, mye);
                    }
                }
            }
            catch (Exception ex)
            {
                //Logger.socket.Error(ex.ToString());

                //m_Status = SocketStatus.DisConnected;

                //MyEventArgs mye = new MyEventArgs();
                //mye.msg = "";
                //mye.status = SocketStatus.DisConnected;
                //if (StatusChange != null)
                //    StatusChange(this, mye);
            }
        }
Пример #5
0
        public void StartListen()
        {
            try
            {
                Socket cls_client = null;

                while (!ServerStop)
                {
                    cls_client = socket.Accept();

                    Connected_Sockets.Add(cls_client);

                    ParameterizedThreadStart thrServerRecive;
                    thrServerRecive = new ParameterizedThreadStart(ServerReciver);
                    Thread thr1 = new Thread(thrServerRecive);
                    thr1.Start(cls_client);

                    byte[] byteArray;

                    byteArray = System.Text.Encoding.Default.GetBytes("Hi");

                    cls_client.Send(byteArray);
                    CheckConnectSocket();

                    APISocksEventArgs mye = new APISocksEventArgs();
                    mye.msg    = "";
                    mye.status = SocketStatus.Connected;
                    if (StatusChange != null)
                    {
                        StatusChange(this, mye);
                    }

                    Logger.Bomb.Info("Client Connect");
                }
            }
            catch (Exception ex)
            {
                Logger.Bomb.Error(ex.ToString());
            }
        }
Пример #6
0
 private void StatusChange(object sender, APISocket.APISocksEventArgs e)
 {
     try
     {
         this.Invoke(new Action(() =>
         {
             if (e.status == APISocket.APISocks.SocketStatus.Connected)
             {
                 PanelACP.BackColor = Color.FromArgb(0, 255, 0);
             }
             if (e.status == APISocket.APISocks.SocketStatus.DisConnected)
             {
                 PanelACP.BackColor = Color.FromArgb(255, 0, 0);
             }
         }
                                ));
     }
     catch (Exception ex)
     {
         Logger.Bomb.Error(ex.ToString());
     }
 }
Пример #7
0
        public void ClientSend(string str)
        {
            try
            {
                byte[] byteArray;

                byteArray = System.Text.Encoding.Default.GetBytes(str);

                socket.Send(byteArray);

                Logger.Bomb.Info("Send :" + str);

                APISocksEventArgs mye = new APISocksEventArgs();
                mye.msg = str;

                if (Sending != null)
                {
                    Sending(this, mye);
                }
            }
            catch (Exception ex)
            {
                Logger.Bomb.Error(ex.ToString());

                StopRecive = true;
                m_Status   = SocketStatus.DisConnected;

                APISocksEventArgs mye = new APISocksEventArgs();
                mye.msg    = "";
                mye.status = SocketStatus.DisConnected;
                if (StatusChange != null)
                {
                    StatusChange(this, mye);
                }

                Connect();
            }
        }
Пример #8
0
        private void Reciving(object sender, APISocket.APISocksEventArgs e)
        {
            try
            {
                this.Invoke(new Action(() =>
                {
                    txtACPRecive.Text = e.msg;
                    //InspectCenter(e.msg);
                }
                                       ));


                if (e.msg.Length > 5)
                {
                    string[] sp = e.msg.Split(',');

                    if (sp.Length >= 1)
                    {
                        if (sp[0] == "GETBOMB")
                        {
                            if (sp.Length == 2)
                            {
                                BLID     = sp[1];
                                isGotBom = true;
                                GetBomb(m_BombInfo.Fab, m_BombInfo.EQ, sp[1]);
                            }
                        }
                        else if (sp[0] == "BOMBRESULT")
                        {
                            if (isGotBom)
                            {
                                //BOMBRESULT,count,Def1X,def1y,.....
                                if (sp.Length >= 2)
                                {
                                    int count            = int.Parse(sp[1]);
                                    List <BombPoint> bps = new List <BombPoint>();

                                    int i, j;

                                    for (i = 0; i < count; i++)
                                    {
                                        BombPoint bp = new BombPoint();
                                        bp.X = (int)(float.Parse(sp[2 + i * 2]) * (float)m_BombInfo.ResolutionX);
                                        bp.Y = (int)(float.Parse(sp[2 + i * 2 + 1]) * (float)m_BombInfo.ResolutionY);

                                        bps.Add(bp);
                                    }
                                    int match = 0;

                                    double xx, yy;
                                    for (i = 0; i < m_BombInfo.Bombs[BombIndex].Count; i++)
                                    {
                                        for (j = 0; j < bps.Count; j++)
                                        {
                                            xx = m_BombInfo.Bombs[BombIndex][i].X - bps[j].X;
                                            yy = m_BombInfo.Bombs[BombIndex][i].Y - bps[j].Y;

                                            if (Math.Sqrt(xx * xx + yy * yy) < 20)
                                            {
                                                match++;
                                            }
                                        }
                                    }
                                    string result = "NG";

                                    if (match == m_BombInfo.Bombs[BombIndex].Count)
                                    {
                                        result = "OK";
                                    }

                                    string xyResult = "";

                                    for (i = 0; i < bps.Count; i++)
                                    {
                                        var bp = bps[i];

                                        xyResult += GetZone(bp, m_BombInfo.ResolutionX, m_BombInfo.ResolutionY).ToString()
                                                    + "(" + bp.X + "," + bp.Y + ")";

                                        if (i != bps.Count - 1)
                                        {
                                            xyResult += ";";
                                        }
                                    }


                                    string strBombRep = "OK";
                                    if (!isTestMode)
                                    {
                                        strBombRep = apibomb.GetBombResult(m_BombInfo.Fab, m_BombInfo.EQ, BLID, "White", xyResult, result);
                                    }

                                    Logger.socket.Info("Send to Web Service: " + m_BombInfo.Fab + "," + m_BombInfo.EQ + "," + BLID + "," + "White" + xyResult + "," + result);

                                    this.Invoke(new Action(() =>
                                    {
                                        textBomSend.Text   = "Send Bomb Result!";
                                        txtBombRecive.Text = strBombRep;
                                    }
                                                           ));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Bomb.Error(ex.ToString());
            }
        }
Пример #9
0
        public void Reciver()
        {
            try
            {
                byte[] data = new byte[100];
                string str;
                int    length;
                bool   needReconnect = false;


                while (true)
                {
                    length = socket.Receive(data);

                    if (length != 0)
                    {
                        str = System.Text.Encoding.Default.GetString(data);

                        str = str.Substring(0, length);

                        APISocksEventArgs mye = new APISocksEventArgs();
                        mye.msg = str;
                        if (Receiving != null)
                        {
                            Receiving(this, mye);
                        }

                        Logger.socket.Info("Recive :" + str);
                    }
                    else
                    {
                        needReconnect = true;
                        break;
                    }
                    if (StopRecive)
                    {
                        break;
                    }

                    if (socket.Connected == false)
                    {
                        break;
                    }
                }

                if (socket.Connected == false || needReconnect)
                {
                    StopRecive = true;

                    socket.Disconnect(true);

                    m_Status = SocketStatus.DisConnected;
                    APISocksEventArgs mye = new APISocksEventArgs();
                    mye.msg    = "";
                    mye.status = SocketStatus.DisConnected;
                    if (StatusChange != null)
                    {
                        StatusChange(this, mye);
                    }

                    Connect();
                }
            }
            catch (Exception ex)
            {
                Logger.Bomb.Error(ex.ToString());

                StopRecive = true;

                m_Status = SocketStatus.DisConnected;
                APISocksEventArgs mye = new APISocksEventArgs();
                mye.msg    = "";
                mye.status = SocketStatus.DisConnected;
                if (StatusChange != null)
                {
                    StatusChange(this, mye);
                }

                Connect();
            }
        }