예제 #1
0
        public void ListenForRequestAnswer()
        {
            byte[] bytesFrom = new byte[1024];

            networkStream.Read(bytesFrom, 0, 1024);
            networkStream.Flush();

            //trim removes not set parts of bytesfrom
            string msg = Encoding.Default.GetString(bytesFrom, 0, 1024).Trim('\0');

            Console.WriteLine(msg);

            RequestMessage message = MessageParser.FromJson <RequestMessage>(msg);

            Console.WriteLine("!!!###### ListenForRequestAnswer got answer !!!!!!!########" + message.answer);
            OnServerPeerAnswer(message);
        }
예제 #2
0
        private void AcceptConnection()
        {
            if (client.Connected)
            {
                StopListening();
                byte[] bytesFrom = new byte[REQUEST_SIZE];

                networkStream.Read(bytesFrom, 0, REQUEST_SIZE);
                networkStream.Flush();

                //trim removes not set parts of bytesfrom
                string msg = Encoding.Default.GetString(bytesFrom, 0, 1024).Trim('\0');

                RequestMessage message = MessageParser.FromJson <RequestMessage>(msg);
                Console.WriteLine(message);
                Console.WriteLine("OOOOOOOOOOOOO");

                OnRequestReceived(message);

                return;
                //TODO: EVENT SERVER CLIENT DISPATCH CLIENT WANTS TO TALK
            }
        }
예제 #3
0
        private void ReadMessages()
        {
            client.ReceiveBufferSize = Int32.MaxValue;
            Console.WriteLine("started reading mesgeges");
            while (isConnected)
            {
                try
                {
                    if (!networkStream.DataAvailable)
                    {
                        continue;
                    }

                    byte[] byteForm = new byte[HEADER_SIZE];
                    networkStream.Read(byteForm, 0, HEADER_SIZE);

                    string headerMsg = Encoding.Default.GetString(byteForm, 0, HEADER_SIZE).Trim('\0');
                    Header header    = JsonConvert.DeserializeObject <Header>(headerMsg);

                    if (header.messageType == MessageType.PollMessage)
                    {
                        continue;
                    }

                    int    amount          = header.size;
                    string typeName        = header.messageType.ToString();
                    byte[] contentByteForm = new byte[amount];

                    int    bytesRead = 0;
                    string totalData = "";
                    int    chunkSize = amount;

                    while (true)
                    {
                        byte[] imageChunk = new byte[chunkSize];
                        networkStream.Read(imageChunk, 0, chunkSize);
                        string chunkStr = Encoding.Default.GetString(imageChunk, 0, chunkSize).Trim('\0');
                        totalData += chunkStr;
                        bytesRead += chunkStr.Length;

                        if (totalData.Length < amount)
                        {
                            chunkSize = amount - totalData.Length;
                        }
                        else
                        {
                            break;
                        }
                    }

                    string  messageContentString = totalData;
                    Message msg = MessageParser.FromJson <Message>(messageContentString);
                    Runner.Primary(() => OnMessageRecieved?.Invoke(msg, header.messageType));
                }

                catch (MessageParseException ex)
                {
                    Console.WriteLine(ex.ToString());
                    //   throw new CorruptDataException("Data corrupt"); //invoke

                    OnMessageParseException?.Invoke(ex);
                    break;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());

                    break;
                }
                finally
                {
                    networkStream.Flush();
                }
            }
            Disconnect();
        }