Ejemplo n.º 1
0
        public static IAsyncResult BeginListen(IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback,
                                               out int dbg_port, out int con_port)
        {
            dbg_port = con_port = 0;

            Socket dbg_sock = null;
            Socket con_sock = null;

            dbg_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            dbg_sock.Bind(dbg_ep);
            dbg_sock.Listen(1000);
            dbg_port = ((IPEndPoint)dbg_sock.LocalEndPoint).Port;

            if (con_ep != null)
            {
                con_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                con_sock.Bind(con_ep);
                con_sock.Listen(1000);
                con_port = ((IPEndPoint)con_sock.LocalEndPoint).Port;
            }

            ListenCallback c = new ListenCallback(ListenInternal);

            return(c.BeginInvoke(dbg_sock, con_sock, callback, con_sock ?? dbg_sock));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handler for C#'s asnync socket.
        /// </summary>
        private void ListenAsyncCallback(IAsyncResult result)
        {
            bool isOk = false;

            try {
                lock (lockObject) {
                    socket = listener.EndAcceptSocket(result);
                }
                isOk = true;

                // start messaging
                isMessaging = true;
                ReadMessageSize();
            }
            catch (Exception ex) {
                isOk = false;
            }
            finally {
                ListenCallback callback = (ListenCallback)result.AsyncState;

                listener.Stop();
                isListening = false;
                callback(isOk);
            }
        }
Ejemplo n.º 3
0
 public SocketWrap(
     ConnectCallback ccb,
     ListenCallback lcb,
     ReceiveCallback rcb,
     DisconnectCallback dcb,
     ErrorCallback ecb)
 {
     _ccb = ccb;
     _rcb = rcb;
     _ecb = ecb;
     _lcb = lcb;
     _dcb = dcb;
     _recvBuf = new byte[1024];
 }
Ejemplo n.º 4
0
        public void StartServer(IPAddress ip, int port, SocketAcceptCallback socketaccept = null, SocketLostCallback socketLost = null, ListenErrorCallback listenError = null, ReceiveCallback receive = null, SendBefore send = null, ListenCallback listen = null)
        {
            _ip   = ip;
            _port = port;
            _socketaccpetcallback = socketaccept;
            _socketLostCallback   = socketLost;
            _receivecallback      = receive;
            _sendBefore           = send;
            _listenCallback       = listen;
            _listenErrorCallback  = listenError;

            var listenThread = new Thread(Listen);

            listenThread.IsBackground = true;
            listenThread.Start();
        }
Ejemplo n.º 5
0
        public static VirtualMachine EndListen(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            if (!asyncResult.IsCompleted)
            {
                asyncResult.AsyncWaitHandle.WaitOne();
            }

            AsyncResult    async = (AsyncResult)asyncResult;
            ListenCallback cb    = (ListenCallback)async.AsyncDelegate;

            return(cb.EndInvoke(asyncResult));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Listen for incoming connections asynchronously.
        /// </summary>
        /// <param name="callback">Called if a connection is established or on failure.</param>
        /// <exception cref="InvalidOperationException">Already connected or listening.</exception>
        /// <exception cref="SocketException">There was an error with the underlying TCP socket.</exception>
        public void ListenAsync(ListenCallback callback)
        {
            if (IsConnected || isListening)
            {
                throw new InvalidOperationException("Already connected.");
            }

            try {
                isListening = true;
                listener.Start();
                listener.BeginAcceptSocket(ListenAsyncCallback, callback);
            }
            catch (Exception ex) {
                callback(false);
                listener.Stop();
                throw;
            }
        }
		public static IAsyncResult BeginListen (IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback,
			out int dbg_port, out int con_port)
		{
			dbg_port = con_port = 0;

			Socket dbg_sock = null;
			Socket con_sock = null;

			dbg_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			dbg_sock.Bind (dbg_ep);
			dbg_sock.Listen (1000);
			dbg_port = ((IPEndPoint) dbg_sock.LocalEndPoint).Port;

			if (con_ep != null) {
				con_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
				con_sock.Bind (con_ep);
				con_sock.Listen (1000);
				con_port = ((IPEndPoint) con_sock.LocalEndPoint).Port;
			}

			ListenCallback c = new ListenCallback (ListenInternal);
			return c.BeginInvoke (dbg_sock, con_sock, callback, con_sock ?? dbg_sock);
		}
 /// <summary>
 /// Listens for incoming connections. Listening state practically does not exist, so it
 /// instantly goes into connected state.
 /// </summary>
 /// <exception cref="UnauthorizedAccessException">Access is denied or port is already open by another process.</exception>
 /// <exception cref="ArgumentOutOfRangeException">One or more properties of the instance are invalid.</exception>
 /// <exception cref="ArgumentException">name does not begin with "COM" or file type of port is not supported.</exception>
 /// <exception cref="IOException">The port is in an invalid state.</exception>
 /// <exception cref="InvalidOperationException">The port is already open by this instance.</exception>
 public void ListenAsync(ListenCallback callback)
 {
     port.Open();
 }