/// Last Modified: 10/24/10 /// <summary> /// A static method that will send packet information /// to the IPEndpoint supplied. This method is blocking /// and does not support asynchronous sending of information. /// </summary> /// ----------------------------------------------------- /// PRECONDITIONS: NA -- No preconditions exist. /// ----------------------------------------------------- /// Parameters: /// <param name="p"> /// A packet of information to send. /// </param> /// <param name="ipep"> /// The destination endpoint to send the packet. /// </param> /// ----------------------------------------------------- /// POSTCONDITIONS: The message will be sent or an exception will have been thrown. /// ----------------------------------------------------- /// Exceptions: /// 1) NodeSocketInvalid /// 2) NodeUnknownException /// ----------------------------------------------------- /// Return Value: public void SendTo(Packet p, IPEndPoint ipep) { Socket s = null; try { //Create the socket to send information to. s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //Cast the endpoint. EndPoint ep = (EndPoint)ipep; //Perform any necessary encryptions based on the security model. byte[] byData = (m_nodeSecurityModel != null) ? m_nodeSecurityModel.ByteEncrypt(p.ToByteArray()) : p.ToByteArray(); //Marshall the string data into a byte stream and send the data. s.SendTo(byData, SocketFlags.None, ep); } catch (ObjectDisposedException ode) { throw new NodeSocketInvalid("The Socket has been closed. ", ode); } catch (SocketException se) { //MSDN NOTE: If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. // After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library //for a detailed description of the error. throw new NodeSocketInvalid(se.Message, se); } catch (Exception ex) { //Call the error callback if one exists. throw new NodeUnknownException(ex.Message, ex); } finally { //Close the socket. s.Close(); } }
/// Last Modified: 11/28/09 /// <summary> /// Starts the sending the packet. /// </summary> /// ----------------------------------------------------- /// PRECONDITIONS: Refer to the following arguments: /// ----------------------------------------------------- /// Parameters: /// ----------------------------------------------------- /// POSTCONDITIONS: NA -- No postconditions exist. /// ----------------------------------------------------- /// Return Value: public void AsynchSend(Packet p, IPEndPoint ipep) { //Avoid invalid socket access. if (m_Socket == null) return; //Prepare the state object. StateObject state = new StateObject(); state.socket = m_Socket; state.iep = ipep; state.buffer = p.ToByteArray(); //Begin sending the packet information. m_Socket.BeginSendTo(state.buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None, (EndPoint)ipep, new AsyncCallback(OnSendCallback), state); }