Esempio n. 1
0
        /// <summary>
        /// Called when client is reading data.
        /// </summary>
        public int Recv(long socket, byte[] buff, int len, int flags)
        {
            CommunicationManager.CheckThread();

            lock (CommunicationManager.SyncRoot)
            {
                if (socket != this.socket)
                {
                    throw new ArgumentException("socket");
                }

                // This shouldn't happen
                if (len > buff.Length)
                {
                    len = buff.Length;
                }

                ReceiveData();

                if (!DataAvailable || len == 0)
                {
                    return(0);
                }

                lock (sendSync)
                {
                    int offset = 0;

                    while (toClientBuffer.Count > 0 && offset < len)
                    {
                        byte[] data = toClientBuffer[0];
                        toClientBuffer.RemoveAt(0);

                        int spaceLeft = len - offset;
                        int dataPart  = Math.Min(data.Length, spaceLeft);

                        Array.Copy(data, 0, buff, offset, dataPart);
                        offset += dataPart;

                        if (dataPart < data.Length)
                        {
                            System.Diagnostics.Debug.Print("!!Sending part message to client!!");

                            int    left     = data.Length - dataPart;
                            byte[] dataLeft = new byte[left];

                            Array.Copy(data, dataPart, dataLeft, 0, left);

                            toClientBuffer.Insert(0, dataLeft);
                        }
                    }

                    return(offset);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Called when client is sending data to server
        /// </summary>
        public int Send(long socket, byte[] buff, int len, int flags)
        {
            CommunicationManager.CheckThread();

            lock (CommunicationManager.SyncRoot)
            {
                if (socket != this.socket)
                {
                    throw new ArgumentException("socket");
                }

                int buffLen = len;

                // Process predefined messages
                while (len > 0 && predefinedClientMsgs != null && clientMsgIndex < predefinedClientMsgs.Length)
                {
                    if (fromClientBuffer != null)
                    {
                        throw new SocketException("Trying to read predefined message but there are unprocessed data in buffer.", this, buff);
                    }

                    int msgLen = predefinedClientMsgs[clientMsgIndex];
                    int newLen = len - msgLen;

                    if (newLen < 0)
                    {
                        throw new SocketException("Part predefined messages are not supported.", this, buff);
                    }

                    byte[] msg = new byte[msgLen];
                    Array.Copy(buff, msg, msgLen);

                    byte[] newBuf = new byte[newLen];
                    Array.Copy(buff, msgLen, newBuf, 0, newLen);
                    len = newLen;

                    byte[] decryptedBuffer = clientEncryption.Decrypt(msg, msgLen);
                    ProcessMessages(decryptedBuffer, false);
                }

                if (len > 0)
                {
                    byte[] decryptedBuffer = clientEncryption.Decrypt(buff, len);
                    ProcessMessages(decryptedBuffer, false);
                }

                SendData();

                return(buffLen);
            }
        }
Esempio n. 3
0
        public void SendData()
        {
            CommunicationManager.CheckThread();

            lock (CommunicationManager.SyncRoot)
            {
                lock (sendSync)
                {
                    while (toServerBuffer.Count > 0)
                    {
                        InternalSendToServer(toServerBuffer[0]);
                        toServerBuffer.RemoveAt(0);
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Reads data from server.
        /// </summary>
        public void ReceiveData()
        {
            CommunicationManager.CheckThread();

            lock (CommunicationManager.SyncRoot)
            {
                const int BuffLen = 65536;

                byte[] encryptedBuffer = new byte[BuffLen];
                int    readBytes       = WinSock.recv(socket, encryptedBuffer, BuffLen, 0);

                if (readBytes > 0)
                {
                    CommunicationManager.BandwidthManager.Download(readBytes);

                    byte[] decryptedBuffer = serverEncryption.Decrypt(encryptedBuffer, readBytes);
                    ProcessMessages(decryptedBuffer, true);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Can be created only from main thread.
        /// </summary>
        public UltimaSocket(long s, uint addr, int port)
        {
            CommunicationManager.CheckThread();

            socket    = s;
            address   = addr;
            this.port = port;

            clientMsgIndex       = 0;
            predefinedClientMsgs = null;

            seed = 0;

            // No encryption
            clientEncryption = new Encryption(new NoEncryption(), new NoEncryption());
            serverEncryption = new Encryption(new NoEncryption(), new NoEncryption());

            toClientBuffer   = new List <byte[]>();
            toServerBuffer   = new List <byte[]>();
            fromClientBuffer = null;
            fromServerBuffer = null;
        }