Beispiel #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            strName = util.globals.local_name;
            this.Text = "SGSclient: " + strName;

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data ();
            msgToSend.cmdCommand = Command.List;
            msgToSend.strName = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer,
                new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            //Start listening to the data asynchronously
            clientSocket.BeginReceiveFrom (byteData,
                                       0, byteData.Length,
                                       SocketFlags.None,
                                       ref epServer,
                                       new AsyncCallback(OnReceive),
                                       null);
        }
Beispiel #2
0
        public void OnDataArrival(byte[] data)
        {
            IncomingData = (Data)sock.RawDeserialize(data, typeof(Data));
            NoPacket = Convert.ToInt16(IPAddress.NetworkToHostOrder(IncomingData.iNoPackets));
            StartPont = 4;
            endPoint = 0;
            try
            {
                for (short ai = 0; ai < NoPacket; ai++)
                {
                    tempBuffer = data.Skip(StartPont).Take(512).ToArray();
                    tempRemainingData = (cCompData)sock.RawDeserialize(tempBuffer, typeof(cCompData));
                    tempBuffer = null;
                    icomplen = Convert.ToInt32(IPAddress.NetworkToHostOrder(tempRemainingData.iCompLen));
                    StartPont = StartPont + 2 + icomplen;
                    endPoint = endPoint + 2 + icomplen;
                    decompressedData = new byte[1024];
                    src = tempRemainingData.buffer.Skip(0).Take(icomplen).ToArray();
                    /*
                     if (icomplen > 0 && icomplen < 512)
                     {
                         error_code = lzo1z_decompress(src, icomplen, decompressedData, ref DecompreddedLen);

                         if (error_code == 0)
                         {
                             OrigData = decompressedData.Skip(8).Take(512).ToArray();
                             decompressedData = null;
                             DataProcessing(OrigData);
                         }
                         else Console.WriteLine("Data Issue ....");
                     }

                     else
                     * * */

                    if (icomplen == 0)
                    {
                        OrigData = tempRemainingData.buffer.Skip(8).ToArray();
                        DataProcessing(OrigData);
                    }
                    OrigData = null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("OnDataArrival  " + ex.ToString());
            }
        }
Beispiel #3
0
 private void WriteToSingleReplica(string key, Data value, IPEndPoint endPoint)
 {
     log.InfoFormat("Write '{1}':'{2}',{3} to {0}", endPoint, key, value.Value, value.Version);
     Task.Delay(5000).ContinueWith(t =>
         {
             try
             {
                 serviceClient.Write(key, value, endPoint);
                 log.InfoFormat("Write success '{1}':'{2}',{3} to {0}", endPoint, key, value.Value, value.Version);
             }
             catch (Exception)
             {
                 log.WarnFormat("Write error '{1}':'{2}',{3} to {0}", endPoint, key, value.Value, value.Version);
                 WriteToSingleReplica(key, value, endPoint);
             }
         });
 }
Beispiel #4
0
        private void btn_Login_Click(object sender, EventArgs e)
        {
            Data msgToSend = new Data();
            msgToSend.cmdCommand = Command.Login;
            msgToSend.strName = strName;

            DialogResult = DialogResult.OK;

            byte[] b = msgToSend.ToByte();

            //Send the message to the server
            clientSocket?.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

            //clientSocket?.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);

            DialogResult = DialogResult.OK;
            Close();
        }
Beispiel #5
0
        //Broadcast the message typed by the user to everyone
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();

                msgToSend.strName = strName;
                msgToSend.strMessage = txtMessage.Text;
                msgToSend.cmdCommand = Command.Message;

                byte[] byteData = msgToSend.ToByte();

                //Send it to the server
                clientSocket.BeginSendTo (byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);

                txtMessage.Text = null;
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "SGSclientUDP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #6
0
        private void btn_Connect_Click(object sender, EventArgs e)
        {
            btn_Connect.Load("base/misc/btn_connect_pressed.png");
            try
            {
                if (serverList.Items.Count > 0 && serverList.SelectedItem != null && clientSocket != null)
                {
                    Data msgToSend = new Data();
                    msgToSend.cmdCommand = Command.PacketSize;

                    byte[] message = msgToSend.ToByte();

                    clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }
            }
            catch (Exception ex)
            {
                if (Program.debug)
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace.ToString(), "AODXClient", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #7
0
        private void ClientForm_Load(object sender, EventArgs e)
        {
            nameLabel.Text = "";

            if (iniParser.GetSide(strName) != "jud")
            {
                btn_crossexamination.Visible = false;
                btn_crossexamination.Enabled = false;
                btn_edit.Visible = false;
                btn_edit.Enabled = false;
                courtRecordPB.Image = Image.FromFile("base/misc/inventory.png");
                btn_testimony.Visible = false;
                btn_testimony.Enabled = false;
                btn_defminus.Visible = false;
                btn_defminus.Enabled = false;
                btn_defplus.Visible = false;
                btn_defplus.Enabled = false;
                btn_prominus.Visible = false;
                btn_prominus.Enabled = false;
                btn_proplus.Visible = false;
                btn_proplus.Enabled = false;
                txtLog.Size = new Size(240, 347);
            }

            fonts.AddFontFile("base/misc/Ace-Attorney-2.ttf");

            musicList.Items.Clear();

            foreach (string song in songs)
                musicList.Items.Add(song);

            Text = "AODXClient: " + strName;

            emoCount = iniParser.GetEmoNum(strName);
            emoMaxPages = (int)Math.Floor((decimal)(emoCount / 10));

            loadEmoButtons();
            loadEviButtons();

            //byteData = new byte[incomingSize];
            byteData = new byte[1048576];

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data();
            msgToSend.cmdCommand = Command.List;
            msgToSend.strName = strName;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

            byteData = new byte[1048576];

            //Start listening to the data asynchronously
            clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
        }
Beispiel #8
0
        private void btn_testimony_Click(object sender, EventArgs e)
        {
            if (btn_testimony.Visible == true)
            {
                Data calloutMsg = new Data();
                calloutMsg.callout = 4;
                calloutMsg.cmdCommand = Command.Message;

                byte[] msg = calloutMsg.ToByte();
                clientSocket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
            }
        }
Beispiel #9
0
        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            /* if (MessageBox.Show("Are you sure you want to leave the courtroom?", "AODXClient: " + strName, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
            {
                e.Cancel = true;
                return;
            } */

            try
            {
                //Send a message to logout of the server
                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Logout;
                msgToSend.strName = strName;
                msgToSend.strMessage = null;

                byte[] b = msgToSend.ToByte();
                if (clientSocket.Connected)
                    clientSocket.Send(b, 0, b.Length, SocketFlags.None);
                clientSocket.Close();
                if (sfxPlayer != null)
                    sfxPlayer.Dispose();
                if (wr != null)
                    wr.Dispose();
                if (blipPlayer != null)
                    blipPlayer.Dispose();
                if (blipReader != null)
                    blipReader.Dispose();
                if (musicPlayer != null)
                    musicPlayer.Dispose();
                if (musicReader != null)
                    musicReader.Dispose();

                if (Directory.Exists("base/cases"))
                    Directory.Delete("base/cases", true);
            }
            catch (SocketException)
            { }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                if (Program.debug)
                    MessageBox.Show(ex.Message + ".\r\n" + ex.StackTrace.ToString(), "AODXClient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #10
0
        private void txtMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter & sendEnabled)
            {
                e.SuppressKeyPress = true;
                if (redraw == false)
                {
                    try
                    {
                        //Fill the info for the message to be send
                        Data msgToSend = new Data();

                        msgToSend.strName = strName;
                        msgToSend.anim = selectedAnim;
                        msgToSend.textColor = selectedColor;
                        msgToSend.strMessage = txtMessage.Text;
                        msgToSend.callout = callout;
                        if (!presenting)
                            msgToSend.cmdCommand = Command.Message;
                        else
                        {
                            msgToSend.cmdCommand = Command.Present;
                            msgToSend.strMessage = msgToSend.strMessage + "|" + selectedEvidence;

                            btn_back_PerformClick();
                        }

                        byte[] byteData = msgToSend.ToByte();

                        //prepWriteDispBoxes(msgToSend);

                        //Send it to the server
                        clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                        txtMessage.Clear();
                    }
                    catch (Exception ex)
                    {
                        if (Program.debug)
                            MessageBox.Show("Unable to send message to the server.\r\n" + ex.Message + ".\r\n" + ex.StackTrace.ToString(), "AODXClient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Beispiel #11
0
 private void btn_proplus_Click(object sender, EventArgs e)
 {
     if (btn_proplus.Visible == true)
     {
         if (proHealth < 5)
         {
             Data msg = new Data();
             msg.cmdCommand = Command.ChangeHealth;
             msg.strName = "pro";
             msg.strMessage = "+1";
             byte[] byteMsg = msg.ToByte();
             clientSocket.BeginSend(byteMsg, 0, byteMsg.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
         }
     }
 }
Beispiel #12
0
        private void SGSClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to leave the chat room?", "SGSclient: " + strName,
                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
            {
                e.Cancel = true;
                return;
            }

            try
            {
                //Send a message to logout of the server
                Data msgToSend = new Data ();
                msgToSend.cmdCommand = Command.Logout;
                msgToSend.strName = strName;
                msgToSend.strMessage = null;

                byte[] b = msgToSend.ToByte ();
                clientSocket.SendTo(b, 0, b.Length, SocketFlags.None, epServer);
                clientSocket.Close();
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #13
0
        private void prepWriteDispBoxes(Data msg, Color newColor)
        {
            string msgText = msg.strMessage;
            textToDisp = new string[3];
            textToDisp[0] = "";
            textToDisp[1] = "";
            textToDisp[2] = "";
            clearDispMsg();
            if (newColor.ToKnownColor() != KnownColor.PeachPuff)
                setDispMsgColor(newColor);

            msgText = msg.strMessage.Substring(msg.strName.Length + 2);

            for (int i = 0; i < 3; i++)
            {
                if (TextRenderer.MeasureText(msgText, displayMsg1.Font).Width > 240)
                {
                    string[] parts = msgText.Split(' ');
                    string combined = "";
                    for (int x = 0; x < parts.Length; x++)
                    {
                        /* if (i == 2) // test bit of code to try and help me test these stupid loops
                        {
                            string test = "";
                        } */

                        //TO DO: Add a handler in case a single word is too long, and measure it by characters
                        if (TextRenderer.MeasureText(parts[x], displayMsg1.Font).Width <= 246)
                        {
                            if (TextRenderer.MeasureText(combined + parts[x] + " ", displayMsg1.Font).Width <= 246)
                            {
                                combined = combined + parts[x] + " "; // TO DO: Don't add a space after the last word
                            }
                            else
                            {
                                textToDisp[i] = combined;
                                msgText = msgText.Substring(combined.Length);
                                break;
                            }
                        }
                        else
                        {
                            for (int y = 0; y < parts[x].Length; y++)
                            {
                                if (TextRenderer.MeasureText(combined + parts[x].Substring(0, y), displayMsg1.Font).Width <= 246)
                                {
                                    combined = combined + parts[x].Substring(0, y); // TO DO: Don't add a space after the last word
                                }
                                else
                                {
                                    textToDisp[i] = combined;
                                    msgText = msgText.Substring(combined.Length);
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    textToDisp[i] = msgText;
                    break;
                }
            }
            //dispTextRedraw.Enabled = true;
            nameLabel.Text = iniParser.GetDispName(latestMsg.strName) ?? latestMsg.strName;
            //blipPlayer.Initialize(blipReader);
            blipPlayer.Stop();
            if (!mute)
                blipPlayer.Play();
            redraw = true;
        }
Beispiel #14
0
 public void Write(string key, Data value, IPEndPoint endPoint)
 {
     createProxy(endPoint).Write(key, value);
 }
Beispiel #15
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                while (byteData[0] == 4 && byteData.Length < incomingSize)
                {
                    //string test = "";
                }

                clientSocket.EndReceive(ar);

                if (byteData[0] == 8)
                {
                    EviData msg = new EviData(byteData);

                    Evidence evi = new Evidence();
                    evi.name = msg.strName;
                    evi.desc = msg.strDesc;
                    evi.note = msg.strNote;
                    evi.index = msg.index;

                    using (MemoryStream ms = new MemoryStream(msg.dataBytes))
                    {
                        evi.icon = Image.FromStream(ms,false,true);
                    }

                    eviList.Add(evi);

                    //string dirName = "";
                    //for (int x = 0; x < msg.strName.Split('/').Length - 1; x++)
                    //{
                    //    dirName = dirName + msg.strName.Split('/')[x];
                    //    if (x < msg.strName.Split('/').Length - 2)
                    //        dirName = dirName + '/';
                    //}
                    //if (!Directory.Exists(dirName))
                    //    Directory.CreateDirectory(dirName);

                    //using (FileStream fs = new FileStream(msg.strName, FileMode.Create))
                    //{
                    //    using (BinaryWriter w = new BinaryWriter(fs))
                    //    {
                    //        if (msg.dataSize > 0)
                    //            w.Write(msg.dataBytes.Take(msg.dataSize).ToArray());
                    //    }
                    //}

                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }
                else
                {

                    Data msgReceived = new Data(byteData);

                    if (msgReceived.cmdCommand == Command.Disconnect)
                    {
                        MessageBox.Show("You are banned from this server!");
                        btn_Connect.Image = Image.FromFile("base/misc/btn_connect.png");
                        //clientSocket.Close();
                        //Close();
                    }
                    else if (msgReceived.cmdCommand == Command.PacketSize)
                    {
                        incomingSize = Convert.ToInt32(msgReceived.strMessage);
                        byteData = new byte[incomingSize];

                        //DialogResult = DialogResult.OK;
                        Data msgToSend = new Data();
                        msgToSend.cmdCommand = Command.DataInfo;

                        byte[] b = msgToSend.ToByte();

                        //Send the message to the server
                        clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                        clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                        //Close();
                    }
                    else if (msgReceived.cmdCommand == Command.DataInfo)
                    {
                        if (msgReceived.strMessage != null && msgReceived.strMessage != "")
                        {
                            string[] data = msgReceived.strMessage.Split('|');
                            int charCount = Convert.ToInt32(data[0]);
                            if (charCount > 0)
                            {
                                for (int x = 1; x < charCount; x++)
                                {
                                    charList.Add(data[x]);
                                }
                            }

                            int songCount = Convert.ToInt32(data[charCount + 1]);
                            if (songCount > 0)
                            {
                                for (int x = charCount + 2; x < charCount + 2 + songCount; x++)
                                {
                                    musicList.Add(data[x]);
                                }
                            }
                        }

                        byteData = new byte[4194304];
                        //Do stuff with the evidence/extra binary data here

                    }
                    else
                        clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);

                    if (msgReceived.cmdCommand == Command.DataInfo)
                    {
                        //Program.charList = charList;
                        //Program.musicList = musicList;
                        //Program.connection = clientSocket;
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
            }
            catch (SocketException)
            {
                if (MessageBox.Show("You have been disconnected from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.OK | MessageBox.Show("You have been kicked from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.Cancel)
                    Close();
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                if (Program.debug)
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace.ToString(), "AODXClient", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #16
0
 private void ReplicateInternal(string key, Data value)
 {
     foreach (var endPoint in endpoints)
         WriteToSingleReplica(key, value, endPoint);
 }
Beispiel #17
0
 public void Replicate(string key, Data value)
 {
     Task.Run(() => ReplicateInternal(key, value));
 }
Beispiel #18
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                while (byteData[0] == 4 && byteData.Length < incomingSize)
                {
                    //string test = "";
                }

                clientSocket.EndReceive(ar);

                if (byteData[0] == 8)
                {
                    EviData msg = new EviData(byteData);

                    Evidence evi = new Evidence();
                    evi.name  = msg.strName;
                    evi.desc  = msg.strDesc;
                    evi.note  = msg.strNote;
                    evi.index = msg.index;

                    using (MemoryStream ms = new MemoryStream(msg.dataBytes))
                    {
                        evi.icon = Image.FromStream(ms, false, true);
                    }

                    eviList.Add(evi);

                    //string dirName = "";
                    //for (int x = 0; x < msg.strName.Split('/').Length - 1; x++)
                    //{
                    //    dirName = dirName + msg.strName.Split('/')[x];
                    //    if (x < msg.strName.Split('/').Length - 2)
                    //        dirName = dirName + '/';
                    //}
                    //if (!Directory.Exists(dirName))
                    //    Directory.CreateDirectory(dirName);

                    //using (FileStream fs = new FileStream(msg.strName, FileMode.Create))
                    //{
                    //    using (BinaryWriter w = new BinaryWriter(fs))
                    //    {
                    //        if (msg.dataSize > 0)
                    //            w.Write(msg.dataBytes.Take(msg.dataSize).ToArray());
                    //    }
                    //}

                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }
                else
                {
                    Data msgReceived = new Data(byteData);

                    if (msgReceived.cmdCommand == Command.Disconnect)
                    {
                        MessageBox.Show("You are banned from this server!");
                        btn_Connect.Image = Image.FromFile("base/misc/btn_connect.png");
                        //clientSocket.Close();
                        //Close();
                    }
                    else if (msgReceived.cmdCommand == Command.PacketSize)
                    {
                        incomingSize = Convert.ToInt32(msgReceived.strMessage);
                        byteData     = new byte[incomingSize];

                        //DialogResult = DialogResult.OK;
                        Data msgToSend = new Data();
                        msgToSend.cmdCommand = Command.DataInfo;

                        byte[] b = msgToSend.ToByte();

                        //Send the message to the server
                        clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                        clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                        //Close();
                    }
                    else if (msgReceived.cmdCommand == Command.DataInfo)
                    {
                        if (msgReceived.strMessage != null && msgReceived.strMessage != "")
                        {
                            string[] data      = msgReceived.strMessage.Split('|');
                            int      charCount = Convert.ToInt32(data[0]);
                            if (charCount > 0)
                            {
                                for (int x = 1; x < charCount; x++)
                                {
                                    charList.Add(data[x]);
                                }
                            }

                            int songCount = Convert.ToInt32(data[charCount + 1]);
                            if (songCount > 0)
                            {
                                for (int x = charCount + 2; x < charCount + 2 + songCount; x++)
                                {
                                    musicList.Add(data[x]);
                                }
                            }
                        }

                        byteData = new byte[4194304];
                        //Do stuff with the evidence/extra binary data here
                    }
                    else
                    {
                        clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                    }

                    if (msgReceived.cmdCommand == Command.DataInfo)
                    {
                        //Program.charList = charList;
                        //Program.musicList = musicList;
                        //Program.connection = clientSocket;
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
            }
            catch (SocketException)
            {
                if (MessageBox.Show("You have been disconnected from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.OK | MessageBox.Show("You have been kicked from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.Cancel)
                {
                    Close();
                }
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                if (Program.debug)
                {
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace.ToString(), "AODXClient", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Beispiel #19
0
        private void musicList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (musicList.Items.Count > 0 && musicList.SelectedItem != null && (string)musicList.SelectedItem != "")
            {
                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.ChangeMusic;
                msgToSend.strName = strName;
                msgToSend.strMessage = (string)musicList.Items[musicList.SelectedIndex];
                byte[] msg = msgToSend.ToByte();

                clientSocket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                //byteData = new byte[1024];

                //clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
            }
        }
Beispiel #20
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);

                if (byteData[0] == 8)
                {
                    EviData data = new EviData(byteData);

                    Evidence evi = new Evidence();
                    evi.name = data.strName;
                    evi.desc = data.strDesc;
                    evi.note = data.strNote;
                    evi.index = data.index;

                    using (MemoryStream ms = new MemoryStream(data.dataBytes))
                    {
                        evi.icon = Image.FromStream(ms, false, true);
                    }

                    bool found = false;
                    foreach (Evidence item in eviList)
                    {
                        if (item.index == evi.index)
                        {
                            found = true;
                            item.name = evi.name;
                            item.note = evi.note;
                            item.desc = evi.desc;
                            item.icon = evi.icon;
                            break;
                        }
                    }
                    if (found == false)
                        eviList.Add(evi);

                    testimonyPB.Location = new Point(257, 3);
                    testimonyPB.BringToFront();
                    PictureBox icon = new PictureBox();
                    icon.Image = evi.icon;
                    icon.Location = new Point(6, 5);
                    icon.Size = new Size(70, 70);
                    icon.BringToFront();
                    testimonyPB.Invoke((MethodInvoker)delegate
                    {
                        //perform on the UI thread
                        testimonyPB.Controls.Add(icon);
                    });
                    Label name = new Label();
                    name.Text = evi.name;
                    name.Location = new Point(91, 8);
                    name.Size = new Size(155, 17);
                    name.TextAlign = ContentAlignment.MiddleCenter;
                    name.ForeColor = Color.DarkOrange;
                    name.BackColor = Color.Transparent;
                    //name.Font = new Font(fonts.Families[0], 12.0f, FontStyle.Bold);
                    name.BringToFront();
                    testimonyPB.Invoke((MethodInvoker)delegate
                    {
                        //perform on the UI thread
                        testimonyPB.Controls.Add(name);
                    });
                    Label note = new Label();
                    note.Text = evi.note;
                    note.Location = new Point(92, 26);
                    note.Size = new Size(153, 44);
                    //note.Font = new Font(fonts.Families[0], 12.0f);
                    note.BackColor = Color.Transparent;
                    note.BringToFront();
                    testimonyPB.Invoke((MethodInvoker)delegate
                    {
                        //perform on the UI thread
                        testimonyPB.Controls.Add(note);
                    });
                    Label desc = new Label();
                    desc.Text = evi.desc;
                    desc.Location = new Point(9, 81);
                    desc.Size = new Size(238, 45);
                    //desc.Font = new Font(fonts.Families[0], 12.0f);
                    desc.BackColor = Color.Transparent;
                    desc.ForeColor = Color.White;
                    desc.BringToFront();
                    testimonyPB.Invoke((MethodInvoker)delegate
                    {
                        //perform on the UI thread
                        testimonyPB.Controls.Add(desc);
                    });
                    testimonyPB.Size = new Size(256, 127);
                    testimonyPB.Image = Image.FromFile("base/misc/inventory_update.png");
                    wr = new WaveFileReader("base/sounds/general/sfx-selectjingle.wav");

                    sfxPlayer.Initialize(wr);
                    if (!mute)
                        sfxPlayer.Play();

                    for (int x = 0; x <= 64; x++)
                    {
                        testimonyPB.Location = new Point(256 - (4 * x), 3);
                        //icon.Location = new Point(256 + 6 - (2 * x), 3 + 5);
                        //name.Location = new Point(256 + 91 - (2 * x), 3 + 8);
                        //note.Location = new Point(256 + 92 - (2 * x), 3 + 26);
                        //desc.Location = new Point(256 + 9 - (2 * x), 3 + 81);
                        icon.Refresh();
                        name.Refresh();
                        note.Refresh();
                        desc.Refresh();
                    }

                    System.Threading.Thread.Sleep(3000);

                    for (int x = 0; x <= 64; x++)
                    {
                        testimonyPB.Location = new Point(0 - (4 * x), 3);
                        //icon.Location = new Point(6 - (2 * x), 3 + 5);
                        //name.Location = new Point(91 - (2 * x), 3 + 8);
                        //note.Location = new Point(92 - (2 * x), 3 + 26);
                        //desc.Location = new Point(9 - (2 * x), 3 + 81);
                        icon.Refresh();
                        name.Refresh();
                        note.Refresh();
                        desc.Refresh();
                    }

                    testimonyPB.Image = null;
                    name.Dispose();
                    icon.Dispose();
                    desc.Dispose();
                    note.Dispose();
                }
                else
                {
                    Data msgReceived = new Data(byteData);

                    //Accordingly process the message received
                    switch (msgReceived.cmdCommand)
                    {
                        case Command.Login:
                            break;

                        case Command.Logout:
                            break;

                        case Command.ChangeMusic:
                            if (msgReceived.strMessage != null && msgReceived.strMessage != "" & msgReceived.strName != null)
                            {
                                appendTxtLogSafe("<<<" + msgReceived.strName + " changed the music to " + msgReceived.strMessage + ">>>\r\n");
                                musicReader = new DmoMp3Decoder("base/sounds/music/" + msgReceived.strMessage);

                                if (musicPlayer.PlaybackState != PlaybackState.Stopped)
                                    musicPlayer.Stop();
                                musicPlayer.Initialize(musicReader);
                                if (!mute)
                                    musicPlayer.Play();
                            }
                            break;

                        case Command.ChangeHealth:
                            if (msgReceived.strName == "def")
                            {
                                if (msgReceived.strMessage == "-1")
                                    defHealth--;
                                else if (msgReceived.strMessage == "+1")
                                    defHealth++;
                            }
                            else if (msgReceived.strName == "pro")
                            {
                                if (msgReceived.strMessage == "-1")
                                    proHealth--;
                                else if (msgReceived.strMessage == "+1")
                                    proHealth++;
                            }

                            updateHealth();
                            break;

                        case Command.Message:
                        case Command.Present:
                            if (latestMsg != null && msgReceived.strName == latestMsg.strName)
                            {
                                newGuy = false;
                            }
                            else
                            {
                                newGuy = true;
                                testimonyPB.Image = null;
                            }

                            latestMsg = msgReceived;
                            objectLayerPB.Image = null;
                            objectLayerPB.Location = new Point(0, 0);
                            objectLayerPB.Size = new Size(256, 192);

                            if (msgReceived.callout <= 3)
                            {
                                sendEnabled = false;
                                curPreAnimTime = 0;
                                curPreAnimTime = 0;
                                curPreAnim = null;
                                soundTime = 0;
                                curSoundTime = 0;

                                if (msgReceived.callout > 0)
                                    performCallout();

                                if (iniParser.GetSoundName(msgReceived.strName, msgReceived.anim) != "1" && iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim) > 0 & File.Exists("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"))
                                {
                                    sfxPlayer.Stop();
                                    wr = new WaveFileReader("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav");
                                    sfxPlayer.Initialize(wr);
                                    soundTime = iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim);
                                }

                                /*  if (iniParser.GetSoundName(msgReceived.strName, msgReceived.anim) != "1" && iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim) > 0 & (File.Exists("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav") | File.Exists("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav")))
                                {
                                    sfxPlayer.Stop();
                                    if (File.Exists("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"))
                                        wr = new WaveFileReader("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav");
                                    else
                                        wr = new WaveFileReader("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav");
                                    sfxPlayer.Initialize(wr);
                                    soundTime = iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim);
                                } */

                                if (iniParser.GetAnimType(msgReceived.strName, msgReceived.anim) == 5)
                                    ChangeSides(true);
                                else
                                    ChangeSides();

                                //If there is no pre-animation
                                if (iniParser.GetAnimType(msgReceived.strName, msgReceived.anim) == 5 | iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim) == null | iniParser.GetPreAnimTime(msgReceived.strName, msgReceived.anim) <= 0)
                                {
                                    charLayerPB.Enabled = true;
                                    setCharSprite("base/characters/" + msgReceived.strName + "/(b)" + iniParser.GetAnim(msgReceived.strName, msgReceived.anim) + ".gif");
                                    if (msgReceived.cmdCommand == Command.Present)
                                    {
                                        sfxPlayer.Stop();
                                        wr = new WaveFileReader("base/sounds/general/sfx-shooop.wav");
                                        sfxPlayer.Initialize(wr);
                                        if (!mute)
                                            sfxPlayer.Play();

                                        switch (iniParser.GetSide(msgReceived.strName))
                                        {
                                            case "def":
                                                testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif");
                                                System.Threading.Thread.Sleep(100);
                                                testimonyPB.Location = new Point(173, 13);
                                                testimonyPB.Size = new Size(70, 70);
                                                testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon;
                                                break;
                                            case "pro":
                                                testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceLeft.gif");
                                                System.Threading.Thread.Sleep(100);
                                                testimonyPB.Location = new Point(13, 13);
                                                testimonyPB.Size = new Size(70, 70);
                                                testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon;
                                                break;
                                            case "hld":
                                                testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceLeft.gif");
                                                System.Threading.Thread.Sleep(100);
                                                testimonyPB.Location = new Point(13, 13);
                                                testimonyPB.Size = new Size(70, 70);
                                                testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon;
                                                break;
                                            case "hlp":
                                                testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif");
                                                System.Threading.Thread.Sleep(100);
                                                testimonyPB.Location = new Point(173, 13);
                                                testimonyPB.Size = new Size(70, 70);
                                                testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon;
                                                break;
                                            default:
                                                testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif");
                                                System.Threading.Thread.Sleep(100);
                                                testimonyPB.Location = new Point(173, 13);
                                                testimonyPB.Size = new Size(70, 70);
                                                testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon;
                                                break;
                                        }

                                        msgReceived.strMessage = msgReceived.strMessage.Split('|')[0];
                                    }
                                    prepWriteDispBoxes(msgReceived, msgReceived.textColor);
                                }
                                else //if there is a pre-animation
                                {
                                    //charLayerPB.Enabled = false;
                                    setCharSprite("base/characters/" + msgReceived.strName + "/" + iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim) + ".gif");
                                    preAnimTime = iniParser.GetPreAnimTime(msgReceived.strName, msgReceived.anim);
                                    curPreAnim = iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim);
                                }
                                //dispTextRedraw.Enabled = true;
                            }
                            else
                            {
                                performCallout();
                            }
                            break;

                        case Command.List:
                            appendTxtLogSafe("<<<" + strName + " has entered the courtroom>>>\r\n");
                            break;

                        case Command.DataInfo:
                            //Do the stuff with the incoming server data here

                            //The user has logged into the system so we now request the server to send
                            //the names of all users who are in the chat room
                            Data msgToSend = new Data();
                            msgToSend.cmdCommand = Command.Login;
                            msgToSend.strName = strName;

                            byteData = new byte[1048576];
                            byteData = msgToSend.ToByte();

                            clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                            byteData = new byte[1048576];
                            break;

                        case Command.PacketSize:
                            break;
                    }

                    if (msgReceived.strMessage != null & msgReceived.cmdCommand == Command.Message | msgReceived.cmdCommand == Command.Login | msgReceived.cmdCommand == Command.Logout)
                    {
                        if (msgReceived.callout <= 3)
                            appendTxtLogSafe(msgReceived.strMessage + "\r\n");
                    }

                    if (msgReceived.cmdCommand != Command.PacketSize)
                        byteData = new byte[1048576];
                    else
                        byteData = new byte[Convert.ToInt32(msgReceived.strMessage)];
                }

                clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);

            }
            catch (SocketException)
            {
                if (MessageBox.Show("You have been kicked from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    Close();
                }
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                if (Program.debug)
                    MessageBox.Show(ex.Message + ".\r\n" + ex.StackTrace.ToString(), "AODXClient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #21
0
 public void Write(string key, Data value)
 {
     IHttpRequest request = remoteHttpServer.CreatePostRequest("Write");
     request.BodyStream.WriteBytes(jsonSerializer.Serialize(new WriteParameters { Key = key, Value = value }));
     request.SendOrDie();
 }
Beispiel #22
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);

                //Convert the bytes received into an object of type Data
                Data msgReceived = new Data(byteData);

                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                    case Command.Login:
                        lstChatters.Items.Add(msgReceived.strName);
                        break;

                    case Command.Logout:
                        lstChatters.Items.Remove(msgReceived.strName);
                        break;

                    case Command.Message:
                        break;

                    case Command.List:
                        lstChatters.Items.AddRange(msgReceived.strMessage.Split('*'));
                        lstChatters.Items.RemoveAt(lstChatters.Items.Count - 1);
                        txtChatBox.Text += "<<<" + strName + " has joined the room>>>\r\n";
                        break;
                }

                if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List)
                    txtChatBox.Text += msgReceived.strMessage + "\r\n";

                byteData = new byte[1024];

                //Start listening to receive more data from the user
                clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer,
                                           new AsyncCallback(OnReceive), null);
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }