コード例 #1
0
        private void ThreadRun()
        {
            bool stopToken = false;

            while ((this.socket.Connected) && (stopToken == false))
            {
                Thread.Sleep(10);
                if (this.socket.Available > 0)
                {
                    TCP_Protocoll antwort = TCP_Protocoll.ReadMessage(this.reader, TranslationDictionary);

                    if (antwort.ProtocolType == TCP_Protocoll.HeaderData.DictonaryDefinitionsSeed)
                    {
                        TranslationDictionary = TCP_Protocoll.CreateDicForComm((int)antwort.Data);
                    }

                    if (antwort.ProtocolType == TCP_Protocoll.HeaderData.GoodBye)
                    {
                        stopToken = true;
                    }

                    if ((this.Geantwortet != null) & (TranslationDictionary != null))
                    {
                        this.Geantwortet(antwort);
                    }
                }
            }
            this.socket.Close(); // Maybe this creats erros, may move to the end of while
        }
コード例 #2
0
 private void Client_Antwort(ServerClient Client, TCP_Protocoll Antwort)
 {
     if (this.ClientAntwort != null)
     {
         this.ClientAntwort(Client, Antwort);
     }
 }
コード例 #3
0
 /// <summary>
 /// Reads a Message from an Input-Stream
 /// </summary>
 /// <param name="Reader">Binary Reader for Network Communcation</param>
 /// <returns>TCP_Protocoll with Data</returns>
 public static TCP_Protocoll ReadMessage(BinaryReader Reader, Dictionary <int, string> Dictionary)
 {
     try
     {
         Int32         readLength = Reader.ReadInt32();
         TCP_Protocoll p          = TCP_Protocoll.generateProtocolFromString(Reader.ReadBytes(readLength));
         if (Dictionary != null)
         {
             p.ProtocolType = CreateHeaderFromAwnswer(p.ProtocolType, Dictionary);
         }
         return(p);
     }
     catch
     {
         return(new TCP_Protocoll(HeaderData.Error, ""));
     }
 }
コード例 #4
0
        /// <summary>
        /// Sends a Message over the Network
        /// </summary>
        /// <param name="protocol">Instance of a TCP_Protocoll with Stored-Data</param>
        /// <param name="Writer">Binary Writer for network Communication</param>
        /// <returns>True for sucess, False for error</returns>
        public static bool SendMessage(TCP_Protocoll protocol, BinaryWriter Writer, Dictionary <int, string> Dictionary)
        {
            try
            {
                if (Dictionary != null)
                {
                    protocol.ProtocolType = CreateHeaderForWrite(protocol.ProtocolType, Dictionary);
                }

                byte[] toSend = generateStringFromProtocol(protocol);
                Writer.Write((Int32)toSend.GetLength(0));
                Writer.Write(toSend);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
コード例 #5
0
        /// <summary>
        /// Serialisiert eine Klasse damit sie als String versendet werden kann
        /// </summary>
        /// <param name="protocol">Eine Instanz der Klasse TCP_Protocol</param>
        /// <returns>String fürs netzwerk senden</returns>
        private static byte[] generateStringFromProtocol(TCP_Protocoll protocol)
        {
            IFormatter   formatter = new BinaryFormatter();
            MemoryStream stream    = new MemoryStream();

            formatter.Serialize(stream, protocol);
            stream.Position = 0;

            byte[] buffer = new byte[stream.Length];

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return(ms.ToArray());
            }
        }
コード例 #6
0
 public void Write(TCP_Protocoll.HeaderData Header, object Data)
 {
     TCP_Protocoll.SendMessage(new TCP_Protocoll(Header, Data), this.writer, this.TranslationDictionary);
 }