示例#1
0
 public static void Send(this ClientInfo client, JsonMessage m)
 {
     if (client.ClientType == ClientType.Web)
     {
         if (client.IsSecure)
         {
             JsonMessageHelpers.SetJsonMessage(client.SStream, m);
         }
         else
         {
             JsonMessageHelpers.SetJsonMessage(client.Stream, m);
         }
     }
     else
     {
         if (client.IsSecure)
         {
             MessageHelpers.SetMessage(client.SStream, m.ToMessage());
         }
         else
         {
             MessageHelpers.SetMessage(client.Stream, m.ToMessage());
         }
     }
 }
示例#2
0
 public void Init(string password = "")
 {
     if (IsSecure)
     {
         Message m    = new Message(Name, MessageType.Initialize);
         SHA256  sha  = SHA256.Create();
         byte[]  hash = sha.ComputeHash(Encoding.UTF8.GetBytes(password));
         sha.Dispose();
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < hash.Length; i++)
         {
             builder.Append(hash[i].ToString("x2"));
         }
         m.SetContent(builder.ToString());
         MessageHelpers.SetMessage(SStream, m);
     }
     else
     {
         Message m    = new Message(Name, MessageType.Initialize);
         SHA256  sha  = SHA256.Create();
         byte[]  hash = sha.ComputeHash(Encoding.UTF8.GetBytes(password));
         sha.Dispose();
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < hash.Length; i++)
         {
             builder.Append(hash[i].ToString("x2"));
         }
         m.SetContent(builder.ToString());
         MessageHelpers.SetMessage(Stream, m);
     }
 }
示例#3
0
 public void ForwardMessage(Message m)
 {
     if (IsSecure)
     {
         MessageHelpers.SetMessage(SStream, m);
     }
     else
     {
         MessageHelpers.SetMessage(Stream, m);
     }
 }
示例#4
0
        static void ClientListener(ref ListenerState state)
        {
            TcpListener server = new TcpListener(iPAddress, DesktopPort);

            server.Start();
            while (true)
            {
                state = ListenerState.Waiting;
                TcpClient client = server.AcceptTcpClient();

                NetworkStream stream = client.GetStream();

                //SslStream ssl = new SslStream(client.GetStream(), false);
                //ssl.AuthenticateAsServer(X509, false, true);

                state = ListenerState.UsernameSub;
                while (client.Available < 3)
                {
                    if (state == ListenerState.Quiting)
                    {
                        break;
                    }
                }
                if (state == ListenerState.Quiting)
                {
                    server.Stop();
                    break;
                }

                Message message = MessageHelpers.GetMessage(stream);
                if (message.MessageType == MessageType.Initionalize && clients.Any(x => x.Value.Name == message.Name) == false)
                {
                    ClientInfo c = new ClientInfo(message.Name, stream, ClientType.Desktop);
                    clients.Add(c.GUID, c);
                    ThreadPool.QueueUserWorkItem(HandleClientDesktopWorker, c);
                    state = ListenerState.Connected;
                }
                else
                {
                    state = ListenerState.Disconnected;
                    Message h = new Message(message.Name, MessageType.Status);
                    h.SetStatusType(StatusType.ErrorDisconnect);
                    h.SetContent("User with the name " + message.Name + " already exists");
                    MessageHelpers.SetMessage(stream, h);
                    stream.Close();
                    stream.Dispose();
                    client.Close();
                    client.Dispose();
                }
            }
        }
示例#5
0
 public static void SendToAll(this Dictionary <string, ClientInfo> values, Message message, List <ClientInfo> ignore)
 {
     foreach (KeyValuePair <string, ClientInfo> client in values)
     {
         if (client.Value.ClientType == ClientType.Web)
         {
             JsonMessageHelpers.SetJsonMessage(client.Value.Stream, message.ToJsonMessage());
         }
         else
         {
             MessageHelpers.SetMessage(client.Value.Stream, message);
         }
     }
 }
示例#6
0
 public void CreateInformation(InfomationType infomationType)
 {
     m = new Message(Name, MessageType.Infomation);
     m.SetInformationType(infomationType);
     if (AutoSend)
     {
         if (IsSecure)
         {
             MessageHelpers.SetMessage(SStream, m);
         }
         else
         {
             MessageHelpers.SetMessage(Stream, m);
         }
         m = null;
     }
 }
示例#7
0
 public void CreateStatus(StatusType status, string content = "")
 {
     m = new Message(Name, MessageType.Status);
     m.SetStatusType(status);
     m.SetContent(content);
     if (AutoSend)
     {
         if (IsSecure)
         {
             MessageHelpers.SetMessage(SStream, m);
         }
         else
         {
             MessageHelpers.SetMessage(Stream, m);
         }
         m = null;
     }
 }
示例#8
0
 public void CreateWhisper(string content, NColor color, string endpoint)
 {
     m = new Message(Name, MessageType.Whisper, endpoint);
     m.SetContent(content);
     m.SetColor(color);
     if (AutoSend)
     {
         if (IsSecure)
         {
             MessageHelpers.SetMessage(SStream, m);
         }
         else
         {
             MessageHelpers.SetMessage(Stream, m);
         }
         m = null;
     }
 }
示例#9
0
 public static void SendToAll(this Dictionary <string, ClientInfo> values, JsonMessage message, List <ClientInfo> ignore)
 {
     foreach (KeyValuePair <string, ClientInfo> client in values)
     {
         if (ignore.Select(x => x.GUID == client.Value.GUID).First())
         {
             continue;
         }
         if (client.Value.ClientType == ClientType.Web)
         {
             JsonMessageHelpers.SetJsonMessage(client.Value.Stream, message);
         }
         else
         {
             MessageHelpers.SetMessage(client.Value.Stream, message.ToMessage());
         }
     }
 }
示例#10
0
 public void CreateTransfer(byte[] fileContents, string filename, NColor color, string endpoint = "")
 {
     m = new Message(Name, MessageType.Transfer, endpoint);
     m.SetFileContents(fileContents);
     m.SetFilename(filename);
     m.SetColor(color);
     if (AutoSend)
     {
         if (IsSecure)
         {
             MessageHelpers.SetMessage(SStream, m);
         }
         else
         {
             MessageHelpers.SetMessage(Stream, m);
         }
         m = null;
     }
 }
示例#11
0
 public bool Send()
 {
     if (m != null)
     {
         if (IsSecure)
         {
             MessageHelpers.SetMessage(SStream, m);
         }
         else
         {
             MessageHelpers.SetMessage(Stream, m);
         }
         m = null;
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#12
0
        private static void HandleClientDesktopWorker(object token)
        {
            ClientInfo    c      = (ClientInfo)token;
            NetworkStream stream = c.Stream;

            // Let bots know there is a new user
            foreach (dynamic b in Bots)
            {
                b.OnUserConnect(c);
            }

            // Tell all connected clients that we are connected
            Message cm = new Message(c.Name, MessageType.Status);

            cm.SetStatusType(StatusType.Connected);
            SendToAllClients(cm, null, -1);

            Room r = new Room();

            // Put user in none full room
            foreach (KeyValuePair <string, Room> room in Rooms)
            {
                if (!room.Value.AddUser(c))
                {
                    continue;
                }
                else
                {
                    r = room.Value;
                    break;
                }
            }
            if (r.Equals(default(Room)))
            {
                JsonMessage m = new JsonMessage("Server", MessageType.Status);
                m.SetStatusType(StatusType.ErrorDisconnect);
                m.SetContent("All available rooms are full");
                JsonMessageHelpers.SetJsonMessage(stream, m);
                return;
            }

            Message rooms = new Message("Server", MessageType.Request);

            rooms.SetRequestType(RequestType.Rooms);
            rooms.SetContent(Rooms.SerializeRooms());
            MessageHelpers.SetMessage(stream, rooms);

            // Send The Server Message Buffer
            foreach (Message message in r.Buffer)
            {
                MessageHelpers.SetMessage(stream, message);
            }

            while (true)
            {
                try
                {
                    c = clients[c.GUID];
                    Message m = MessageHelpers.GetMessage(stream);
                    foreach (dynamic b in Bots)
                    {
                        m = b.OnMessage(c, m);
                    }

                    // Manage Message buffer
                    if (m.MessageType == MessageType.Message)
                    {
                        r.AddMesssage(m);
                    }
                    else if (m.MessageType == MessageType.Infomation)
                    {
                        MessageHelpers.SetMessage(stream, InformationHandler(m.InfomationType, c.GUID));
                        continue;
                    }
                    else if (m.MessageType == MessageType.Status && m.StatusType == StatusType.ChangeRoom)
                    {
                        try
                        {
                            ClientInfo cInfo = clients[c.GUID];
                            if (!r.AddUser(cInfo))
                            {
                                m = new Message("Server", MessageType.Message);
                                m.SetColor(ColorToNColor(Color.Aquamarine));
                                m.SetContent("The requested room is full");
                                MessageHelpers.SetMessage(stream, m);
                                continue;
                            }
                            r.RemoveUser(cInfo);

                            int id = int.Parse(m.Content);
                            cInfo.SetRoomID(id);
                            clients[c.GUID] = cInfo;
                            Room oldR = r;
                            r = Rooms.Where(x => x.Value.ID == id).First().Value;
                            r.AddUser(cInfo);

                            m = new Message("Server", MessageType.Message);
                            m.SetContent("Moved to Room " + r.Name + "(" + id + ")");
                            MessageHelpers.SetMessage(stream, m);

                            foreach (Message message in r.Buffer)
                            {
                                MessageHelpers.SetMessage(stream, message);
                            }
                            foreach (dynamic b in Bots)
                            {
                                b.OnUserChangeRoom(c, oldR, r);
                            }
                            continue;
                        }
                        catch
                        {
                            try
                            {
                                ClientInfo cInfo = clients[c.GUID];
                                Room       oldR  = r;
                                r = Rooms.Where(x => x.Value.Name == m.Content).First().Value;
                                cInfo.SetRoomID(r.ID);
                                r.AddUser(cInfo);
                                clients[c.GUID] = cInfo;

                                m = new Message("Server", MessageType.Message);
                                m.SetContent("Moved to Room " + r.Name);
                                MessageHelpers.SetMessage(stream, m);

                                foreach (Message message in r.Buffer)
                                {
                                    MessageHelpers.SetMessage(stream, message);
                                }
                                foreach (dynamic b in Bots)
                                {
                                    b.OnUserChangeRoom(c, oldR, r);
                                }
                                continue;
                            }
                            catch { continue; }
                        }
                    }
                    if (m.Name != c.Name)
                    {
                        m.SetName(c.Name);
                    }
                    if (m.StatusType == StatusType.Disconnecting && m.MessageType == MessageType.Status)
                    {
                        foreach (dynamic b in Bots)
                        {
                            b.OnUserDisconnect(c);
                        }
                        r.RemoveUser(clients[c.GUID]);
                        MessageHelpers.SetMessage(stream, m);
                        stream.Close();
                        clients.Remove(c.GUID);

                        Message d = new Message(c.Name, MessageType.Status);
                        d.SetStatusType(StatusType.Disconnected);

                        SendToAllClients(d, null, -1);

                        break;
                    }

                    foreach (KeyValuePair <string, ClientInfo> network in clients)
                    {
                        if (network.Key == c.GUID)
                        {
                            continue;
                        }
                        else
                        {
                            if (m.EndPoint == "")
                            {
                                SendToAllClients(m, null, clients[c.GUID].RoomId, c.GUID);
                                break;
                            }
                            else
                            {
                                if (network.Value.Name == m.EndPoint)
                                {
                                    MessageHelpers.SetMessage(network.Value.Stream, m);
                                }
                            }
                        }
                    }
                }
                catch (IOException)
                {
                    if (clients.ContainsKey(c.GUID))
                    {
                        foreach (dynamic b in Bots)
                        {
                            b.OnUserDisconnect(c);
                        }
                        r.RemoveUser(clients[c.GUID]);
                        clients[c.GUID].Stream.Close();
                        clients.Remove(c.GUID);
                    }
                    foreach (KeyValuePair <string, ClientInfo> network in clients)
                    {
                        Message d = new Message(network.Key == c.Name ? "You" : c.Name, MessageType.Status);
                        d.SetStatusType(StatusType.Disconnected);
                        MessageHelpers.SetMessage(network.Value.Stream, d);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    if (clients.ContainsKey(c.GUID))
                    {
                        foreach (dynamic b in Bots)
                        {
                            b.OnUserDisconnect(c);
                        }
                        r.RemoveUser(clients[c.GUID]);
                        clients[c.GUID].Stream.Close();
                        clients.Remove(c.GUID);
                    }
                    Console.WriteLine(c.Name + " disconnected due to an error. Details: " + ex.Message);
                    foreach (KeyValuePair <string, ClientInfo> network in clients)
                    {
                        Message e = new Message(network.Key == c.Name ? "You" : c.Name, MessageType.Status);
                        e.SetStatusType(StatusType.ErrorDisconnect);
                        MessageHelpers.SetMessage(network.Value.Stream, e);
                    }
                    break;
                }
            }
        }