Esempio n. 1
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);
        }
Esempio n. 2
0
        // return listener if possible; set callback to null if no callback is needed
        public SockMgr BeginBuildTcp(SockFactoryOptions options, SocketRole socketRole, SockMgr.SockMgrConnectEventHandler connectCallback = null, SockMgr.SockMgrAcceptEventHandler acceptCallback = null, object callbackState = null)
        {
            SockMgr listenerMgr = null;

            _sockFactory.SetOptions(options);
            switch (socketRole)
            {
            case SocketRole.Client:
                if (options.TimesToTry > 0)
                {
                    _sockFactory.BuildTcpClient(connectCallback, callbackState);
                }
                break;

            case SocketRole.Listener:
                listenerMgr = _sockFactory.GetTcpListener();
                _sockFactory.ServerAccept(listenerMgr, acceptCallback, callbackState);
                RegisterNewSockMgr(listenerMgr);
                break;
            }
            return(listenerMgr);
        }