Beispiel #1
0
        static void Main(string[] args)
        {
            String thisprocessname = Process.GetCurrentProcess().ProcessName;

            if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname) > 1)
            {
                return;
            }

            SQLiteConnection m_dbConnection;

            m_dbConnection = new SQLiteConnection("Data Source=SecConvDB.sqlite; Version=3;");
            m_dbConnection.Open();

            //create DataBase file if not exists
            DataBase.CreateDataBase(m_dbConnection);
            Communique.AddDelegateToDictionary();
            m_dbConnection.Close();

            //List<string> lista = new List<string>();
            //lista.Add("Monika");
            //lista.Add("Robert");
            //lista.Add("2017-12-06-23:00:00");
            //lista.Add("1:30:22");
            //Communique.DelFriend(lista);
            //Communique.CallState(lista);*/
            //Communique.CallState(lista);

            security.CreatePublicKey();
            AsynchronousSocketListener.StartListening();
        }
        public static void ReadCallback(IAsyncResult ar)
        {
            String content = String.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            byte[] sessionKey = null;
            int    bytesRead  = 0;

            //SESSIONKEY_RETURN:
            // Read data from the client socket.
            bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(
                                    state.buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read
                // more data.
                content = state.sb.ToString();
                if (content.IndexOf("<EOF>") > -1)
                {
                    // All the data has been read from the
                    // client. Display it on the console.
                    Console.WriteLine("Read {0} bytes from socket. \nData : {1}",
                                      content.Length, content);

                    string messageBits = Utilities.getBinaryMessage(content);
                    //take 8 bits to recognize the communique
                    int bits8 = Convert.ToInt32(messageBits.Substring(0, 8), 2);//decimal value

                    if (bits8 == 17)
                    {
                        sessionKey       = Program.security.SetSessionKey(Convert.FromBase64String(content.Substring(2, content.Length - 8)));
                        state.sessionKey = sessionKey;
                        Send(handler, (char)17 + " " + Convert.ToBase64String(Program.security.GetOwnerPublicKey().ToByteArray()) + " <EOF>");
                        //goto SESSIONKEY_RETURN;
                        state.buffer = new byte[1024];
                        state.sb     = new StringBuilder();
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                             new AsyncCallback(ReadCallback), state);
                    }
                    else
                    {
                        content = Communique.ChooseCommunique(content, state.sessionKey, handler);
                        // Echo the data back to the client.

                        if (bits8 != 2 && bits8 != 11)
                        {
                            Send(handler, content);
                        }

                        if (bits8 == 1 && content == ((char)5).ToString() + " <EOF>") //logIn
                        {
                            string userAddressIP = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
                            long   userID        = Communique.getUserIDHavingAdressIP(userAddressIP);
                            if (userID == -1)
                            {
                                Send(handler, Communique.Fail());
                            }
                            {
                                Send(handler, Communique.LogIP(userID));   //data about friend
                                Thread.Sleep(250);                         //miliseconds
                                Send(handler, Communique.History(userID)); //userID history
                            }
                        }

                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();

                        if (bits8 == 0)
                        {
                        }
                    }
                }
                else
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                         new AsyncCallback(ReadCallback), state);
                }
            }
        }