public void BroadcastMessageAsync(IMessage message) { for (int i = 0; i < mGroupClients.Count; ++i) { IClientDetails currClientDetails = mGroupClients[i]; IClient currClient = null; if (clientConnections.ContainsKey(currClientDetails)) { currClient = clientConnections[currClientDetails]; } else { currClient = new P2PClient(ListenPort, currClientDetails.ClientIPAddress, currClientDetails.ClientListenPort, Group); currClient.Initialize(); } currClient.SendMessageAsync(message); if (!clientConnections.ContainsKey(currClientDetails)) { clientConnections[currClientDetails] = currClient; } } }
public void SendMessageAsync(IMessage message, IClientDetails details) { if (message == null) { //throw a null reference exception throw new NullReferenceException("The supplied IMessage object is null!"); } if (details == null) { //throw a null reference exception throw new NullReferenceException("The supplied IClientDetails object is null!"); } IClient currClient = null; if (clientConnections.ContainsKey(details)) { currClient = clientConnections[details]; } else { currClient = new P2PClient(ListenPort, details.ClientIPAddress, details.ClientListenPort, Group); currClient.Initialize(); } currClient.SendMessageAsync(message); if (!clientConnections.ContainsKey(details)) { clientConnections[details] = currClient; } }
public void BroadcastMessageAsyncExceptAddress(string[] addressesToExclude, IMessage message) { for (int i = 0; i < mGroupClients.Count; ++i) { IClientDetails currClientDetails = mGroupClients[i]; var skip = false; for (var j = 0; j < addressesToExclude.Length; j++) { if (addressesToExclude[j] == currClientDetails.ToString()) { skip = true; } } if (skip) { continue; //skip if should be excluded } //set client IClient currClient = null; if (clientConnections.ContainsKey(currClientDetails)) { currClient = clientConnections[currClientDetails]; } else { currClient = new P2PClient(ListenPort, currClientDetails.ClientIPAddress, currClientDetails.ClientListenPort, Group); currClient.Initialize(); } //send message currClient.SendMessageAsync(message); //add to clientConnections if not there already if (!clientConnections.ContainsKey(currClientDetails)) { clientConnections[currClientDetails] = currClient; } } }
public void OnHandleClientData(IAsyncResult asyncResult) { try { TxRxPacket dataStatus = (TxRxPacket)asyncResult.AsyncState; dataStatus.mCurrentSocket.EndReceive(asyncResult); dataStatus.StoreCurrentData(); IMessage rxMessage = mMessageParser.ParseMessage(dataStatus.mStoredBuffer); if (rxMessage == null) { //receive the rest of the message dataStatus.mCurrentSocket.BeginReceive(dataStatus.mDataBuffer, 0, dataStatus.mDataBuffer.Length, SocketFlags.None, new AsyncCallback(OnHandleClientData), dataStatus); return; } //handle the message (which can either be register or unregister) //send response message if needed switch (rxMessage.Type) { case ((int)MessageType.ResgisteredClientsListMessage): { Socket workerSocket = dataStatus.mCurrentSocket; //respond with the current group in the message RegisteredClientsListMessage rxClientList = (RegisteredClientsListMessage)rxMessage; if (rxClientList.Clients != null) { for (int i = 0; i < rxClientList.Clients.Count; ++i) { groupClientsDetails.Add(rxClientList.Clients[i]); //register on each of them IClient client = new P2PClient(mListenPort, rxClientList.Clients[i].ClientIPAddress, rxClientList.Clients[i].ClientListenPort, group); client.Initialize(); client = null; } if (mOnRecieveListOfClients != null && group == ((RegisteredClientsListMessage)rxMessage).Group) { mOnRecieveListOfClients.Invoke(this, new ReceiveListOfClientsEventArgs(((RegisteredClientsListMessage)rxMessage).Clients)); } } break; } case ((int)MessageType.RegisterMessage): { if (!(groupClientsDetails.IndexOf(((RegisterMessage)rxMessage).Client) >= 0)) { groupClientsDetails.Add(((RegisterMessage)rxMessage).Client); if (mOnRegisterClient != null && group == ((RegisterMessage)rxMessage).Group) { mOnRegisterClient.Invoke(this, new ServerRegisterEventArgs(((RegisterMessage)rxMessage).Client)); } if (mOnReceiveMessage != null) { mOnReceiveMessage.Invoke(this, new ReceiveMessageEventArgs(rxMessage)); } } break; } case ((int)MessageType.UnregisterMessage): { if ((groupClientsDetails.IndexOf(((UnregisterMessage)rxMessage).Client) >= 0)) { groupClientsDetails.Remove(((UnregisterMessage)rxMessage).Client); if (mOnUnRegisterClient != null && group == ((UnregisterMessage)rxMessage).Group) { mOnUnRegisterClient.Invoke(this, new ServerRegisterEventArgs(((UnregisterMessage)rxMessage).Client)); } } break; } case ((int)MessageType.CommandMessage): { if (mOnReceiveMessage != null) { mOnReceiveMessage.Invoke(this, new ReceiveMessageEventArgs(rxMessage)); } break; } default: { if (rxMessage.Type != ((int)MessageType.EmptyMessage)) { if (mOnReceiveMessage != null) { mOnReceiveMessage.Invoke(this, new ReceiveMessageEventArgs(rxMessage)); } } break; } } } catch (ObjectDisposedException ex) { System.Diagnostics.Debug.WriteLine("Socket has been closed : " + ex.Message); } catch (SocketException ex) { if (ex.ErrorCode == 10054) // Error code for Connection reset by peer { System.Diagnostics.Debug.WriteLine("Connection reset by peer : " + ex.Message); } else { System.Diagnostics.Debug.WriteLine(ex.Message); } } }
public void Initialize() { mListener.Initialize(); mClient.Initialize(); }