//Send game data to all clients public void Broadcast(SerializableObject sObject) { foreach(StateObject so in clients) { Send(so.WorkingSocket, sObject); } }
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); }
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); } }
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 !"); } }
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 = ""; }
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()); } }
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()); } }