Beispiel #1
0
        public static JsonMessage ToJsonMessage(this Message message)
        {
            JsonMessage js = new JsonMessage(message.Name, message.MessageType, message.EndPoint);

            js.SetColor(message.Color);
            js.SetContent(message.Content);
            js.SetStatusType(message.StatusType);
            js.SetInformationType(message.InfomationType);
            js.SetRequestType(message.RequestType);
            return(js);
        }
Beispiel #2
0
        private static void HandleClientWebWorker(object token)
        {
            object[]      d      = (object[])token;
            TcpClient     client = (TcpClient)d[0];
            ClientInfo    c      = (ClientInfo)d[1];
            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
            JsonMessage cm = new JsonMessage(c.Name, MessageType.Status);

            cm.SetStatusType(StatusType.Connected);
            SendToAllClients(null, cm, -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 avaible rooms are full");
                JsonMessageHelpers.SetJsonMessage(stream, m);
                return;
            }

            // Send The Server Message Buffer
            foreach (Message message in r.Buffer)
            {
                JsonMessageHelpers.SetJsonMessage(stream, message.ToJsonMessage());
            }

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

            rooms.SetRequestType(RequestType.Rooms);
            rooms.SetContent(Rooms.SerializeRooms());

            JsonMessageHelpers.SetJsonMessage(stream, rooms);

            while (true)
            {
                try
                {
                    c = clients[c.GUID];
                    if (client.Available < 3)
                    {
                        Thread.Sleep(20);
                        continue;
                    }

                    JsonMessage m = JsonMessageHelpers.GetJsonMessage(stream);
                    foreach (dynamic b in Bots)
                    {
                        m = b.OnJsonMessage(c, m);
                    }

                    if (m.MessageType == MessageType.Message)
                    {
                        TotalMessagesSent += 1;
                        r.AddMesssage(m.ToMessage());
                    }
                    if (m.MessageType == MessageType.Infomation)
                    {
                        JsonMessageHelpers.SetJsonMessage(stream, InformationHandler(m.InfomationType, c.GUID).ToJsonMessage());
                        continue;
                    }
                    else if (m.MessageType == MessageType.Status && m.StatusType == StatusType.ChangeRoom)
                    {
                        try
                        {
                            ClientInfo cInfo = clients[c.GUID];
                            if (!r.AddUser(cInfo))
                            {
                                m = new JsonMessage("Server", MessageType.Message);
                                m.SetColor(ColorToNColor(Color.Aquamarine));
                                m.SetContent("The requested room is full");
                                JsonMessageHelpers.SetJsonMessage(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 JsonMessage("Server", MessageType.Message);
                            m.SetContent("Moved to Room " + id);
                            JsonMessageHelpers.SetJsonMessage(stream, m);

                            foreach (Message message in r.Buffer)
                            {
                                JsonMessageHelpers.SetJsonMessage(stream, message.ToJsonMessage());
                            }
                            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;
                                r.AddUser(cInfo);
                                cInfo.SetRoomID(r.ID);
                                clients[c.GUID] = cInfo;

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

                                foreach (Message message in r.Buffer)
                                {
                                    JsonMessageHelpers.SetJsonMessage(stream, message.ToJsonMessage());
                                }
                                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)
                    {
                        stream.Close();
                        stream.Dispose();
                        foreach (dynamic b in Bots)
                        {
                            b.OnUserDisconnect(c);
                        }
                        r.RemoveUser(clients[c.GUID]);
                        clients.Remove(c.GUID);

                        JsonMessage t = new JsonMessage(c.GUID, MessageType.Status);
                        t.SetStatusType(StatusType.Disconnected);

                        SendToAllClients(null, t, 0);
                        break;
                    }
                    foreach (KeyValuePair <string, ClientInfo> network in clients)
                    {
                        if (network.Key == c.GUID)
                        {
                            continue;
                        }
                        else
                        {
                            if (m.EndPoint == "")
                            {
                                SendToAllClients(null, m, clients[c.GUID].RoomId, c.GUID);
                                break;
                            }
                            else
                            {
                                if (network.Value.Name == m.EndPoint)
                                {
                                    JsonMessageHelpers.SetJsonMessage(network.Value.Stream, m);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    JsonMessage error = new JsonMessage("error", MessageType.Status);
                    error.SetStatusType(StatusType.ErrorDisconnect);
                    error.SetContent(e.Message);
                    if (clients.ContainsKey(c.GUID))
                    {
                        foreach (dynamic b in Bots)
                        {
                            b.OnUserDisconnect(c);
                        }
                        r.RemoveUser(clients[c.GUID]);
                        clients.Remove(c.GUID);
                    }
                    if (stream != null)
                    {
                        JsonMessageHelpers.SetJsonMessage(stream, error);
                        stream.Close();
                    }
                    break;
                }
            }
        }