Пример #1
0
        //======================================================================
        //连接(对于Client和Server,以及UDP和TCP,连接的逻辑不同)与断开连接
        //======================================================================

        public void Active(Socket socket)
        {
            if (m_actived == 0)
            {
                m_socket = socket;

                socket.NoDelay = true;
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
                socket.ReceiveBufferSize = NetDefine.SocketBufferSize;
                socket.SendBufferSize    = NetDefine.SocketBufferSize;

                RemoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;
                LocalEndPoint  = (IPEndPoint)socket.LocalEndPoint;

                m_saeSend            = m_pool.Acquire();
                m_saeSend.Completed += OnSendCompleted;

                m_saeReceive            = m_pool.Acquire();
                m_saeReceive.Completed += OnReceiveCompleted;

                m_actived = 1;

                BeginSend();
                BeginReceive();
            }
        }
Пример #2
0
        public void Active(Socket socket, IPEndPoint remoteEndPoint)
        {
            if (Interlocked.CompareExchange(ref this.m_actived, 1, 0) == 0)
            {
                m_socket = socket;

                socket.ReceiveBufferSize = NetDefine.SocketBufferSize;
                socket.SendBufferSize    = NetDefine.SocketBufferSize;

                RemoteEndPoint = (IPEndPoint)remoteEndPoint;
                LocalEndPoint  = (IPEndPoint)socket.LocalEndPoint;

                m_saeSend                = m_pool.Acquire();
                m_saeSend.Completed     += OnSendCompleted;
                m_saeSend.RemoteEndPoint = remoteEndPoint;

                m_saeReceive                = m_pool.Acquire();
                m_saeReceive.Completed     += OnReceiveCompleted;
                m_saeReceive.RemoteEndPoint = remoteEndPoint;

                BeginSend();
                BeginReceive();
            }
        }
Пример #3
0
        //======================================================================
        //连接与断开连接相关
        //======================================================================
        public void Active(Socket socket, IPEndPoint remoteEndPoint)
        {
            RemoteEndPoint = (IPEndPoint)remoteEndPoint;

            if (Interlocked.CompareExchange(ref this.m_actived, 1, 0) == 0)
            {
                m_socket = socket;

                LocalEndPoint = (IPEndPoint)socket.LocalEndPoint;

                m_saeSend                = m_pool.Acquire();
                m_saeSend.Completed     += OnSendCompleted;
                m_saeSend.RemoteEndPoint = remoteEndPoint;

                BeginSend();
            }
        }
Пример #4
0
        public void Start()
        {
            this.LogWarning("");

            m_IsRunning = true;

            if (m_socket == null)
            {
                m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                m_socket.Bind(new IPEndPoint(IPAddress.Any, m_port));
                LocalEndPoint = m_socket.LocalEndPoint as IPEndPoint;
                m_port        = LocalEndPoint.Port;;

                m_saeReceive                = m_pool.Acquire();
                m_saeReceive.Completed     += OnReceiveCompleted;
                m_saeReceive.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

                ReceiveAsync();
            }
        }