예제 #1
0
        void ReadSocket(IAsyncResult ir)
        {
            // get state
            socketinfo si = (socketinfo)ir.AsyncState;
            // get listener
            Socket list = si.sock;
            // get data from client
            Socket client = (Socket)list.EndAccept(ir);
            // get socket info for client
            socketinfo csi = new socketinfo(client, si.buffer, si.startidx);
            // get client name
            string name = client.RemoteEndPoint.ToString();

            // notify
            debug("Connection from: " + name);
            try
            {
                // receive data that arrives
                client.BeginReceive(csi.buffer, csi.startidx, csi.freebuffersize, SocketFlags.None, new AsyncCallback(ReadData), csi);
            }
            catch (SocketException ex)
            {
                v(client.RemoteEndPoint + " " + ex.SocketErrorCode + ex.Message + ex.StackTrace);
            }
            // wait for new connection
            list.BeginAccept(new AsyncCallback(ReadSocket), si);
        }
예제 #2
0
 void ReadSocket(IAsyncResult ir)
 {
     // get state
     socketinfo si = (socketinfo)ir.AsyncState;
     // get listener
     Socket list = si.sock;
     // get data from client
     Socket client = (Socket)list.EndAccept(ir);
     // get socket info for client
     socketinfo csi = new socketinfo(client,si.buffer,si.startidx);
     // get client name
     string name = client.RemoteEndPoint.ToString();
     // notify
     debug("Connection from: " + name);
     try
     {
         // receive data that arrives
         client.BeginReceive(csi.buffer, csi.startidx, csi.freebuffersize, SocketFlags.None, new AsyncCallback(ReadData), csi);
     }
     catch (SocketException ex)
     {
         v(client.RemoteEndPoint + " " + ex.SocketErrorCode + ex.Message + ex.StackTrace);
     }
     // wait for new connection
     list.BeginAccept(new AsyncCallback(ReadSocket), si);
     
 }
예제 #3
0
        void ReadData(IAsyncResult ir)
        {
            //get our state
            socketinfo csi       = (socketinfo)ir.AsyncState;
            bool       connected = csi.sock.Connected;

            try
            {
                SocketError se = SocketError.Success;
                // see how much data was read for this call
                int len = csi.sock.EndReceive(ir, out se);
                // receive any data
                if (len > 0)
                {
                    try
                    {
                        // get messages from data
                        v("srv data received: socket_status: " + se.ToString() + " data size: " + csi.freebuffersize + " data: " + HexToString(csi.buffer, len));
                        Message[] msgs = Message.gotmessages(ref csi.buffer, ref csi.startidx);
                        v("srv messages received: " + msgs.Length + " messages.  ");
                        // handle messages
                        for (int i = 0; i < msgs.Length; i++)
                        {
                            Message m = msgs[i];
                            v("srv message# " + i + " size: " + m.ByteLength + " type: " + m.Type + " tag: " + m.Tag + " data: " + m.Content);
                            handle(m.Type, m.Content, csi.sock);
                        }
                        v("srv handled " + msgs.Length + " messages.");
                    }
                    catch (Exception ex)
                    {
                        debug(ex.Message + ex.StackTrace);
                    }
                }
                else
                {
                    // implies possible disconnect, verify
                    connected = issocketconnected(csi.sock);
                }
            }
            catch (SocketException ex)
            {
                debug(ex.SocketErrorCode + ex.Message + ex.StackTrace);
            }
            catch (Exception ex)
            {
                debug(ex.Message + ex.StackTrace);
            }

            try
            {
                // wait for more data to arrive
                if (connected)
                {
                    Thread.Sleep(100);
                    csi.sock.BeginReceive(csi.buffer, csi.startidx, csi.freebuffersize, SocketFlags.None, new AsyncCallback(ReadData), csi);
                }
                else
                {
                    csi.sock.Close();
                }
            }
            catch (SocketException ex)
            {
                // host likely disconnected
                v("disconnected.  msg: " + ex.SocketErrorCode + ex.Message + ex.StackTrace);
                csi.sock.Close();
            }
        }
예제 #4
0
 internal socketinfo(socketinfo si)
 {
     sock     = si.sock;
     startidx = si.startidx;
     buffer   = si.buffer;
 }
예제 #5
0
 internal socketinfo(socketinfo si)
 {
     sock = si.sock;
     startidx = si.startidx;
     buffer = si.buffer;
 }