Example #1
0
    private static void Service(object sender, SocketAsyncEventArgs e)
    {
        if (e.SocketError != SocketError.Success)
        {
            LoopToStartAccept();
            HandleBadAccept(e);
            return;
        }

        LoopToStartAccept();


        try
        {
            using (Socket soc = e.AcceptSocket)
            {
                var rg = new Random();
    #if (VERBOSE)
                Console.WriteLine("New socket: " + rg.Next(0, 1000000));
    #endif
                //soc.NoDelay = true;
                soc.ReceiveTimeout = 60000;
                soc.SendTimeout    = 60000;
                using (Stream stream = new NetworkStream(soc))
                    using (BinaryWriter bw = new BinaryWriter(stream))
                    {
                        while (true) //reuse same connection for many commands
                        {
                            byte[] data = new byte[1024];
                            using (MemoryStream ms = new MemoryStream())
                            {
                                int numBytesRead = 0;
                                while (numBytesRead < 4)
                                {
                                    int read = 0;
                                    try
                                    {
                                        read = stream.Read(data, numBytesRead, data.Length - numBytesRead);
                                    }
                                    catch (Exception ex)
                                    {
                                        //client closed connection
                                        return;
                                    }
                                    numBytesRead += read;
                                    if (read <= 0)
                                    {
                                        //throw new Exception("Read <= 0: " + read);
                                        //client closed connection
                                        return;
                                    }
                                }
                                numBytesRead -= 4;
                                var total = BitConverter.ToInt32(new byte[4] {
                                    data[0], data[1], data[2], data[3]
                                }, 0);
                                if (total == -3) //ping
                                {
                                    //pong
                                    bw.Write(BitConverter.GetBytes(-3));
                                    bw.Flush();
                                    continue;
                                }
                                if (numBytesRead > 0)
                                {
                                    var finput = new byte[numBytesRead];
                                    for (int i = 0; i < numBytesRead; i++)
                                    {
                                        finput[i] = data[4 + i];
                                    }
                                    ms.Write(finput, 0, numBytesRead);
                                }
                                total -= numBytesRead;
                                while (total > 0)
                                {
                                    numBytesRead = stream.Read(data, 0, data.Length);
                                    if (numBytesRead <= 0)
                                    {
                                        throw new Exception("numBytesRead <= 0: " + numBytesRead);
                                    }
                                    ms.Write(data, 0, numBytesRead);
                                    total -= numBytesRead;
                                }
                                var input  = ms.ToArray();
                                var pinger = new Pinger()
                                {
                                    bw = bw
                                };
                                ThreadPool.QueueUserWorkItem(f => { pinger.Do(); });
                                var output = ServerLogic.Logic.Execute(input, db_path);
                                var length = BitConverter.GetBytes(output.Length);
                                pinger.Done = true;
                                lock (pinger._lock)
                                {
                                    bw.Write(length);
                                    bw.Write(output);
                                    bw.Flush();
                                }
                            }
                        }
                    }
            }
        }
        catch (Exception ex)
        {
    #if (VERBOSE)
            Console.WriteLine("Socket error: " + ex.Message);
    #endif
            //try
            //{
            //    var rg = new Random();
            //    File.WriteAllText("sock_error_" + rg.Next() + ".txt", ex.Message + " " + ex.StackTrace + (ex.InnerException != null ? (" " + ex.InnerException.Message + " " + ex.InnerException.StackTrace) : ""));
            //}
            //catch (Exception) { }
            return;
        }
        finally
        {
    #if (VERBOSE)
            Console.WriteLine("Listener finally ");
    #endif
        }
    }