Exemplo n.º 1
0
 private void InvokeOnAccept(EasySocket client)
 {
     if (OnAccept != null)
     {
         OnAccept(this, client);
     }
 }
Exemplo n.º 2
0
 public static EasySocket Listen(IPEndPoint local)
 {
     EasySocket socket = new EasySocket(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp));
     socket.m_Socket.Bind(local);
     socket.m_Socket.Listen(5);
     socket.m_Socket.BeginAccept(AcceptEvent, socket);
     return socket;
 }
Exemplo n.º 3
0
        public static EasySocket Listen(IPEndPoint local)
        {
            EasySocket socket = new EasySocket(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp));

            socket.m_Socket.Bind(local);
            socket.m_Socket.Listen(5);
            socket.m_Socket.BeginAccept(AcceptEvent, socket);
            return(socket);
        }
Exemplo n.º 4
0
        static void OnAccept(EasySocket socket, EasySocket client)
        {
            System.Console.Out.WriteLine("CONNECTION!");

            client.OnRead += OnRead;
            client.OnClose += OnClose;

            client.Write(Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\n\rContent-Length: 3\n\r\n\rHi!"));
            client.Close();
        }
Exemplo n.º 5
0
        private static void SendEvent(IAsyncResult rs)
        {
            EasySocket es = rs.AsyncState as EasySocket;

            // complete the operation; we don't do anything with it
            int bytes = es.m_Socket.EndSend(rs);

            // signal callback
            IOLoop.Instance.PushEvent(() => es.InvokeOnWrite());
        }
Exemplo n.º 6
0
        private static void AcceptEvent(IAsyncResult rs)
        {
            EasySocket es = rs.AsyncState as EasySocket;

            // read in the new connection
            EasySocket client = new EasySocket(es.m_Socket.EndAccept(rs));

            // send the event
            IOLoop.Instance.PushEvent(() => es.InvokeOnAccept(client));

            // configure client for events
            client.m_Socket.BeginReceive(client.m_ReadBuffer, 0, client.m_ReadBuffer.Length, SocketFlags.None, ReceiveEvent, client);

            // continue listening for events
            es.m_Socket.BeginAccept(AcceptEvent, es);
        }
Exemplo n.º 7
0
        private static void ConnectEvent(IAsyncResult rs)
        {
            EasySocket es = rs.AsyncState as EasySocket;

            // end the connection request
            es.m_Socket.EndConnect(rs);

            // check if the connection failed due to timeout or successfully connected
            if (!es.m_Socket.Connected)
            {
                IOLoop.Instance.PushEvent(() => es.InvokeOnTimeout());
                return;
            }

            IOLoop.Instance.PushEvent(() => es.InvokeOnConnect());

            // set up event handlers
            es.m_Socket.BeginReceive(es.m_ReadBuffer, 0, es.m_ReadBuffer.Length, SocketFlags.None, ReceiveEvent, es);
        }
Exemplo n.º 8
0
        private static void ReceiveEvent(IAsyncResult rs)
        {
            EasySocket es = rs.AsyncState as EasySocket;

            // read data from socket, and invoke either the OnClose or OnReceive callback as appropriate
            int read = es.m_Socket.EndReceive(rs);

            if (read <= 0)
            {
                IOLoop.Instance.PushEvent(() => es.InvokeOnClose());
            }
            else
            {
                IOLoop.Instance.PushEvent(() => es.InvokeOnRead(es.m_ReadBuffer, read));

                // create a new buffer for reading, since the old one may not be processed for some time
                es.m_ReadBuffer = new byte[4096];

                // read another set of data, since the socket is not closed (yet)
                es.m_Socket.BeginReceive(es.m_ReadBuffer, 0, es.m_ReadBuffer.Length, SocketFlags.None, ReceiveEvent, es);
            }
        }
Exemplo n.º 9
0
 static void OnRead(EasySocket socket, byte[] bytes, int len)
 {
     if (len != 0)
         System.Console.Out.WriteLine(Encoding.UTF8.GetString(bytes, 0, len));
 }
Exemplo n.º 10
0
 static void OnClose(EasySocket socket)
 {
 }
Exemplo n.º 11
0
        private static void AcceptEvent(IAsyncResult rs)
        {
            EasySocket es = rs.AsyncState as EasySocket;

            // read in the new connection
            EasySocket client = new EasySocket(es.m_Socket.EndAccept(rs));

            // send the event
            IOLoop.Instance.PushEvent(() => es.InvokeOnAccept(client));

            // configure client for events
            client.m_Socket.BeginReceive(client.m_ReadBuffer, 0, client.m_ReadBuffer.Length, SocketFlags.None, ReceiveEvent, client);

            // continue listening for events
            es.m_Socket.BeginAccept(AcceptEvent, es);
        }
Exemplo n.º 12
0
 private void InvokeOnAccept(EasySocket client)
 {
     if (OnAccept != null)
         OnAccept(this, client);
 }