コード例 #1
0
ファイル: Program.cs プロジェクト: thedanieldude1/DanielCode
		public static void Main(string[] args){




			if(args.Length>0){
				server = new Server();
				Console.ReadLine();
			}
			else{
				Console.WriteLine("NAME: ");
				client = new Client(Console.ReadLine());
				ClientConsole();
			}
		
		}
コード例 #2
0
ファイル: Program.cs プロジェクト: Maartyl/cs-chan
    public static Client Start(string host) {
      var c2s = new Uri("chan://" + host + "/client2server");
      var s2c = new Uri("chan://" + host + "/server2client");
      var s = new Client();
      var ss = s.store;

      var cfg = NetChanConfig.MakeDefault<string>();
      var t3 = ss.PrepareClientReceiverForType(cfg); //... I need the task...
      var t4 = ss.PrepareClientSenderForType(cfg);

      ChanEvent.Listen(ss.GetReceiver<string>(s2c), Console.WriteLine);
      s.Sender = ss.GetSender<string>(c2s);

      s.Task = Task.WhenAny(t3, t4);
      return s;
    }
コード例 #3
0
 public frmShowFiles()
 {
     InitializeComponent();
     client = new Client();
 }
コード例 #4
0
ファイル: ServerManager.cs プロジェクト: Jaex/Chat
 private void ListenThread()
 {
     while (IsWorking)
     {
         try
         {
             TcpClient incomingClient = tcpListener.AcceptTcpClient();
             Client client = new Client(incomingClient);
             client.MessageReceived += ClientMessageReceived;
             client.Disconnected += ClientDisconnected;
             OnConsoleOutput(string.Format("Client connected: {0}", client.IP));
         }
         catch (SocketException)
         {
         }
     }
 }
コード例 #5
0
ファイル: ServerManager.cs プロジェクト: Jaex/Chat
        private void ClientMessageReceived(Client client, string text)
        {
            OnConsoleOutput(string.Format("{0} {1}: {2}", client.UserInfo.Nickname, client.IP, text));

            PacketInfo packetInfo;

            try
            {
                packetInfo = JsonConvert.DeserializeObject<PacketInfo>(text);
            }
            catch
            {
                return;
            }

            if (packetInfo != null && !string.IsNullOrEmpty(packetInfo.Command))
            {
                if (packetInfo.Command == "Connect")
                {
                    client.UserInfo.Nickname = packetInfo.GetParameter("Nickname");
                    string password = packetInfo.GetParameter("Password");
                    ClientConnect(client, password);
                }
                else if (client.Authorized)
                {
                    switch (packetInfo.Command)
                    {
                        case "Disconnect":
                            string reason = packetInfo.GetParameter("Reason");
                            ClientDisconnected(client, reason);
                            break;
                        case "Message":
                            if (client.Authorized)
                            {
                                PacketInfo packetInfoMessage = new PacketInfo("Message");
                                MessageInfo messageInfo = packetInfo.GetData<MessageInfo>();
                                messageInfo.FromUser = client.UserInfo;
                                packetInfoMessage.Data = messageInfo;

                                if (messageInfo.ToUser != null && !string.IsNullOrEmpty(messageInfo.ToUser.Nickname))
                                {
                                    SendTo(messageInfo.ToUser, packetInfoMessage);
                                    SendTo(messageInfo.FromUser, packetInfoMessage);
                                }
                                else
                                {
                                    SendToAll(packetInfoMessage);
                                }
                            }
                            break;
                        case "Ping":
                            SendTo(client.UserInfo, new PacketInfo("Pong"));
                            break;
                    }
                }
            }
        }
コード例 #6
0
ファイル: ServerManager.cs プロジェクト: Jaex/Chat
        private void ClientDisconnected(Client client, string reason)
        {
            if (client.Authorized)
            {
                Clients.Remove(client);
                OnUserDisconnected(client.UserInfo);
                OnConsoleOutput(string.Format("Client disconnected: {0} {1} {2}", client.UserInfo.Nickname, client.IP, reason));

                if (IsWorking)
                {
                    PacketInfo packetInfoDisconnected = new PacketInfo("Disconnected");
                    packetInfoDisconnected.Data = client.UserInfo;
                    SendToAll(packetInfoDisconnected);
                }
            }
        }
