/* * Commands are received asynchronously. OnReceive is the handler for them. */ private void OnReceive(IAsyncResult ar) { try { EndPoint receivedFromEP = new IPEndPoint(IPAddress.Any, 0); //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 call. case Command.Invite: { if (bIsCallActive == false) { //We have no active call. //Ask the user to accept the call or not. if (MessageBox.Show("Call coming from " + msgReceived.strName + ".\r\n\r\nAccept it?", "VoiceChat", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { SendMessage(Command.OK, receivedFromEP); vocoder = msgReceived.vocoder; otherPartyEP = receivedFromEP; otherPartyIP = (IPEndPoint)receivedFromEP; InitializeCall(); } else { //The call is declined. Send a busy response. SendMessage(Command.Busy, receivedFromEP); } } else { //We already have an existing call. Send a busy response. SendMessage(Command.Busy, receivedFromEP); } break; } //OK is received in response to an Invite. case Command.OK: { //Start a call. InitializeCall(); 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 call established. This is used to prevent other users from sending a Bye, which //would otherwise end the call. if (receivedFromEP.Equals (otherPartyEP) == true) { //End the call. UninitializeCall(); } 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, "VoiceChat-OnReceive ()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/* * Send a message to the remote party. */ private void SendMessage(Command cmd, EndPoint sendToEP) { try { //Create the message to send. Data msgToSend = new Data(); msgToSend.strName = txtName.Text; //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, "VoiceChat-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); //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) { case Command.ChangeVocoderALaw: LogAppend("El servidor cambio el modo a ALaw"); break; case Command.ChangeVocoderNone: LogAppend("El servidor cambio el modo a None"); break; case Command.ChangeVocoderuLaw: LogAppend("El servidor cambio el modo a uLaw"); break; //We have an incoming call. case Command.Invite: { if (bIsServerStarted) { LogAppend("Se ha conectado: " + receivedFromEP.ToString()); vocoder = msgReceived.vocoder; lock (otherPartyIPs) { if (!ExistsIp((IPEndPoint)receivedFromEP)) otherPartyIPs.Add((IPEndPoint)receivedFromEP); } SendMessage(Command.OK, receivedFromEP); } break; } //OK is received in response to an Invite. case Command.OK: { if (!bIsServerStarted) { eMode = Mode.Client; //Solamente puede recibir este parametro si no está en modo servidor lock (otherPartyIPs) { otherPartyIPs.Clear(); otherPartyIPs.Add((IPEndPoint)receivedFromEP); } LogAppend("Connectado a : " + receivedFromEP.ToString()); InitializeCall(); } break; } //Remote party is busy. case Command.Bye: { //Check if the Bye command has indeed come from the user/IP with which we have //a call established. This is used to prevent other users from sending a Bye, which //would otherwise end the call. if (receivedFromEP.Equals(otherPartyEP) == true) { //End the call. UninitializeCall(); } break; } } byteData = new byte[1024]; //Get ready to receive more commands. if (!bStop) clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref receivedFromEP, new AsyncCallback(OnReceive), null); else IsThreadReceiveCommandsEnd = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "VoiceChat-OnReceive ()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }