Пример #1
0
Файл: Server.cs Проект: VDZx/GNI
 public void Message(string s)
 {
     GNIData data = new GNIData("chatmessage", s);
     Console.ForegroundColor = ConsoleColor.White;
     Console.WriteLine(s);
     BroadcastSignal(data);
 }
Пример #2
0
Файл: Server.cs Проект: VDZx/GNI
 public void SMessage(string s)
 {
     GNIData data = new GNIData("systemmessage", s);
     Console.ForegroundColor = ConsoleColor.Green;
     Console.WriteLine(s);
     BroadcastSignal(data);
 }
Пример #3
0
 //Public methods
 public void Add(GNIData data)
 {
     GNIData[] newData = new GNIData[allData.Length + 1];
     for (int i = 0; i < allData.Length; i++)
     {
         newData[i] = allData[i];
     }
     newData[newData.Length - 1] = data;
     allData = newData;
 }
Пример #4
0
Файл: Client.cs Проект: VDZx/GNI
 public override void OnDataReceived(GNIData data, uint source = 0)
 {
     switch (data.keyString)
     {
         case "chatmessage":
             Message(data.valueString);
             break;
         case "systemmessage":
             SMessage(data.valueString);
             break;
     }
 }
Пример #5
0
 public void BroadcastSignal(GNIData data)
 {
     while (clientLock) { Thread.Sleep(50); }
     clientLock = true;
     try
     {
         for (int i = 0; i < clients.Count; i++)
         {
             SendSignal(clients[i], data);
         }
     }
     catch (Exception) { }
     clientLock = false;
 }
Пример #6
0
        public GNIData GetData(int key)
        {
            int     keyPosition = KeyPosition(key);
            GNIData returnData;

            if (keyPosition == -1)
            {
                returnData = new GNIData(true);
            }
            else
            {
                returnData = allData[keyPosition];
            }
            return(returnData);
        }
Пример #7
0
 public void BroadcastSignal(GNIData data)
 {
     while (clientLock)
     {
         Thread.Sleep(50);
     }
     clientLock = true;
     try
     {
         for (int i = 0; i < clients.Count; i++)
         {
             SendSignal(clients[i], data);
         }
     }
     catch (Exception) { }
     clientLock = false;
 }
Пример #8
0
 //------------SendSignal------------
 public void SendSignal(uint recipient, GNIData data)
 {
     while (clientLock)
     {
         Thread.Sleep(50);
     }
     clientLock = true;
     try
     {
         for (int i = 0; i < clients.Count; i++)
         {
             if (clients[i].clientID == recipient)
             {
                 SendSignal(clients[i], data);
             }
         }
     }
     catch (Exception) { }
     clientLock = false;
 }
Пример #9
0
 //Private methods
 private void Remove(int keyPosition, bool disambiguation)
 {
     if (keyPosition == -1)
     {
         return;
     }
     GNIData[] newData = new GNIData[allData.Length - 1];
     for (int i = 0; i < allData.Length; i++)
     {
         if (i < keyPosition)
         {
             newData[i] = allData[i];
         }
         else if (i > keyPosition)
         {
             newData[i - 1] = allData[i];
         }
     }
     allData = newData;
 }
Пример #10
0
Файл: Client.cs Проект: VDZx/GNI
        public void SendMessage(string message)
        {
            if (message.StartsWith("/"))
            {
                if (message.StartsWith("/nick"))
                {
                    string[] split = message.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (split.Length > 1)
                    {
                        GNIData data = new GNIData("nick", split[1]);
                        SendSignal(tcpClient, data);
                    }
                }
            }
            else
            {
                GNIData data = new GNIData("message", message);
                SendSignal(tcpClient, data);

            }
        }
Пример #11
0
        public void Overwrite(GNIData data)
        {
            int keyPosition = -1;

            switch (data.keyType)
            {
            case GNIDataType.Short:
                keyPosition = KeyPosition(data.keyInt);
                break;

            case GNIDataType.String:
                keyPosition = KeyPosition(data.keyString);
                break;
            }
            if (keyPosition == -1)
            {
                Add(data);
            }
            else
            {
                allData[keyPosition] = data;
            }
        }
Пример #12
0
Файл: Server.cs Проект: VDZx/GNI
 public override void OnDataReceived(GNIData data, uint source)
 {
     switch (data.keyString)
     {
         case "message":
             Message("[" + DateTime.Now.ToString() + "] <" + GetClient(source).name + "> " + data.valueString);
             break;
         case "nick":
             string oldname = "";
             for (int i = 0; i < clients.Count; i++)
             {
                 if (clients[i].clientID == source)
                 {
                     GNIClientInformation client = clients[i];
                     oldname = client.name;
                     client.name = data.valueString;
                     clients[i] = client;
                 }
             }
             if(oldname != "") SMessage(oldname + " has changed nick to " + data.valueString);
             break;
     }
 }
Пример #13
0
 public override void OnDataReceived(GNIData data, uint source = 0)
 {
     Console.WriteLine("Received signal: " + data.keyString);
     switch (data.keyString)
     {
         case "filename":
             this.filenames[source] = data.valueString;
             Console.WriteLine("Filename for " + source + " set to " + data.valueString);
             break;
         case "filedata":
             string filename = filenames[source];
             Console.WriteLine("Receiving file " + filename + " from " + source);
             try
             {
                 BinaryWriter writer = new BinaryWriter(new FileStream(filename, FileMode.Create));
                 writer.Write(data.valueBytes);
                 writer.Close();
                 SendSignal(GetClient(source), new GNIData().SetData("finish", 0));
                 Console.WriteLine("Succesfully received file " + filename + " from " + source);
             }
             catch (Exception ex) { Console.WriteLine("Error while writing " + filename + ": " + ex.Message); }
             break;
     }
 }
Пример #14
0
 public GNIData GetData(int key)
 {
     int keyPosition = KeyPosition(key);
     GNIData returnData;
     if (keyPosition == -1) returnData = new GNIData(true);
     else returnData = allData[keyPosition];
     return returnData;
 }
Пример #15
0
 public GNIDataSet(bool empty)
 {
     allData = new GNIData[0];
 }
Пример #16
0
 public void Overwrite(GNIData data)
 {
     int keyPosition = -1;
     switch (data.keyType)
     {
         case GNIDataType.Short:
             keyPosition = KeyPosition(data.keyInt);
             break;
         case GNIDataType.String:
             keyPosition = KeyPosition(data.keyString);
             break;
     }
     if (keyPosition == -1) Add(data);
     else allData[keyPosition] = data;
 }
Пример #17
0
        //------------HandleSignal----------
        protected void HandleSignal(byte[] rawSignal, uint source = 0)
        {
            GNIData data = new GNIData();

            System.IO.MemoryStream ms = new System.IO.MemoryStream(rawSignal);

            //Read types
            int keyTypeByte = ReadVariableLengthInt(ms, 1);
            int valueTypeByte = ReadVariableLengthInt(ms, 1);

            //Enter types
            switch (keyTypeByte)
            {
                case 0: data.keyType = GNIDataType.None; break;
                case 1: data.keyType = GNIDataType.Short; break;
                case 2: data.keyType = GNIDataType.String; break;
                case 3: data.keyType = GNIDataType.ByteArray; break;
            }
            switch (valueTypeByte)
            {
                case 0: data.valueType = GNIDataType.None; break;
                case 1: data.valueType = GNIDataType.Short; break;
                case 2: data.valueType = GNIDataType.String; break;
                case 3: data.valueType = GNIDataType.ByteArray; break;
            }

            int keyLength = ReadVariableLengthInt(ms, GNIGeneral.GetLengthLength(data.keyType));
            int valueLength = ReadVariableLengthInt(ms, GNIGeneral.GetLengthLength(data.valueType));

            //If string, read encoding byte
            int encoding = 0;
            if (data.keyType == GNIDataType.String || data.valueType == GNIDataType.String)
            {
                encoding = ReadVariableLengthInt(ms, 1);
            }

            //Enter encoding
            switch (encoding)
            {
                case 0: data.encoding = GNIEncoding.ASCII; break;
            }

            //Read and enter key
            switch (data.keyType)
            {
                case GNIDataType.None: data.EnterKeyNull(); break;
                case GNIDataType.Short: data.EnterKeyInt(ReadShort(ms)); break;
                case GNIDataType.String: data.EnterKeyString(ReadString(ms, keyLength, data.encoding)); break;
                case GNIDataType.ByteArray: byte[] buffer = new byte[keyLength]; ms.Read(buffer, 0, keyLength); data.EnterKeyByteArray(buffer); break;
            }

            //Read and enter value
            switch (data.valueType)
            {
                case GNIDataType.None: data.EnterValueNull(); break;
                case GNIDataType.Short: data.EnterValueInt(ReadShort(ms)); break;
                case GNIDataType.String: data.EnterValueString(ReadString(ms, valueLength, data.encoding)); break;
                case GNIDataType.ByteArray: byte[] buffer = new byte[valueLength]; ms.Read(buffer, 0, valueLength); data.EnterValueByteArray(buffer); break;
            }

            OnDataReceived(data, source);
        }
Пример #18
0
 //Private methods
 private void Remove(int keyPosition, bool disambiguation)
 {
     if(keyPosition == -1) return;
     GNIData[] newData = new GNIData[allData.Length - 1];
     for (int i = 0; i < allData.Length; i++)
     {
         if(i < keyPosition)
             newData[i] = allData[i];
         else if (i > keyPosition)
             newData[i - 1] = allData[i];
     }
     allData = newData;
 }
Пример #19
0
 //------------SendSignal------------
 public void SendSignal(uint recipient, GNIData data)
 {
     while (clientLock) { Thread.Sleep(50); }
     clientLock = true;
     try
     {
         for (int i = 0; i < clients.Count; i++)
         {
             if (clients[i].clientID == recipient) SendSignal(clients[i], data);
         }
     }
     catch (Exception) { }
     clientLock = false;
 }
Пример #20
0
 public void SendSignal(GNIClientInformation recipient, GNIData data)
 {
     SendSignal(recipient.tcpClient, data);
 }
Пример #21
0
 public void SendSignal(GNIClientInformation recipient, GNIData data)
 {
     SendSignal(recipient.tcpClient, data);
 }
Пример #22
0
 public virtual void OnDataReceived(GNIData data, uint source = 0)
 {
 }
Пример #23
0
        public void SendSignal(TcpClient recipient, GNIData data)
        {
            byte keyType = 0;
            switch (data.keyType)
            {
                case GNIDataType.Short: keyType = 1; break;
                case GNIDataType.String: keyType = 2; break;
                case GNIDataType.ByteArray: keyType = 3; break;
            }

            byte valueType = 0;
            switch (data.valueType)
            {
                case GNIDataType.Short: valueType = 1; break;
                case GNIDataType.String: valueType = 2; break;
                case GNIDataType.ByteArray: valueType = 3; break;
            }

            byte encoding = 0;
            if (data.valueType == GNIDataType.String)
            {
                switch (data.encoding)
                {
                    case GNIEncoding.ASCII: encoding = 0; break;
                }
            }

            byte[] keyBytes = new byte[0];
            switch (data.keyType)
            {
                case GNIDataType.Short: keyBytes = BitConverter.GetBytes(Convert.ToInt16(data.keyInt)); break;
                case GNIDataType.String:
                    switch (data.encoding)
                    {
                        case GNIEncoding.ASCII: keyBytes = Encoding.ASCII.GetBytes(data.keyString); break;
                    }
                    break;
                case GNIDataType.ByteArray:
                    keyBytes = data.keyBytes;
                    break;
            }

            byte[] valueBytes = new byte[0];
            switch (data.valueType)
            {
                case GNIDataType.Short: valueBytes = BitConverter.GetBytes(Convert.ToInt16(data.valueInt)); break;
                case GNIDataType.String:
                    switch (data.encoding)
                    {
                        case GNIEncoding.ASCII: valueBytes = Encoding.ASCII.GetBytes(data.valueString); break;
                    }
                    break;
                case GNIDataType.ByteArray:
                    valueBytes = data.valueBytes;
                    break;
            }

            int encodingAddition = 0;
            if (data.keyType == GNIDataType.String || data.valueType == GNIDataType.String) encodingAddition = 1;

            int keyLength = GNIGeneral.GetLengthLength(data.keyType);
            int valueLength = GNIGeneral.GetLengthLength(data.valueType);

            byte[] toSend = new byte[
                4 //Data length
                + 1 //Key type
                + 1 //Value type
                + keyLength //Key length
                + valueLength //Value length
                + encodingAddition
                + keyBytes.Length
                + valueBytes.Length
                ];

            int currentposition = 0;

            //Write data length
            byte[] buffer = BitConverter.GetBytes(toSend.Length - 4);
            for (int i = 0; i < 4; i++) { toSend[currentposition] = buffer[i]; currentposition++; }
            //Write key type
            toSend[currentposition] = keyType; currentposition++;
            //Write value type
            toSend[currentposition] = valueType; currentposition++;
            //Write key length
            if (keyLength == 4) buffer = BitConverter.GetBytes(Convert.ToInt32(keyBytes.Length));
            else buffer = BitConverter.GetBytes(Convert.ToInt16(keyBytes.Length));
            for (int i = 0; i < keyLength; i++) { toSend[currentposition] = buffer[i]; currentposition++; }
            //Write value length
            if (valueLength == 4) buffer = BitConverter.GetBytes(Convert.ToInt32(valueBytes.Length));
            else buffer = BitConverter.GetBytes(Convert.ToInt16(valueBytes.Length));
            for (int i = 0; i < valueLength; i++) { toSend[currentposition] = buffer[i]; currentposition++; }
            //Write encoding if applicable
            if (data.keyType == GNIDataType.String || data.valueType == GNIDataType.String)
            {
                toSend[currentposition] = encoding; currentposition++;
            }
            //Write key
            for (int i = 0; i < keyBytes.Length; i++) { toSend[currentposition] = keyBytes[i]; currentposition++; }
            //Write value
            for (int i = 0; i < valueBytes.Length; i++) { toSend[currentposition] = valueBytes[i]; currentposition++; }

            //Send the data!
            if (recipient.Connected)
            {
                try
                {
                    recipient.GetStream().Write(toSend, 0, toSend.Length);
                }
                catch (Exception) { LoseConnection(recipient); }
            }
            else LoseConnection(recipient);
        }
Пример #24
0
 //Public methods
 public void Add(GNIData data)
 {
     GNIData[] newData = new GNIData[allData.Length + 1];
     for (int i = 0; i < allData.Length; i++)
     {
         newData[i] = allData[i];
     }
     newData[newData.Length - 1] = data;
     allData = newData;
 }
Пример #25
0
        public void SendSignal(TcpClient recipient, GNIData data)
        {
            byte keyType = 0;

            switch (data.keyType)
            {
            case GNIDataType.Short: keyType = 1; break;

            case GNIDataType.String: keyType = 2; break;

            case GNIDataType.ByteArray: keyType = 3; break;
            }

            byte valueType = 0;

            switch (data.valueType)
            {
            case GNIDataType.Short: valueType = 1; break;

            case GNIDataType.String: valueType = 2; break;

            case GNIDataType.ByteArray: valueType = 3; break;
            }

            byte encoding = 0;

            if (data.valueType == GNIDataType.String)
            {
                switch (data.encoding)
                {
                case GNIEncoding.ASCII: encoding = 0; break;
                }
            }

            byte[] keyBytes = new byte[0];
            switch (data.keyType)
            {
            case GNIDataType.Short: keyBytes = BitConverter.GetBytes(Convert.ToInt16(data.keyInt)); break;

            case GNIDataType.String:
                switch (data.encoding)
                {
                case GNIEncoding.ASCII: keyBytes = Encoding.ASCII.GetBytes(data.keyString); break;
                }
                break;

            case GNIDataType.ByteArray:
                keyBytes = data.keyBytes;
                break;
            }

            byte[] valueBytes = new byte[0];
            switch (data.valueType)
            {
            case GNIDataType.Short: valueBytes = BitConverter.GetBytes(Convert.ToInt16(data.valueInt)); break;

            case GNIDataType.String:
                switch (data.encoding)
                {
                case GNIEncoding.ASCII: valueBytes = Encoding.ASCII.GetBytes(data.valueString); break;
                }
                break;

            case GNIDataType.ByteArray:
                valueBytes = data.valueBytes;
                break;
            }

            int encodingAddition = 0;

            if (data.keyType == GNIDataType.String || data.valueType == GNIDataType.String)
            {
                encodingAddition = 1;
            }

            int keyLength   = GNIGeneral.GetLengthLength(data.keyType);
            int valueLength = GNIGeneral.GetLengthLength(data.valueType);

            byte[] toSend = new byte[
                4             //Data length
                + 1           //Key type
                + 1           //Value type
                + keyLength   //Key length
                + valueLength //Value length
                + encodingAddition
                + keyBytes.Length
                + valueBytes.Length
                            ];

            int currentposition = 0;

            //Write data length
            byte[] buffer = BitConverter.GetBytes(toSend.Length - 4);
            for (int i = 0; i < 4; i++)
            {
                toSend[currentposition] = buffer[i]; currentposition++;
            }
            //Write key type
            toSend[currentposition] = keyType; currentposition++;
            //Write value type
            toSend[currentposition] = valueType; currentposition++;
            //Write key length
            if (keyLength == 4)
            {
                buffer = BitConverter.GetBytes(Convert.ToInt32(keyBytes.Length));
            }
            else
            {
                buffer = BitConverter.GetBytes(Convert.ToInt16(keyBytes.Length));
            }
            for (int i = 0; i < keyLength; i++)
            {
                toSend[currentposition] = buffer[i]; currentposition++;
            }
            //Write value length
            if (valueLength == 4)
            {
                buffer = BitConverter.GetBytes(Convert.ToInt32(valueBytes.Length));
            }
            else
            {
                buffer = BitConverter.GetBytes(Convert.ToInt16(valueBytes.Length));
            }
            for (int i = 0; i < valueLength; i++)
            {
                toSend[currentposition] = buffer[i]; currentposition++;
            }
            //Write encoding if applicable
            if (data.keyType == GNIDataType.String || data.valueType == GNIDataType.String)
            {
                toSend[currentposition] = encoding; currentposition++;
            }
            //Write key
            for (int i = 0; i < keyBytes.Length; i++)
            {
                toSend[currentposition] = keyBytes[i]; currentposition++;
            }
            //Write value
            for (int i = 0; i < valueBytes.Length; i++)
            {
                toSend[currentposition] = valueBytes[i]; currentposition++;
            }

            //Send the data!
            if (recipient.Connected)
            {
                try
                {
                    recipient.GetStream().Write(toSend, 0, toSend.Length);
                }
                catch (Exception) { LoseConnection(recipient); }
            }
            else
            {
                LoseConnection(recipient);
            }
        }
Пример #26
0
        //------------HandleSignal----------
        protected void HandleSignal(byte[] rawSignal, uint source = 0)
        {
            GNIData data = new GNIData();

            System.IO.MemoryStream ms = new System.IO.MemoryStream(rawSignal);

            //Read types
            int keyTypeByte   = ReadVariableLengthInt(ms, 1);
            int valueTypeByte = ReadVariableLengthInt(ms, 1);

            //Enter types
            switch (keyTypeByte)
            {
            case 0: data.keyType = GNIDataType.None; break;

            case 1: data.keyType = GNIDataType.Short; break;

            case 2: data.keyType = GNIDataType.String; break;

            case 3: data.keyType = GNIDataType.ByteArray; break;
            }
            switch (valueTypeByte)
            {
            case 0: data.valueType = GNIDataType.None; break;

            case 1: data.valueType = GNIDataType.Short; break;

            case 2: data.valueType = GNIDataType.String; break;

            case 3: data.valueType = GNIDataType.ByteArray; break;
            }

            int keyLength   = ReadVariableLengthInt(ms, GNIGeneral.GetLengthLength(data.keyType));
            int valueLength = ReadVariableLengthInt(ms, GNIGeneral.GetLengthLength(data.valueType));

            //If string, read encoding byte
            int encoding = 0;

            if (data.keyType == GNIDataType.String || data.valueType == GNIDataType.String)
            {
                encoding = ReadVariableLengthInt(ms, 1);
            }

            //Enter encoding
            switch (encoding)
            {
            case 0: data.encoding = GNIEncoding.ASCII; break;
            }

            //Read and enter key
            switch (data.keyType)
            {
            case GNIDataType.None: data.EnterKeyNull(); break;

            case GNIDataType.Short: data.EnterKeyInt(ReadShort(ms)); break;

            case GNIDataType.String: data.EnterKeyString(ReadString(ms, keyLength, data.encoding)); break;

            case GNIDataType.ByteArray: byte[] buffer = new byte[keyLength]; ms.Read(buffer, 0, keyLength); data.EnterKeyByteArray(buffer); break;
            }

            //Read and enter value
            switch (data.valueType)
            {
            case GNIDataType.None: data.EnterValueNull(); break;

            case GNIDataType.Short: data.EnterValueInt(ReadShort(ms)); break;

            case GNIDataType.String: data.EnterValueString(ReadString(ms, valueLength, data.encoding)); break;

            case GNIDataType.ByteArray: byte[] buffer = new byte[valueLength]; ms.Read(buffer, 0, valueLength); data.EnterValueByteArray(buffer); break;
            }

            OnDataReceived(data, source);
        }
Пример #27
0
 public virtual void OnDataReceived(GNIData data, uint source = 0)
 {
 }
Пример #28
0
 public override void OnDataReceived(GNIData data, uint source = 0)
 {
     //base.DataReceived(data);
     Console.WriteLine(data.valueString);
 }
Пример #29
0
 public override void OnDataReceived(GNIData data, uint source = 0)
 {
     switch (data.keyString)
     {
         case "finish":
             transferring = false;
             break;
     }
 }