Exemplo n.º 1
0
        /// <summary>
        /// Bind socket
        /// </summary>
        /// <param name="local">Local EndPoint</param>
        public static TuringSocket Bind(IPEndPoint local)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(local);
            socket.Listen(50);

            TuringSocket ret = new TuringSocket(socket, local);

            socket.BeginAccept(ret.OnAccept, ret);
            return(ret);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Connect to
        /// </summary>
        /// <param name="remote">Remote EndPoint</param>
        public static TuringSocket ConnectTo(IPEndPoint remote)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(remote);

            TuringSocket ret = new TuringSocket(socket, remote);

            // WaitMessage
            ReadMessageAsync(new TuringMessageState(ret));

            return(ret);
        }
Exemplo n.º 3
0
        void RaiseOnMessage(TuringSocket sender, TuringMessage message)
        {
            if (message == null)
            {
                return;
            }

            // Enqueue
            if (EnqueueMessages)
            {
                lock (_Readed) { _Readed.Enqueue(message); }
                _Signal.Set();
            }
            OnMessage?.Invoke(sender, message);
        }
Exemplo n.º 4
0
        void OnAccept(IAsyncResult result)
        {
            // Get Sockets
            TuringSocket main;
            Socket       source;

            try
            {
                main = (TuringSocket)result.AsyncState;
                if (main == null)
                {
                    return;
                }

                source = main._Socket.EndAccept(result);
                if (source == null)
                {
                    return;
                }
            }
            catch { return; }

            // Connect
            try
            {
                TuringSocket ret = new TuringSocket(source, (IPEndPoint)source.RemoteEndPoint);
                // CopyEvents
                ret.OnMessage += RaiseOnMessage;

                ReadMessageAsync(new TuringMessageState(ret));
            }
            catch// (Exception e)
            {
            }

            // Re-accept
            try { main._Socket.BeginAccept(OnAccept, main); } catch { }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="source">Source socket</param>
 public TuringMessageState(TuringSocket source)
 {
     Source = source;
     Clear();
 }