Exemplo n.º 1
0
        private void ReceiveCallback(IAsyncResult AR)
        {
            Socket current = (Socket)AR.AsyncState;
            int received;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException x)
            {
                this.textBox_log.Invoke(new MethodInvoker(delegate ()
                { textBox_log.AppendText("Client forcefully disconnected \n"); }));

                current.Close(); // Dont shutdown because the socket may be disposed and its disconnected anyway
                _clientSockets.Remove(current);
                _disconnectAllClients = true;                
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(_buffer, recBuf, received);
            Int32 role = BitConverter.ToInt32(recBuf,0);
            Int32 boxOut = BitConverter.ToInt32(recBuf, 4);
            Int32 reqOut = BitConverter.ToInt32(recBuf, 8);
            Int32 stock = BitConverter.ToInt32(recBuf, 12);
            Int32 u_orders = BitConverter.ToInt32(recBuf, 16);
            Int32 roundCode = BitConverter.ToInt32(recBuf, 20);

            if (_disconnectAllClients || _clientSockets.Count > 4)
            {
                Message m = new Message(0, 400, 400, -900); // disconnect all users
                byte[] data = m.getMessageByteArray();
                current.Send(data);
                current.Close(); // Dont shutdown because the socket may be disposed and its disconnected anyway
                _clientSockets.Remove(current);
                Console.WriteLine("Velikost bufferu pro sokety: " + _clientSockets.Count.ToString());
                if(_clientSockets.Count == 0)
                {
                    System.Diagnostics.Process.Start("Server.exe", _PORT.ToString());
                    Environment.Exit(0);
                }
                return;
            }

            if (roundCode == -600) // load configuration + role
            {
                initClientByRole(current);
                //return;
            }

            else if (reqOut != 0 && boxOut != 0 && roundCode == -500)
            {
                // chci data - dosly poprve
                if (_nextRound[role] == false)
                {
                    lock (_locker)
                    {
                        if (isNextRound())
                        {
                            Thread.Sleep(2000); // thread collector
                        }
                        updeteRoundCounter(role);

                        updateDataQues(role, boxOut, reqOut);
                        writeToFile(role, stock, u_orders);

                        this.textBox_log.Invoke(new MethodInvoker(delegate ()
                        { textBox_log.AppendText("Hrac<" + role.ToString() + "> pozaduje: " + reqOut.ToString() + " posila: " + boxOut.ToString() + "\n"); }));

                        this.tb_LogDetail.Invoke(new MethodInvoker(delegate ()
                        { tb_LogDetail.AppendText("flag zpavy " + roundCode.ToString() + " - "); }));

                        if (isNextRound())
                        {
                            this.tb_LogDetail.Invoke(new MethodInvoker(delegate ()
                            { tb_LogDetail.AppendText("vsichni odeslaly sva data"); }));
                        }
                        else
                        {
                            this.tb_LogDetail.Invoke(new MethodInvoker(delegate ()
                            { tb_LogDetail.AppendText("pole nextRound obsahuje FALSE - na nekoho se ceka"); }));
                        }

                        Message m = new Message(role, 300, 300, -300); // waiting
                        byte[] data = m.getMessageByteArray();
                        current.Send(data);

                        this.tb_LogDetail.Invoke(new MethodInvoker(delegate ()
                        { tb_LogDetail.AppendText(" - data odeslana \n"); }));
                    }
                }
                else
                {
                    Message m = new Message(role, 300, 300, -300); // waiting
                    byte[] data = m.getMessageByteArray();
                    current.Send(data);
                }
                //return;
            }

            else if (roundCode == -300) // client ceka az server posle 200 - new round
            {
                if (isNextRound())  // vsichni uz odeslali sva data - server musi vsem zaslat "new round"
                {
                    lock (_locker)
                    {
                        if (_allPlayersReady[role] == false)
                        {
                            _allPlayersReady[role] = true; // obslouzeni role na indexu
                        
                        //}
                            if (!_dataShifted)
                            {
                                shiftQueByNewValue();
                                _dataShifted = true;
                                _roundNumber++;
                            }
                            if (arePlayerReady()) // jsou obslouzeni vsichni - odpovi nic nedelej
                            {
                                resetRoundCounter();
                                textBox_log.Invoke(new MethodInvoker(delegate ()
                                { textBox_log.AppendText("Zacalo nove kolo [" + _roundNumber.ToString() + "] \n"); }));
                            }
                            //}
                            //Thread.Sleep(2000); // thread collector
                            sendDataByRole(role, current); // new round
                        }
                    }
                    //return;                        
                }
                else
                {
                    Message m = new Message(role, 400, 400, -400); // do nothing
                    byte[] data = m.getMessageByteArray();
                    current.Send(data);
                }
            }
            else
            {
                Message m = new Message(role, 300, 300, -300); // waiting
                byte[] data = m.getMessageByteArray();
                current.Send(data);
            }

            current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);

        }
