Exemplo n.º 1
0
 //Send game data to all clients
 public void Broadcast(SerializableObject sObject)
 {
     foreach(StateObject so in clients)
     {
         Send(so.WorkingSocket, sObject);
     }
 }
Exemplo n.º 2
0
        public void Send(SerializableObject data)
        {
            byte[] byteData = Serialization.Serialize(data);

            // Begin sending the data to the remote device.
            socket.BeginSend(byteData, 0, byteData.Length, 0,
                new AsyncCallback(SendCallback), socket);
        }
Exemplo n.º 3
0
 private void SendScore(String name, int score)
 {
     SerializableObject so = new SerializableObject(DataType.Score, name, score);
     if (n is Host) //if host
     {
         LogUpdateText("Host is broadcasting...");
         ((Host)n).Broadcast(so);
     }
     else
     {
         LogUpdateText("Client is sending...");
         ((Client)n).Send(so);
     }
 }
Exemplo n.º 4
0
 private void btnStart_Click(object sender, EventArgs e)
 {
     if (names.Count > 1)
     {
         SerializableObject so = new SerializableObject(DataType.Game_Start);
         ((Host)n).Broadcast(so);
         GameStart();
     }
     else
     {
         LogUpdateText("You need at least 2 players to start a game !");
     }
 }
Exemplo n.º 5
0
        private void btnSendMessage_Click(object sender, EventArgs e)
        {
            SerializableObject so = new SerializableObject(DataType.Message, player.Name, chatBox.Text);
            if (n is Host)
            {
                ((Host)n).Broadcast(so);

                MessageEvent(player.Name, chatBox.Text);
            }
            else
            {
                ((Client)n).Send(so);
            }

            chatBox.Text = "";
        }
Exemplo n.º 6
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket client = (Socket)ar.AsyncState;

                // Complete the connection.
                client.EndConnect(ar);

                try
                {
                    //Send client's player name to host
                    IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
                    SerializableObject sobject = new SerializableObject(DataType.Player_Registration, player.Name);
                    Send(sobject);

                    Receive(client);
                }
                catch (SocketException e)
                {
                    UpdateStatus("Problem connecting to server: " + e.ToString());
                }
            }

            catch (Exception e)
            {
                UpdateStatus(e.ToString());
            }
        }
Exemplo n.º 7
0
        protected override void ReceiveCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.WorkingSocket;

            try // Read data from the handler socket.
            {
                int nbrBytes = handler.EndReceive(ar);
                state.AddTempBufferToBuffer(nbrBytes);

                if (handler.Available > 0) //if received data is not complete, then continue receiving
                {
                    handler.BeginReceive(
                        state.TempBuffer, 0,
                        state.TempBuffer.Length,
                        SocketFlags.None,
                        ReceiveCallback,
                        state);
                }
                else //if data has been fully received
                {
                    lock (locker)
                    {
                        SerializableObject sObject = (SerializableObject)Serialization.Deserialize(state.Buffer.ToArray());
                        String s;

                        switch (sObject.DataType)
                        {
                            case DataType.Player_Registration:

                                s = "Player " + sObject.Name + " connected to host";
                                UpdateStatus(s);

                                AddPlayer(sObject.Name);

                                //Send host name to client
                                SerializableObject so = new SerializableObject(DataType.Player_Registration, player.Name);
                                byte[] byteData = Serialization.Serialize(so);

                                // Begin sending the data to the remote device.
                                handler.BeginSend(byteData, 0, byteData.Length, 0,
                                    new AsyncCallback(SendCallback), handler);
                                break;

                            case DataType.Score:
                                UpdateScore(sObject.Name, sObject.Score);
                                Debug.WriteLine("Host received score update for " + sObject.Name + " : " + sObject.Score);
                                Broadcast(sObject);
                                break;

                            case DataType.Message:
                                Message(sObject.Name, sObject.Message);
                                Broadcast(sObject);
                                break;
                        }
                    }//end lock
                    //continue receiving other data
                    Receive(state.WorkingSocket);
                }//end else (->data has been received)
            }//end try
            catch (SocketException e)
            {
                UpdateStatus(e.ToString());
            }
        }