Example #1
0
        private void startFirst()
        {
            while (!connected)
            {
                try
                {
                    if (client.Available > 0)
                    {
                        gr = new GAISReader(client.GetStream());

                        Int32 id = gr.ReadInt32();
                        nickName = gr.ReadString();

                        foreach (ConnectionInfo c in Server.connections)
                        {
                            if (c.nickName == nickName)
                            {
                                GAISWriter gw = new GAISWriter();
                                gw.Write((Int32)OPCODE.RESPONSE);
                                gw.Write((Int32)RESPONSE.NICKNAME_TAKEN);
                                GAISWriter gwSend = new GAISWriter(client.GetStream());
                                gwSend.Write(gw.ToArray());
                                gwSend.Flush();
                                return;
                            }
                        }
                        ConnectionInfo ci = new ConnectionInfo();
                        ci.nickName = nickName;
                        ci.cl       = client;
                        Server.connections.Add(ci);

                        //create a new thread for this user
                        chatThread = new Thread(new ThreadStart(Run));
                        //start the thread
                        chatThread.Start();

                        connected = true;
                    }
                }
                catch
                {
                    client.Close();
                    connected = true;
                }

                Thread.Sleep(100);
            }
        }
Example #2
0
        public static void SendMsgToAll(String nickName, GAISReader gr)
        {
            List <ConnectionInfo> ToRemove = new List <ConnectionInfo>();

            GAISWriter gw = HandleReader(gr);

            foreach (ConnectionInfo c in connections)
            {
                if (c.nickName == nickName)
                {
                    if (gw == null)
                    {
                        ToRemove.Add(c);
                    }
                    else
                    {
                        continue;
                    }
                }
                if (gw != null)
                {
                    try
                    {
                        GAISWriter sendGW = new GAISWriter(c.cl.GetStream());
                        sendGW.Write(gw.ToArray());
                        sendGW.Flush();
                    }
                    catch
                    {
                        ToRemove.Add(c);
                    }
                }
            }

            foreach (ConnectionInfo c in ToRemove)
            {
                connections.Remove(c);
            }

            ToRemove.Clear();
        }