Exemplo n.º 1
0
        /// <summary>
        /// Initiate an asynchronous receive operation on a buffer.
        /// </summary>
        /// <param name="Buffer">Supplies the buffer to receive from.</param>
        private void InitiateReceive(SocketRecvBuffer Buffer)
        {
            //
            // If shutdown is in process, then prevent new receive operations
            // from being initiated and wake the main thread if the last
            // pending receive has been drained.
            //

            if (QuitRequested)
            {
                if (PendingBuffers == 0)
                    QuitEvent.Set();

                return;
            }

            IncrementPendingBuffers();

            try
            {
                EndPoint RecvEndPoint = (EndPoint)Buffer.Sender;
                IAsyncResult Result = Buffer.SocketDescriptor.Socket.BeginReceiveFrom(
                    Buffer.Buffer,
                    0,
                    Buffer.Buffer.Length,
                    SocketFlags.None,
                    ref RecvEndPoint,
                    RecvCompletionCallback,
                    Buffer);
            }
            catch (Exception e)
            {
                DecrementPendingBuffers();

                Logger.Log(LogLevel.Error, "NWMasterServer.InitiateReceive(): BeginReceiveFrom failed: Exception: {0}", e);
                Stop();
                return;
            }
        }
Exemplo n.º 2
0
            /// <summary>
            /// Set up a SocketInfo descriptor for a socket and bind it to a
            /// local endpoint.
            /// </summary>
            /// <param name="MasterServer">Supplies the associated master
            /// server object.</param>
            /// <param name="BindAddress">Supplies the bind address.</param>
            /// <param name="BindPort">Supplies the bind port, else 0 if there
            /// is to be no specific local bind port.</param>
            /// <param name="SocketUse">Supplies the usage type of the
            /// socket.</param>
            /// <param name="OnRecvSocketDatagram">Supplies the high level
            /// socket receive message callback for the socket.</param>
            public SocketInfo(NWMasterServer MasterServer, IPAddress BindAddress, int BindPort, SocketUse SocketUse, OnRecvSocketDatagramDelegate OnRecvSocketDatagram)
            {
                this.MasterServer = MasterServer;
                this.SocketUse = SocketUse;
                this.OnRecvSocketDatagram = OnRecvSocketDatagram;

                for (int i = 0; i < BUFFER_COUNT; i += 1)
                {
                    SocketRecvBuffer Buffer = new SocketRecvBuffer();

                    Buffers[i] = Buffer;

                    Buffer.Buffer = new byte[MAX_FRAME_SIZE];
                    Buffer.Sender = new IPEndPoint(0, 0);
                    Buffer.SocketDescriptor = this;
                }

                Socket.Blocking = false;
                Socket.Bind(new IPEndPoint(BindAddress, BindPort));
                Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
            }