Example #1
0
        internal int StartUpdateServer()
        {
            base.Start();
            Task.Run(() =>
            {
                try
                {
                    IFormatter formatter = new BinaryFormatter();
                    using (Socket s = AcceptSocket())
                        using (var ns = new NetworkStream(s))
                        {
                            while (!serverShutdown)
                            {
                                int numUpdates = SerDe.ReadInt(ns);
                                for (int i = 0; i < numUpdates; i++)
                                {
                                    var ms = new MemoryStream(SerDe.ReadBytes(ns));
                                    KeyValuePair <int, dynamic> update = (KeyValuePair <int, dynamic>)formatter.Deserialize(ms);

                                    if (Accumulator.accumulatorRegistry.ContainsKey(update.Key))
                                    {
                                        Accumulator accumulator = Accumulator.accumulatorRegistry[update.Key];
                                        accumulator.GetType().GetMethod("Add").Invoke(accumulator, new object[] { update.Value });
                                    }
                                    else
                                    {
                                        Console.Error.WriteLine("WARN: cann't find update.Key: {0} for accumulator, will create a new one", update.Key);
                                        var genericAccumulatorType  = typeof(Accumulator <>);
                                        var specificAccumulatorType = genericAccumulatorType.MakeGenericType(update.Value.GetType());
                                        Activator.CreateInstance(specificAccumulatorType, new object[] { update.Key, update.Value });
                                    }
                                }
                                ns.WriteByte((byte)1); // acknowledge byte other than -1
                                ns.Flush();
                            }
                        }
                }
                catch (SocketException e)
                {
                    if (e.ErrorCode != 10004)   // A blocking operation was interrupted by a call to WSACancelBlockingCall - TcpListener.Stop cancelled AccepSocket as expected
                    {
                        throw e;
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                    throw;
                }
            });

            return((base.LocalEndpoint as IPEndPoint).Port);
        }
Example #2
0
        internal int StartUpdateServer()
        {
            base.Start();
            Task.Run(() =>
            {
                try
                {
                    IFormatter formatter = new BinaryFormatter();
                    using (Socket s = AcceptSocket())
                        using (var ns = new NetworkStream(s))
                        {
                            while (!serverShutdown)
                            {
                                int numUpdates = SerDe.ReadInt(ns);
                                for (int i = 0; i < numUpdates; i++)
                                {
                                    var ms = new MemoryStream(SerDe.ReadBytes(ns));
                                    KeyValuePair <int, dynamic> update = (KeyValuePair <int, dynamic>)formatter.Deserialize(ms);
                                    Accumulator accumulator            = Accumulator.accumulatorRegistry[update.Key];
                                    accumulator.GetType().GetMethod("Add").Invoke(accumulator, new object[] { update.Value });
                                }
                                ns.WriteByte((byte)1); // acknowledge byte other than -1
                                ns.Flush();
                                Thread.Sleep(1000);
                            }
                        }
                }
                catch (SocketException e)
                {
                    if (e.ErrorCode != 10004)   // A blocking operation was interrupted by a call to WSACancelBlockingCall - TcpListener.Stop cancelled AccepSocket as expected
                    {
                        throw e;
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                    throw;
                }
            });

            return((base.LocalEndpoint as IPEndPoint).Port);
        }