public juggle.Ichannel connect(String ip, short port)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                s.Bind(new IPEndPoint(IPAddress.Any, 0));

                udpchannel ch = new udpchannel(s, new IPEndPoint(IPAddress.Parse(ip), port));
                ch.onDisconnect += this.onChannelDisconn;

                process_.reg_channel(ch);

                _tmp_socket_data.Add(s, new tmp_socket_data(ch));
                s.BeginReceiveFrom(_tmp_socket_data[s].recvbuf, 0, _tmp_socket_data[s].recvbuflenght, 0, ref _tmp_socket_data[s].remote_ep, new AsyncCallback(this.onRecv), s);

                return(ch);
            }
            catch (System.Net.Sockets.SocketException e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Net.Sockets.SocketException:{0}", e);

                return(null);
            }
            catch (System.Exception e)
            {
                log.log.error(new System.Diagnostics.StackFrame(true), timerservice.Tick, "System.Exceptio:{0}", e);

                return(null);
            }
        }
Example #2
0
        public void disconn_channel(juggle.Ichannel ch)
        {
            udpchannel udp_ch = ch as udpchannel;

            _tmp_socket_data.Remove(udp_ch.s);
            udp_ch.s.Close();
        }
        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);
        }
        public void ChannelDisconn(juggle.Ichannel ch)
        {
            process_.unreg_channel(ch);

            udpchannel udp_ch = ch as udpchannel;

            udpchannels.Remove(udp_ch.remote_ep.ToString());
        }
            public tmp_socket_data(udpchannel _ch)
            {
                remote_ep     = new IPEndPoint(IPAddress.Any, 0);
                recvbuflenght = 16 * 1024;
                recvbuf       = new byte[recvbuflenght];

                ch = _ch;
            }
        public void onChannelDisconn(juggle.Ichannel ch)
        {
            udpchannel udp_ch = ch as udpchannel;

            _tmp_socket_data.Remove(udp_ch.s);

            if (onChannelDisconnect != null)
            {
                onChannelDisconnect(ch);
            }
        }
        public void onChannelDisconn(juggle.Ichannel ch)
        {
            if (onChannelDisconnect != null)
            {
                onChannelDisconnect(ch);
            }

            process_.unreg_channel(ch);

            udpchannel udp_ch = ch as udpchannel;

            udpchannels.Remove(udp_ch.remote_ep.ToString());
        }
        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);
            }
        }