コード例 #7
0
ファイル: ServerManager.cs プロジェクト: Jaex/Chat
        private bool ClientConnect(Client client, string password = "")
        {
            if (!string.IsNullOrEmpty(Password) && !password.Equals(Password, StringComparison.InvariantCultureIgnoreCase))
            {
                KickClient(client, "Wrong password.");
                return false;
            }

            string nickname = client.UserInfo.Nickname;

            if (string.IsNullOrEmpty(nickname) || nickname.Equals(Nickname, StringComparison.InvariantCultureIgnoreCase))
            {
                KickClient(client, "Invalid nickname.");
                return false;
            }

            if (Clients.Any(x => nickname.Equals(x.UserInfo.Nickname, StringComparison.InvariantCultureIgnoreCase)))
            {
                KickClient(client, "Nickname is already in use: " + nickname);
                return false;
            }

            Clients.Add(client);
            OnUserConnected(client.UserInfo);
            client.Authorized = true;

            SendUserList(client);

            PacketInfo packetInfoConnected = new PacketInfo("Connected");
            packetInfoConnected.Data = client.UserInfo;
            SendToAll(packetInfoConnected);

            return true;
        }
コード例 #8
0
ファイル: ServerManager.cs プロジェクト: Jaex/Chat
 public void SendUserList(Client client)
 {
     if (Clients.Count > 0)
     {
         PacketInfo packetInfo = new PacketInfo("UserList");
         packetInfo.Data = Clients.Select(x => x.UserInfo).ToArray();
         client.SendPacket(packetInfo);
     }
 }
コード例 #9
0
ファイル: ServerManager.cs プロジェクト: Jaex/Chat
 public void KickClient(Client client, string reason = "")
 {
     PacketInfo packetInfo = new PacketInfo("Kick");
     if (!string.IsNullOrEmpty(reason)) packetInfo.AddParameter("Reason", reason);
     client.SendPacket(packetInfo);
     client.Disconnect();
 }
コード例 #10
0
ファイル: ClientManager.cs プロジェクト: Jaex/Chat
        private void client_MessageReceived(Client client, string text)
        {
            PacketInfo packetInfo = JsonConvert.DeserializeObject<PacketInfo>(text);

            if (packetInfo != null && !string.IsNullOrEmpty(packetInfo.Command))
            {
                switch (packetInfo.Command)
                {
                    case "Connected":
                        UserInfo connectedUserInfo = packetInfo.GetData<UserInfo>();
                        OnNotification(connectedUserInfo.Nickname + " connected.");
                        OnUserConnected(connectedUserInfo);
                        break;
                    case "Disconnected":
                        UserInfo disconnectedUserInfo = packetInfo.GetData<UserInfo>();
                        OnNotification(disconnectedUserInfo.Nickname + " disconnected.");
                        OnUserDisconnected(disconnectedUserInfo);
                        break;
                    case "Message":
                        MessageInfo messageInfo = packetInfo.GetData<MessageInfo>();
                        OnMessageReceived(messageInfo);
                        break;
                    case "UserList":
                        UserInfo[] userList = packetInfo.GetData<UserInfo[]>();
                        OnUserListReceived(userList);
                        break;
                    case "Kick":
                        string reason = packetInfo.GetParameter("Reason");
                        OnKicked(reason);
                        break;
                    case "Pong":
                        if (pingTimer != null) OnPong(pingTimer.ElapsedMilliseconds);
                        break;
                }
            }
        }
コード例 #11
0
ファイル: ClientManager.cs プロジェクト: Jaex/Chat
 private void client_Disconnected(Client client, string reason)
 {
     IsConnected = IsAuthenticated = false;
     OnNotification("Disconnected.");
 }
コード例 #12
0
ファイル: ClientManager.cs プロジェクト: Jaex/Chat
 public void Connect(string password = "")
 {
     try
     {
         client = new Client(IPAddress, Port);
         client.UserInfo.Nickname = Nickname;
         client.MessageReceived += client_MessageReceived;
         client.Disconnected += client_Disconnected;
         PacketInfo packetInfo = new PacketInfo("Connect");
         packetInfo.AddParameter("Nickname", Nickname);
         if (!string.IsNullOrEmpty(password)) packetInfo.AddParameter("Password", password);
         client.SendPacket(packetInfo);
         IsConnected = true;
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }