public void Join(SpreadConnection c, string groupName) { // Check if this group has already been joined. if (this.connection != null) { throw new SpreadException("Already joined."); } // Set member variables. this.connection = c; this.name = groupName; // Error check the name. byte[] bytes; try { bytes = System.Text.Encoding.ASCII.GetBytes(name); } catch (Exception e) { throw new SpreadException("Encoding not supported: " + e); } for (int i = 0; i < bytes.Length; i++) { // Make sure the byte (character) is within the valid range. if ((bytes[i] < 36) || (bytes[i] > 126)) { throw new SpreadException("Illegal character in group name."); } } // Get a new message. SpreadMessage joinMessage = new SpreadMessage(); // Set the group we're sending to. joinMessage.AddGroup(name); // Set the service type. joinMessage.ServiceType = SpreadMessage.JOIN_MESS; // Send the message. connection.Multicast(joinMessage); }
//------------------------------ Private ---------------------------------------- private void send(string pToGroup, string pMessage, bool pIsSelfDiscard) { SpreadMessage _spreadMsg = new SpreadMessage(); _spreadMsg.IsAgreed = true; // _spreadMsg.IsSafe = true; _spreadMsg.IsSelfDiscard = pIsSelfDiscard; _spreadMsg.AddGroup(pToGroup); _spreadMsg.Data = System.Text.Encoding.ASCII.GetBytes(pMessage); // Send msg and wait for response try { connection.Multicast(_spreadMsg); // if ( ! pIsSelfDiscard) { // SyncLock.Wait(); // } } catch (Exception _ex) { T.LogStatus("Multicast Exception: " + _ex); throw; } }
public void Leave() { // Check if we can leave. if (connection == null) { throw new SpreadException("No group to leave."); } // Get a new message. SpreadMessage leaveMessage = new SpreadMessage(); // Set the group we're sending to. leaveMessage.AddGroup(name); // Set the service type. leaveMessage.ServiceType = SpreadMessage.LEAVE_MESS; // Send the message. connection.Multicast(leaveMessage); // No longer connected. connection = null; }