Ejemplo n.º 1
0
        private void onRecv(IAsyncResult ar)
        {
            udpacceptnetworkservice service = ar.AsyncState as udpacceptnetworkservice;

            int read = listen.EndReceiveFrom(ar, ref tempRemoteEP);

            if (read > 0)
            {
                IPEndPoint sender = (IPEndPoint)tempRemoteEP;

                if (udpchannels.ContainsKey(sender.ToString()))
                {
                    udpchannel ch = udpchannels[sender.ToString()];

                    ch.recv(recvbuf, read);
                }
                else
                {
                    udpchannel ch = new udpchannel(listen, sender);
                    ch.onDisconnect += this.onChannelDisconn;

                    udpchannels.Add(sender.ToString(), ch);
                    onChannelConn(ch);

                    ch.recv(recvbuf, read);
                }
            }

            listen.BeginReceiveFrom(recvbuf, 0, recvbuflenght, 0, ref tempRemoteEP, new AsyncCallback(this.onRecv), this);
        }
        private void onRecv(IAsyncResult ar)
        {
            udpacceptnetworkservice service = ar.AsyncState as udpacceptnetworkservice;

            try
            {
                int read = listen.EndReceiveFrom(ar, ref tempRemoteEP);
                if (read > 0)
                {
                    IPEndPoint sender = (IPEndPoint)tempRemoteEP;

                    if (udpchannels.ContainsKey(sender.ToString()))
                    {
                        udpchannel ch = udpchannels[sender.ToString()];

                        ch.recv(recvbuf, read);
                    }
                    else
                    {
                        udpchannel ch = new udpchannel(listen, sender);
                        ch.onDisconnect += this.onChannelDisconn;
                        ch.Disconnect   += ChannelDisconn;

                        udpchannels.Add(sender.ToString(), ch);
                        onChannelConn(ch);

                        ch.recv(recvbuf, read);
                    }
                }

                listen.BeginReceiveFrom(recvbuf, 0, recvbuflenght, 0, ref tempRemoteEP, new AsyncCallback(this.onRecv), this);
            }
            catch (System.Net.Sockets.SocketException)
            {
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exception:{0}", e);
            }
        }