public BlockChainVotings()
        {
            NetworkComms.DisableLogging();
            LiteLogger logger = new LiteLogger(LiteLogger.LogMode.ConsoleOnly);

            NetworkComms.EnableLogging(logger);


            db  = new VotingsDB();
            net = new Network();

            db.ConnectToDBAsync();

            t = new Timer(CommonHelpers.PeersCheckInterval * 10);

            pendingBlocks       = new Dictionary <Block, DateTime>();
            pendingTransactions = new Dictionary <Transaction, DateTime>();

            net.OnBlocksMessage              += OnBlockMessage;
            net.OnRequestBlocksMessage       += OnRequestBlocksMessage;
            net.OnRequestTransactionsMessage += OnRequestTransactionsMessage;
            net.OnTransactionsMessage        += OnTransactionsMessage;

            t.Elapsed += CheckDeleteOldPendingItems;
        }
Пример #2
0
        public static void Start()
        {
            if (_started)
            {
                return;
            }
            _started = true;

            //NetworkComms.EnableLogging(new LiteLogger(LiteLogger.LogMode.ConsoleOnly));
            NetworkComms.DisableLogging();

            NetworkComms.IgnoreUnknownPacketTypes = true;
            var serializer = DPSManager.GetDataSerializer <ProtobufSerializer>();

            NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(serializer,
                                                                            NetworkComms.DefaultSendReceiveOptions.DataProcessors, NetworkComms.DefaultSendReceiveOptions.Options);

            NetworkComms.AppendGlobalIncomingPacketHandler <ServerInfo>(ServerInfo.GetHeader(), ServerInfoReceived);
            NetworkComms.AppendGlobalIncomingPacketHandler <Logout>(Network.Logout.GetHeader(), LogoutHandlger);
            NetworkComms.AppendGlobalIncomingPacketHandler <string>("PING", PingHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Ping>(Ping.GetHeader(), PingPongHandler);
            PeerDiscovery.EnableDiscoverable(PeerDiscovery.DiscoveryMethod.UDPBroadcast);
            PeerDiscovery.OnPeerDiscovered += OnPeerDiscovered;

            Connection.StartListening(ConnectionType.UDP, new IPEndPoint(IPAddress.Any, 0));
            PeerDiscovery.DiscoverPeersAsync(PeerDiscovery.DiscoveryMethod.UDPBroadcast);
        }
Пример #3
0
        private Client()
        {
            NetworkComms.DisableLogging();

            NetworkComms.IgnoreUnknownPacketTypes = true;
            var serializer = DPSManager.GetDataSerializer <ProtobufSerializer>();

            NetworkComms.DefaultSendReceiveOptions.DataProcessors.Add(
                DPSManager.GetDataProcessor <RijndaelPSKEncrypter>());
            NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(serializer,
                                                                            NetworkComms.DefaultSendReceiveOptions.DataProcessors, NetworkComms.DefaultSendReceiveOptions.Options);

            RijndaelPSKEncrypter.AddPasswordToOptions(NetworkComms.DefaultSendReceiveOptions.Options, Utility.PSK);

            NetworkComms.AppendGlobalIncomingPacketHandler <Ping>(Ping.Header, PingHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <ClassInfo>(ClassInfo.Header, ClassInfoHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <InstructorLogin>(InstructorLogin.Header, InstructorLoginHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <ClientInfo>(ClientInfo.Header, ClientInfoHandler);
            PeerDiscovery.EnableDiscoverable(PeerDiscovery.DiscoveryMethod.UDPBroadcast);

            PeerDiscovery.OnPeerDiscovered += OnPeerDiscovered;

            //NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>("PartialFileData", IncomingPartialFileData);

            //NetworkComms.AppendGlobalIncomingPacketHandler<SendInfo>("PartialFileDataInfo",
            //    IncomingPartialFileDataInfo);

            //NetworkComms.AppendGlobalConnectionCloseHandler(OnConnectionClose);

            PeerDiscovery.DiscoverPeersAsync(PeerDiscovery.DiscoveryMethod.UDPBroadcast);
        }
Пример #4
0
        static void Main(string[] Args)
        {
            Application.EnableVisualStyles();   //这行实现 XP 可视风格
            Application.SetCompatibleTextRenderingDefault(false);
            Application.DoEvents();
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            NetworkComms.DisableLogging();
            //NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions<ProtobufSerializer>();
            //上面的写法由于在.net Framework V4中出错,更如下写法
            SendReceiveOptions nullCompressionSRO = null;// new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null);

            NetworkComms.DefaultSendReceiveOptions = nullCompressionSRO;
            //在全局处理函数中忽略相关消息 不添加日志
            NetworkComms.IgnoreUnknownPacketTypes = true;

            //连接服务器完成******************************************

            ////初始化主窗口
            //MsgForm msgForm = new MsgForm();


            ////显示登录窗口
            //LoginForm loginForm = new LoginForm(applicationName);

            ////如果登陆成功,则进入主窗口

            //if (loginForm.ShowDialog() == DialogResult.OK)
            //{
            //    msgForm.Initialize();
            //    Application.Run(msgForm);

            //}
            //else
            //{
            //    return;
            //}

            //初始化窗口

            WIMClient.Skin.QQMainForm mainForm = new Skin.QQMainForm();

            WIMClient.Skin.QQLoginForm qqLoginForm = new WIMClient.Skin.QQLoginForm();

            if (qqLoginForm.ShowDialog() == DialogResult.OK)
            {
                mainForm.Initialize();
                Application.Run(mainForm);
            }
        }
Пример #5
0
        public static void Start()
        {
            if (_started)
            {
                return;
            }
            _started = true;

            NetworkComms.DisableLogging();

            var serializer = DPSManager.GetDataSerializer <ProtobufSerializer>();

            NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(serializer,
                                                                            NetworkComms.DefaultSendReceiveOptions.DataProcessors, NetworkComms.DefaultSendReceiveOptions.Options);

            PeerDiscovery.EnableDiscoverable(PeerDiscovery.DiscoveryMethod.UDPBroadcast);

            NetworkComms.AppendGlobalIncomingPacketHandler <AndroidInfo>(AndroidInfo.GetHeader(), AndroidHandler.HandShakeHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <StudentInfoRequest>(StudentInfoRequest.GetHeader(), AndroidHandler.StudentInfoRequested);
            NetworkComms.AppendGlobalIncomingPacketHandler <EndPointInfo>(EndPointInfo.GetHeader(), HandShakeHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <GetWork>(GetWork.GetHeader(), GetWorkHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Login>(Login.GetHeader(), LoginHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Logout>(Logout.GetHeader(), LogoutHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <SchedulesRequest>(SchedulesRequest.GetHeader(), AndroidHandler.ScheduleRequestHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <EnrollRequest>(EnrollRequest.GetHeader(), AndroidHandler.EnrollRequestHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <RegisterStudent>(RegisterStudent.GetHeader(), AndroidHandler.RegisterStudentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <SaveWork>(SaveWork.GetHeader(), SaveWorkHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <GetCourses>(GetCourses.GetHeader(), AndroidHandler.GetCoursesHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <GetCoursesDesktop>(GetCoursesDesktop.GetHeader(), GetCoursesHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Pong>(Pong.GetHeader(), PongHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <EnrollStudent>(EnrollStudent.GetHeader(), EnrollStudentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <StartEnrollment>(StartEnrollment.GetHeader(), AndroidHandler.StartEnrollmentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <AddSchedule>(AddSchedule.GetHeader(), AndroidHandler.AddScheduleHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <CommitEnrollment>(CommitEnrollment.GetHeader(), AndroidHandler.CommitEnrollmentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <StatusRequest>(StatusRequest.GetHeader(), AndroidHandler.StatusRequestHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <CancelEnrollment>(CancelEnrollment.GetHeader(), AndroidHandler.CancelEnrollmentHandler);
            //  try
            // {
            Connection.StartListening(ConnectionType.UDP, new IPEndPoint(IPAddress.Any, 0), true);
            // }
            // catch (Exception e)
            // {
            //
            // }
        }
Пример #6
0
        public Tracker()
        {
            this.Peers  = new List <Peer>();
            this.Status = TrackerStatus.Stopped;

            t = new Timer(CommonHelpers.CheckAliveInterval);

            NetworkComms.DisableLogging();
            if (File.Exists("BlockChainVotingsTracker_log.txt"))
            {
                File.Delete("BlockChainVotingsTracker_log.txt");
            }
            LiteLogger logger = new LiteLogger(LiteLogger.LogMode.ConsoleAndLogFile, "BlockChainVotingsTracker_log.txt");

            NetworkComms.EnableLogging(logger);

            NetworkComms.AppendGlobalConnectionEstablishHandler(OnConnectPeer);
        }
Пример #7
0
        private Server()
        {
            var serializer = DPSManager.GetDataSerializer <ProtobufSerializer>();

            NetworkComms.DefaultSendReceiveOptions.DataProcessors.Add(
                DPSManager.GetDataProcessor <RijndaelPSKEncrypter>());

            NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(serializer,
                                                                            NetworkComms.DefaultSendReceiveOptions.DataProcessors, NetworkComms.DefaultSendReceiveOptions.Options);

            RijndaelPSKEncrypter.AddPasswordToOptions(NetworkComms.DefaultSendReceiveOptions.Options, Utility.PSK);

            NetworkComms.DisableLogging();

            NetworkComms.AppendGlobalIncomingPacketHandler <LogonInfo>(LogonInfo.Header, LogonInfoHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Login>(Login.Header, LoginHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Pong>(Pong.Header, PongHandler);

            PeerDiscovery.EnableDiscoverable(PeerDiscovery.DiscoveryMethod.UDPBroadcast);
        }
Пример #8
0
        private void MsgForm_Load(object sender, EventArgs e)
        {
            //不启用日志记录

            NetworkComms.DisableLogging();

            //用户状态改变通知<5>
            //NetworkComms.AppendGlobalIncomingPacketHandler<UserStateContract>("UserStateNotify", IncomingUserStateNotify);


            //服务器通知客户断开 一般是由于有新连接进入<6>

            NetworkComms.AppendGlobalIncomingPacketHandler <string>("CloseConnection", IncomingCloseConn);

            //收到服务器转发来的聊天消息
            NetworkComms.AppendGlobalIncomingPacketHandler <MsgEntity>("ServerChatMessage", IncomingChatMessage);

            NetworkComms.AppendGlobalIncomingPacketHandler <MsgEntity>("ClientChatMessage", IncomingClientChatMessage);

            //NetworkComms.AppendGlobalIncomingPacketHandler<SetUpP2PContract>("SetupP2PMessage", IncomingSetupP2PMessage);
            GetMyOfflineMessage();
        }
Пример #9
0
        static void Main(string[] args)
        {
            TStuffLog.LogLevel     = LogLevel.Info;
            TStuffLog.LogFileLevel = LogLevel.Info;
            TStuffLog.LogActions.Add((message, serializedObject, level, member, filepat, line) =>
            {
                if (TStuffLog.LogLevel > level)
                {
                    return;
                }
                switch (level)
                {
                case LogLevel.Trace:
                    Console.ForegroundColor = ConsoleColor.Green;
                    break;

                case LogLevel.Debug:
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    break;

                case LogLevel.Info:

                    break;

                case LogLevel.Warning:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;

                case LogLevel.Error:
                case LogLevel.Fatal:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(level), level, null);
                }
                Console.WriteLine("[{5},{0}/{1}:{2}, Level:{3}] Message: {4}", filepat.Split('\\').Last(), member, line, level.ToString(), message, DateTime.Now.ToString("T"));
                Console.ResetColor();
            });

            NetworkComms.DisableLogging();

            if (File.Exists("config.json"))
            {
                GConfig.D = JsonConvert.DeserializeObject <GameServerConfiguration>(File.ReadAllText("config.json"));
            }
            else
            {
                GConfig.D = new GameServerConfiguration();
                File.WriteAllText("config.json", JsonConvert.SerializeObject(GConfig.D, Formatting.Indented));
            }
            TStuffLog.Info("Server Started");
            TStuffLog.LogLevel     = GConfig.D.ServerLogLevel;
            TStuffLog.LogFileLevel = GConfig.D.LogFileLevel;

            DataSerializer ds = DPSManager.GetDataSerializer <ProtobufSerializer>();

            NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(ds, new List <DataProcessor>(), new Dictionary <string, string>());
            var gamecontext = SetupGameRegister();

            SetupGlobalNetworkHandler(gamecontext);

            GConfig.D.GameSimulationTime = 400;
            var cont = new LoginService(gamecontext);
            var map  = new CreateGameService(gamecontext);


            Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, GConfig.D.ServerPort));
            //Connectc fake user
            gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 0", 0);
            gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 1", 1);
            gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 2", 2);
            gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 3", 3);
            gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 4", 4);


            //File.WriteAllText("mobs.json",JsonConvert.SerializeObject(gamecontext.Mobs));
            //File.WriteAllText("tower.json", JsonConvert.SerializeObject(gamecontext.Towers));
            //File.WriteAllText("race_mob.json", JsonConvert.SerializeObject(gamecontext.MobRaces));
            //File.WriteAllText("race_tower.json", JsonConvert.SerializeObject(gamecontext.TowerRaces));

            Console.ReadLine();
        }
Пример #10
0
        //开始监听和设置
        private void StartListening()
        {
            string strIP   = txtIP.Text.Trim();
            string strport = txtPort.Text.Trim();

            if (string.IsNullOrWhiteSpace(strIP))
            {
                MessageBox.Show("服务器地址不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(strport))
            {
                MessageBox.Show("端口号不能为空");
                return;
            }
            int port = 0;

            if (int.TryParse(strport, out port))
            {
                if (port < 2000)
                {
                    MessageBox.Show("端口号取值范围为2000到65535");
                    return;
                }
                if (port > 65535)
                {
                    MessageBox.Show("端口号取值范围为2000到65535");
                    return;
                }
                if (IsUse(port))
                {
                    MessageBox.Show("当前端口号已被占用");
                    return;
                }
            }
            else
            {
                MessageBox.Show("端口号必须是整数");
                return;
            }
            //配置日志记录器
            //ILogger logger = new LiteLogger(LiteLogger.LogMode.ConsoleAndLogFile, "ServerLogFile_" + NetworkComms.NetworkIdentifier + ".txt");
            //NetworkComms.EnableLogging(logger);
            //不启用日志记录
            NetworkComms.DisableLogging();
            //如果某客户端离线,触发此方法
            NetworkComms.AppendGlobalConnectionCloseHandler(HandleConnectionClosed);


            #region 用户登录和聊天相关

            //客户端发来的聊天信息  转发之
            NetworkComms.AppendGlobalIncomingPacketHandler <MsgEntity>("ChatMessage", IncomingChatMessage);

            //客户端获取好友列表
            NetworkComms.AppendGlobalIncomingPacketHandler <string>("GetFriends", IncomingGetFriends);

            //获取所有在线用户列表

            NetworkComms.AppendGlobalIncomingPacketHandler <string>("GetOnlineUser", IncomingGetOnlineUser);

            //用户登录
            NetworkComms.AppendGlobalIncomingPacketHandler <IMUserInfo>("UserLogin", IncomingLoginHandler);
            //更改密码
            NetworkComms.AppendGlobalIncomingPacketHandler <UserPswEntity>("ChangePsw", IncomingChangePsw);

            //注册新用户
            //NetworkComms.AppendGlobalIncomingPacketHandler<RcUsers>("RegUser", IncomingRegisterUser);

            #endregion

            #region 用户和组相关
            //====================================================================================================
            ////获取所有用户组
            //NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetGroup", IncomingGetGrooup);



            // 获取离线消息
            NetworkComms.AppendGlobalIncomingPacketHandler <UserIDContract>("GetMyOffLineMsg", IncomingMyOffLineMsg);

            //获取所有在线用户的P2P信息
            NetworkComms.AppendGlobalIncomingPacketHandler <string>("GetP2PInfo", IncomingP2PInfo);

            #endregion
            IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(strIP), port);
            TCPConnection.StartListening(thePoint, false);
            AddLineToLog("初始化完成,正在监听:");
            AddLineToLog("IP地址" + strIP + "--" + "端口" + port.ToString());
        }