示例#1
0
        //<summary>
        // This is used to convert the byte array received from a client into the method name and parameters for a method call.
        // The format is the same as the SendConversion method's output.
        //</summary>
        public static List <object> ReceiveConversion(byte[] array)
        {
            List <object> paramaters = new List <object> ();

            for (int i = 0; i < array.Length; i++)
            {
                switch (array [i])
                {
                case 1:
                    paramaters.Add(convertToChar(AltimitArray.copyOfRange(array, i + 1, i + 3)));
                    i += 2;
                    break;

                case 2:
                    paramaters.Add(convertToInt(AltimitArray.copyOfRange(array, i + 1, i + 5)));
                    i += 4;
                    break;

                case 3:
                    paramaters.Add(convertToLong(AltimitArray.copyOfRange(array, i + 1, i + 9)));
                    i += 8;
                    break;

                case 4:
                    paramaters.Add(convertToShort(AltimitArray.copyOfRange(array, i + 1, i + 3)));
                    i += 2;
                    break;

                case 5:
                    paramaters.Add(convertToFloat(AltimitArray.copyOfRange(array, i + 1, i + 5)));
                    i += 4;
                    break;

                case 6:
                    paramaters.Add(convertToDouble(AltimitArray.copyOfRange(array, i + 1, i + 9)));
                    i += 8;
                    break;

                case 7:
                    int length = convertToInt(AltimitArray.copyOfRange(array, i + 1, i + 5));
                    i += 5;
                    try {
                        paramaters.Add(convertToString(AltimitArray.copyOfRange(array, i, i + length)));
                    } catch (Exception e) {
                        Debug.LogWarning(e.ToString());
                    }
                    i += length - 1;
                    break;

                case 8:
                    paramaters.Add(convertToBool(array [i + 1]));
                    i += 1;
                    break;

                case 9:
                    length = convertToInt(AltimitArray.copyOfRange(array, i + 1, i + 5));
                    i     += 5;
                    try {
                        paramaters.Add(convertToUUD(AltimitArray.copyOfRange(array, i, i + length)));
                    } catch (Exception e) {
                        Debug.LogWarning(e.ToString());
                    }
                    i += length - 1;
                    break;
                }
            }

            return(paramaters);
        }
示例#2
0
        //<summary>
        // Once the data has been recieved from the socket this is ran.
        //</summary>
        private static void ReceiveCallBack(IAsyncResult ar)
        {
            threadList.Add(Thread.CurrentThread);

            byte[] key           = { 5, 9, 0, 4 };
            int    messageSize   = 0;
            int    messageOffset = 0;

            byte[] currentMessage;

            try{
                if (isConnected())
                {
                    StateObject state  = (StateObject)ar.AsyncState;
                    Socket      client = state.workSocket;

                    int buffSize = client.EndReceive(ar);

                    do
                    {
                        if (buffSize != 0 || fullMessage.Length != 0)
                        {
                            if (fullMessage.Length != 0 && buffSize != 0)
                            {
                                Array.Resize(ref fullMessage, fullMessage.Length + buffSize);
                                byte[] newMessage = new byte[buffSize];
                                newMessage = AltimitArray.copyOfRange(state.buffer, 0, buffSize);
                                Array.ConstrainedCopy(newMessage, 0, fullMessage, fullMessageSize, newMessage.Length);
                                messageSize = 0;
                            }
                            else if (buffSize != 0)
                            {
                                fullMessage = new byte[buffSize];
                                fullMessage = AltimitArray.copyOfRange(state.buffer, 0, buffSize);
                                messageSize = 0;
                            }
                            else if (fullMessage.Length != 0)
                            {
                                messageSize = 0;
                            }

                            if (messageSize == 0 && fullMessage.Length >= 4)
                            {
                                messageSize = AltimitConverter.convertToInt(AltimitArray.copyOfRange(fullMessage, 0, 4));
                                if (messageSize <= fullMessage.Length)
                                {
                                    byte[] messageKey = AltimitArray.copyOfRange(fullMessage, messageSize - 4, messageSize);
                                    if (key.SequenceEqual(messageKey))
                                    {
                                        currentMessage = new byte[messageSize - 8];
                                        messageOffset  = 4;
                                        Array.ConstrainedCopy(fullMessage, messageOffset, currentMessage, 0, currentMessage.Length);
                                        messageOffset = messageSize;

                                        new Thread(() =>
                                        {
                                            List <object> sentMessage = AltimitConverter.ReceiveConversion(currentMessage);
                                            InvokeMessage(sentMessage);
                                        }).Start();

                                        fullMessage     = AltimitArray.copyOfRange(fullMessage, messageOffset, fullMessage.Length);
                                        fullMessageSize = fullMessage.Length;

                                        buffSize = 0;
                                    }
                                    else
                                    {
                                        Debug.Log("Key was not found. Message will try to be completed in next read!");
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }while(fullMessage.Length > 0);

                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallBack), state);
                }
                else
                {
                    Disconnect();
                    return;
                }
            } catch (Exception e) {
                Debug.LogError(e.ToString());
                Disconnect();
                return;
            }
        }