/// <summary>Close the server transport and free any resources associated with it.</summary> /// <remarks> /// Close the server transport and free any resources associated with it. /// <p>Note that the server transport is <b>not deregistered</b>. You'll /// have to do it manually if you need to do so. The reason for this /// behaviour is, that the portmapper removes all entries regardless of /// the protocol (TCP/IP or UDP/IP) for a given ONC/RPC program number /// and version. /// <p>Calling this method on a <code>OncRpcUdpServerTransport</code> /// results in the UDP network socket immediately being closed. The /// handler thread will therefore either terminate directly or when it /// tries to sent back a reply which it was about to handle at the time /// the close method was called. /// </remarks> public override void Close() { if (socket != null) { // // Since there is a non-zero chance of getting race conditions, // we now first set the socket instance member to null, before // we close the corresponding socket. This avoids null-pointer // exceptions in the method which waits for new requests: it is // possible that this method is awakened because the socket has // been closed before we could set the socket instance member to // null. Many thanks to Michael Smith for tracking down this one. // socket.Shutdown(SocketShutdown.Both); Socket deadSocket = socket; socket = null; deadSocket.Close(); } if (sendingXdr != null) { XdrEncodingStream deadXdrStream = sendingXdr; sendingXdr = null; try { deadXdrStream.Close(); } catch (System.IO.IOException) { } catch (org.acplt.oncrpc.OncRpcException) { } } if (receivingXdr != null) { XdrDecodingStream deadXdrStream = receivingXdr; receivingXdr = null; try { deadXdrStream.Close(); } catch (IOException) { } catch (OncRpcException) { } } }
/// <summary> /// Create a new instance of a <code>OncRpcUdpServerTransport</code> which /// encapsulates UDP/IP-based XDR streams of an ONC/RPC server. /// </summary> /// <remarks> /// Create a new instance of a <code>OncRpcUdpServerTransport</code> which /// encapsulates UDP/IP-based XDR streams of an ONC/RPC server. Using a /// server transport, ONC/RPC calls are received and the corresponding /// replies are sent back. /// This constructor is a convenience constructor for those transports /// handling only a single ONC/RPC program and version number. /// </remarks> /// <param name="dispatcher"> /// Reference to interface of an object capable of /// dispatching (handling) ONC/RPC calls. /// </param> /// <param name="bindAddr">The local Internet Address the server will bind to.</param> /// <param name="port"> /// Number of port where the server will wait for incoming /// calls. /// </param> /// <param name="info"> /// Array of program and version number tuples of the ONC/RPC /// programs and versions handled by this transport. /// </param> /// <param name="bufferSize"> /// Size of buffer for receiving and sending UDP/IP /// datagrams containing ONC/RPC call and reply messages. /// </param> /// <exception cref="org.acplt.oncrpc.OncRpcException"></exception> /// <exception cref="System.IO.IOException"></exception> public OncRpcUdpServerTransport(OncRpcDispatchable dispatcher , IPAddress bindAddr, int port, OncRpcServerTransportRegistrationInfo [] info, int bufferSize) : base(dispatcher, port, info) { // // Make sure the buffer is large enough and resize system buffers // accordingly, if possible. // if (bufferSize < 1024) { bufferSize = 1024; } socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); if (bindAddr == null) bindAddr = IPAddress.Any; IPEndPoint localEP = new IPEndPoint(bindAddr, port); socket.Bind(localEP); if (port == 0) { this.port = ((IPEndPoint)(socket.LocalEndPoint)).Port; } if (socket.SendBufferSize < bufferSize) { socket.SendBufferSize = bufferSize; } if (socket.ReceiveBufferSize < bufferSize) { socket.ReceiveBufferSize = bufferSize; } // // Create the necessary encoding and decoding streams, so we can // communicate at all. // sendingXdr = new org.acplt.oncrpc.XdrUdpEncodingStream(socket, bufferSize); receivingXdr = new org.acplt.oncrpc.XdrUdpDecodingStream(socket, bufferSize); }