Exemple #1
0
        private static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("usage: local_thr <bind-to> <message-size> <message-count>");
                return(1);
            }

            string bindTo       = args[0];
            int    messageSize  = int.Parse(args[1]);
            int    messageCount = int.Parse(args[2]);

            using (var pullSocket = new PullSocket())
            {
                pullSocket.Bind(bindTo);

                var msg = new Msg();
                msg.InitEmpty();

                pullSocket.Receive(ref msg);

                var stopWatch = Stopwatch.StartNew();
                for (int i = 0; i != messageCount - 1; i++)
                {
                    pullSocket.Receive(ref msg);
                    if (msg.Size != messageSize)
                    {
                        Console.WriteLine("message of incorrect size received. Received: " + msg.Size + " Expected: " + messageSize);
                        return(-1);
                    }
                }
                stopWatch.Stop();
                var millisecondsElapsed = stopWatch.ElapsedMilliseconds;
                if (millisecondsElapsed == 0)
                {
                    millisecondsElapsed = 1;
                }

                msg.Close();

                double messagesPerSecond = (double)messageCount / millisecondsElapsed * 1000;
                double megabits          = messagesPerSecond * messageSize * 8 / 1000000;

                Console.WriteLine("message size: {0} [B]", messageSize);
                Console.WriteLine("message count: {0}", messageCount);
                Console.WriteLine("mean throughput: {0:0.000} [msg/s]", messagesPerSecond);
                Console.WriteLine("mean throughput: {0:0.000} [Mb/s]", megabits);
            }

            return(0);
        }
Exemple #2
0
        protected override void Consume(PullSocket socket, int messageSize)
        {
            var msg = new Msg();

            msg.InitEmpty();

            for (int i = 0; i < MsgCount; i++)
            {
                socket.Receive(ref msg);
                Debug.Assert(msg.Data.Length == messageSize, "Message length was different from expected size.");
                Debug.Assert(msg.Data[msg.Size / 2] == 0x42, "Message did not contain verification data.");
            }
        }
Exemple #3
0
 void OnReceive()
 {
     try
     {
         if (!_pullSocket.HasIn)
         {
             return;
         }
         byte[] data      = _pullSocket.Receive();
         int    offset    = 0;
         var    converter = EndianBitConverter.Big;
         long   netHandle = converter.ToInt64(data, offset);
         offset += 8;
         int length = converter.ToUInt16(data, offset);
         offset += 2;
         int cmdName = converter.ToInt32(data, offset);
         offset += 4;
         RequestFactor factor;
         if (_commands.TryGetValue(cmdName.ToString("D"), out factor))
         {
             var stream = new MemoryStream();
             try
             {
                 stream.Write(data, offset, length - 4);
                 stream.Position = 0;
                 var package = factor.CreatePackageMethod.Invoke(null, new object[] { stream });
                 _actionsWaiting.Enqueue(new Tuple <long, CommandBase, object>(netHandle, factor.Command, package));
                 stream.Dispose();
             }
             catch (ProtoException ex)
             {
                 stream.Dispose();
                 //协议错误,考虑要不要断开
                 Logger.Debug("unknow request:{0} from {1}", cmdName.ToString("D"), netHandle);
             }
         }
         else
         {
             //不认识的协议,考虑要不要断开
             Logger.Debug("unknow request:{0} from {1}", cmdName.ToString("D"), netHandle);
         }
     }
     catch (Exception ex)
     {
         Logger.Fatal(ex);
     }
 }
Exemple #4
0
 void ReceiveGameServer()
 {
     while (!_cancelToken.IsCancellationRequested)
     {
         try
         {
             Thread.Sleep(1);
             if (!_pullSocket.HasIn)
             {
                 continue;
             }
             byte[] data      = _pullSocket.Receive();
             var    converter = EndianBitConverter.Big;
             int    offset    = 0;
             int    userCount = converter.ToInt32(data, offset);
             offset += 4;
             var sessions = new List <PlayerSession>();
             if (userCount == 0)
             {
                 //群发
                 sessions.AddRange(GetAllSessions().Where(s => s.IsLogin));
             }
             else
             {
                 for (int i = 0; i < userCount; i++)
                 {
                     long netHandle = converter.ToInt64(data, offset);
                     offset += 8;
                     sessions.AddRange(this.GetSessions(s => s.NetHandle == netHandle));
                 }
             }
             var package = new ArraySegment <byte>(data, offset, data.Length - offset);
             foreach (PlayerSession session in sessions)
             {
                 session.Send(package);
             }
         }
         catch (Exception ex)
         {
             Logger.Error(ex.Message + Environment.NewLine + ex.StackTrace);
         }
     }
 }