Exemplo n.º 2
0
        private void sendDataByRole(Int32 role, Socket current)
        {
            Int32 boxIn;
            Int32 boxReqIn;

            //resetRoundCounter();

            switch (role)
            {
                case 0:
                    boxIn = _materialQue[1];
                    boxReqIn = _infoOrderQue[6];
                    break;
                case 1:
                    boxIn = _materialQue[3];
                    boxReqIn = _infoOrderQue[4];
                    break;
                case 2:
                    boxIn = _materialQue[5];
                    boxReqIn = _infoOrderQue[2];
                    break;
                case 3:
                    boxIn = _materialQue[7];
                    boxReqIn = _infoOrderQue[0];
                    break;
                default:
                    boxIn = 0;
                    boxReqIn = 0;
                    break;
            }
            if(_roundNumber > 35)
            {
                if (!_endOfGame)
                {
                    _finalCosts = getFinalCosts();
                    _averageCosts = Convert.ToInt32(getAverageCosts());
                    _endOfGame = true;
                    writeToFinalScore(_finalCosts);
                    //System.Diagnostics.Process.Start("client.exe", "100 192.168.1.2");
                }
                
                Message m = new Message(role, _averageCosts, _finalCosts, -1000); // end game occur
                byte[] data = m.getMessageByteArray();
                current.Send(data);
            }
            else
            {
                Message m = new Message(role, boxIn, boxReqIn, -200); // new round
                byte[] data = m.getMessageByteArray();
                current.Send(data);
                this.tb_LogDetail.Invoke(new MethodInvoker(delegate ()
                { tb_LogDetail.AppendText("data zaslana pro " + role.ToString() + "\n"); }));
            }
            
        }
Exemplo n.º 3
0
 private void loadConfigurationRequest()
 {
     Message m = new Message(_ROLE, 600, 600, 600, 600, -600);
     byte[] buffer = m.getMessageByteArray();
     _clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
 }
Exemplo n.º 4
0
        private void SendRequest()
        {
            try
            {
                // pred odeslanim hodnot si upravim sklad a zakazky podle aktualni hodnoty v poli
                //int un_order = (int) num_unfulfilled_orders.Value;
                int un_order = (int)stringToInt(lbl_dluh.Text);
                //int stock = (int) num_stock.Value;
                int stock = (int)stringToInt(lbl_sklad.Text);
                //int customerRequest = (int)num_in_req_box.Value;
                int customerRequest = (int)stringToInt(lbl_reqIn.Text);
                updateCLientStockAndUnfulfilledOrders(un_order, stock);
                //add2CostSum();
                add2Chart(customerRequest);
                _roundNumber++;
                updateScore();

                //Message m = new Message(_ROLE, (Int32)stringToInt(lbl_boxOut.Text), (Int32)num_reqOut.Value, _stock,_unfulfilledOrders ,-500);
                Message m = new Message(_ROLE, (Int32)stringToInt(lbl_boxOut.Text), (Int32)stringToInt(lbl_reqOut.Text), _stock, _unfulfilledOrders, -500);
                byte[] buffer = m.getMessageByteArray();
                // vymazani vsech vstupnich poli (aby doslo ke zmene -> zavola se metoda)
                
                resetAllCells();
                //hideControls();
                
                //posladni dat serveru
                _clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
                this.label2.Invoke(new MethodInvoker(delegate ()
                { label2.Text = "odeslano"; }));
            }
            catch(Exception e) // nastala chyba -> udelej log
            {
                this.label1.Invoke(new MethodInvoker(delegate ()
                { label1.Text = "chyba pri odeslani"; }));

                if (!_clientSocket.Connected)
                {
                    Console.WriteLine("Vypadlo internetove spojeni");
                    Environment.Exit(666);
                }
            }
        }
Exemplo n.º 5
0
 private void SendWaitingRequest()
 {
     Message m = new Message(_ROLE, 300, 300,300,300,-300);
     byte[] buffer = m.getMessageByteArray();
     _clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
 }