示例#1
0
        // <https://gist.github.com/louis-e/888d5031190408775ad130dde353e0fd>
        public SockMgr GetUdpListener()
        {
            Socket listener = new Socket(_options.ListenerIpAddress.AddressFamily,
                                         SocketType.Dgram, ProtocolType.Udp);

            listener.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            listener.Bind(new IPEndPoint(_options.ListenerIpAddress, _options.ListenerPort));

            SockBase sockBase = new SockBase(listener, SocketRole.Listener, true);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            return(sockMgr);
        }
示例#2
0
        public SockMgr GetUdpClient()
        {
            Socket sock = new Socket(_options.ListenerIpAddress.AddressFamily,
                                     SocketType.Dgram, ProtocolType.Udp);

            SockBase sockBase = new SockBase(sock, SocketRole.Client, false);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            // TODO: use BeginConnect instead
            sock.Connect(new IPEndPoint(_options.ListenerIpAddress, _options.ListenerPort));

            return(sockMgr);
        }
示例#3
0
        public void BuildTcpClient(SockMgr.SockMgrConnectEventHandler connectCallback, object callbackState = null)
        {
            Socket sock = new Socket(_options.ListenerIpAddress.AddressFamily,
                                     SocketType.Stream, ProtocolType.Tcp);

            if (_options.ClientPort >= 0)
            {
                sock.Bind(new IPEndPoint(IPAddress.Any, _options.ClientPort));
            }

            SockBase sockBase = new SockBase(sock, SocketRole.Client, false);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            sockMgr.SockMgrConnectEvent += OnSocketConnect;
            sockMgr.StartConnect(new IPEndPoint(_options.ListenerIpAddress, _options.ListenerPort), _options.TimesToTry, connectCallback, callbackState);
        }
示例#4
0
        public SockMgr GetTcpListener()
        {
            IPAddress  ipAddress     = IPAddress.Parse("0.0.0.0");
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, _options.ListenerPort);

            // Create a TCP/IP socket.
            Socket listener = new Socket(ipAddress.AddressFamily,
                                         SocketType.Stream, ProtocolType.Tcp);

            // makes restarting a socket become possible
            // https://blog.csdn.net/limlimlim/article/details/23424855
            listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            SockBase sockBase = new SockBase(listener, SocketRole.Listener, true);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            listener.Bind(localEndPoint);
            listener.Listen(4);

            return(sockMgr);
        }
示例#5
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                AcceptStateObject state = (AcceptStateObject)ar.AsyncState;
                // Get the socket that handles the client request.
                Socket listener = state.workSocket;
                Socket handler  = listener.EndAccept(ar);

                SockBase sockBase = new SockBase(handler, SocketRole.Client, true);
                sockBase.IsConnected = true;
                sockBase.StartReceive();

                SocketAcceptEvent?.Invoke(this, new SocketAcceptEventArgs(state, sockBase));
                if (state.externalCallback != null)
                {
                    state.externalCallback(this, new SocketAcceptEventArgs(state, sockBase));
                }

                listener.BeginAccept(
                    new System.AsyncCallback(AcceptCallback), state);
            }
            catch (ObjectDisposedException) { }  // listener closed
        }
示例#6
0
 public SocketConnectEventArgs(ConnectStateObject state, SockBase handler)
 {
     State = state; Handler = handler;
 }
示例#7
0
 public SocketAcceptEventArgs(AcceptStateObject state, SockBase handler)
 {
     State = state; Handler = handler;
 }
示例#8
0
 public SocketSendEventArgs(SendStateObject state, SockBase handler)
 {
     State = state; Handler = handler;
 }