예제 #1
0
        //Listen for ClientConnection on port
        public void Listen(ushort port)
        {
            _localSecurity = new Security.Security();
            _localSecurity.GenerateSecurity(true, true, true);

            _localRecvBuffer = new TransferBuffer(8192, 0, 0);
            _localListener   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _localSocket     = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //Thread Management
            _thPacketProcessor = new Thread(ThreadedPacketProcessing)
            {
                Name         = "Proxy.Network.Client.PacketProcessor",
                IsBackground = true
            };
            _thPacketProcessor.Start();

            try
            {
                if (_localListener.IsBound == false)
                {
                    _localListener.Bind(new IPEndPoint(IPAddress.Loopback, port));
                    _localListener.Listen(1);
                }
                _localListener.BeginAccept(OnClientConnect, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
예제 #2
0
        private void OnClientConnect(IAsyncResult ar)
        {
            if (_isClosing)
            {
                return;
            }
            try
            {
                _doPacketProcess = true;
                _localSocket     = _localListener.EndAccept(ar);
                _localSocket.BeginReceive(_localRecvBuffer.Buffer, 0, 8192, SocketFlags.None, WaitForData, _localSocket);

                _localSecurity = new Security.Security();
                _localSecurity.GenerateSecurity(false, false, false);

                Connected?.Invoke();
            }
            catch (Exception ex)
            {
                throw new Exception("Network.Client.OnClientConnect: " + ex.Message, ex);
            }
        }