/* * Send a message to the remote party. */ private void SendMessage(Command cmd, EndPoint sendToEP, string sendName = "") { try { if (sendName == "") sendName = Name; //Create the message to send. Data msgToSend = new Data(); msgToSend.strName = sendName; //Name of the user. msgToSend.cmdCommand = cmd; //Message to send. msgToSend.vocoder = vocoder; //Vocoder to be used. byte[] message = msgToSend.ToByte(); //Send the message asynchronously. clientSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, sendToEP, new AsyncCallback(OnSend), null); } catch (Exception ex) { //MessageBox.Show(ex.Message, "ThereMe - SendMessage ()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/* * Commands are received asynchronously. OnReceive is the handler for them. */ private void OnReceive(IAsyncResult ar) { try { EndPoint receivedFromEP = new IPEndPoint(IPAddress.Any, 0); if (clientSocket == null) return; //Get the IP from where we got a message. clientSocket.EndReceiveFrom(ar, ref receivedFromEP); //Convert the bytes received into an object of type Data. Data msgReceived = new Data(byteData); //Act according to the received message. switch (msgReceived.cmdCommand) { //We have an incoming connect. case Command.Invite: { if (bIsConnectActive == false) { //We have no active connect. //Ask the user to accept the connect or not. if (MessageBox.Show("Invite from " + msgReceived.strName + ".\r\n\r\nMusic On?", "VoiceChat", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { SendMessage(Command.OK, receivedFromEP); vocoder = msgReceived.vocoder; otherPartyEP = receivedFromEP; otherPartyIP = (IPEndPoint)receivedFromEP; InitializeConnect(); ClientConnectedEvent(msgReceived.strName); } else { //The connect is declined. Send a busy response. SendMessage(Command.Busy, receivedFromEP); } } else { //We already have an existing connect. Send a busy response. SendMessage(Command.Busy, receivedFromEP); } break; } //Partner played a key. case Command.Key: { ClientKeyPlayedEvent(Convert.ToInt32(msgReceived.strName)); break; } //OK is received in response to an Invite. case Command.OK: { //Start a connect. InitializeConnect(); ClientConnectedEvent(msgReceived.strName); break; } //Remote party is busy. case Command.Busy: { MessageBox.Show("User busy.", "VoiceChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; } case Command.Bye: { //Check if the Bye command has indeed come from the user/IP with which we have //a connect established. This is used to prevent other users from sending a Bye, which //would otherwise end the connect. if (receivedFromEP.Equals(otherPartyEP) == true) { //End the connect. UninitializeConnect(); ClientDisconnectedEvent(); } break; } } byteData = new byte[1024]; //Get ready to receive more commands. clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref receivedFromEP, new AsyncCallback(OnReceive), null); } catch (Exception ex) { //MessageBox.Show(ex.Message, "ThereMe - OnReceive ()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }