Ejemplo n.º 1
0
        public void Bind()
        {
            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();

                    // Start an asynchronous socket to listen for connections.
                    log.add_to_log(log_vrste.info, "Waiting for a connection...", "AsyncTCPServer.cs Bind()");
                    listener.BeginAccept(
                        new AsyncCallback(AcceptCallback),
                        listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Ejemplo n.º 2
0
        public List <Message> EndRead(Connection connection, int bytesRead, out IOStatus status)
        {
            status = IOStatus.NONE;
            List <Message> readMessages = new List <Message>();
            Socket         socket       = connection.socket;

            byte[] messageData;
            byte[] rbuffer = connection.GetRBuffer();

            log.add_to_log(log_vrste.info, String.Format("Handler {0}: buffer={1}", connection.uid, ByteArrayToString(rbuffer)), "InputOutput.cs EndRead()");
            log.add_to_log(log_vrste.info, String.Format("Handler {0}: read {1} bytes", connection.uid, bytesRead), "InputOutput.cs EndRead()");
            log.add_to_log(log_vrste.info, String.Format("Handler {0}: read {1}", connection.uid, ByteArrayToString(new ArraySegment <byte>(connection.tmp_rbuffer, 0, bytesRead).ToArray())), "InputOutput.cs EndRead()");

            if (bytesRead == 0)
            {
                throw new Exception("Client disconnected");
            }

            connection.WriteRBuffer(connection.tmp_rbuffer, 0, bytesRead);
            rbuffer = connection.GetRBuffer();

            int  length = 0, left, start = 0;
            byte opcode;

            while (connection.position < rbuffer.Length)
            {
                left = (int)rbuffer.Length - connection.position;
                log.add_to_log(log_vrste.info, String.Format("Handler {0}: left={1}", connection.uid, left), "InputOutput.cs EndRead()");

                if (left >= 5)
                {
                    length = BitConverter.ToInt32(rbuffer, connection.position);
                    connection.position += INT_SIZE;
                    opcode = rbuffer[connection.position];
                    connection.position += BYTE_SIZE;

                    if ((left - (FIVE_BYTES)) >= length)
                    {
                        start                = FIVE_BYTES;
                        messageData          = new ArraySegment <byte>(rbuffer, start, length).ToArray();
                        connection.position += length;

                        log.add_to_log(log_vrste.info, String.Format("Handler {0}: read message, opcode={1}, length={2}, data={3}", connection.uid, opcode, length, ByteArrayToString(messageData)), "InputOutput.cs EndRead()");
                        log.add_to_log(log_vrste.info, String.Format("Handler {0}: at position: {1}", connection.uid, connection.position), "InputOutput.cs EndRead()");
                        readMessages.Add(new Message(opcode, messageData));

                        if (rbuffer.Length > (length + start))
                        {
                            log.add_to_log(log_vrste.info, String.Format("Handler {0}: got parts of new message.", connection.uid), "InputOutput.cs EndRead()");
                            log.add_to_log(log_vrste.info, String.Format("Handler {0}: current buffer={1}", connection.uid, ByteArrayToString(rbuffer)), "InputOutput.cs EndRead()");

                            byte[] tmp = new ArraySegment <byte>(rbuffer, length + start, rbuffer.Length - (length + start)).ToArray();
                            connection.ResetRBuffer();
                            connection.WriteRBuffer(tmp, 0, tmp.Length);
                            rbuffer             = connection.GetRBuffer();
                            connection.position = 0;
                            log.add_to_log(log_vrste.info, String.Format("Handler {0}: current buffer length is {1}", connection.uid, rbuffer.Length), "InputOutput.cs EndRead()");
                        }
                        else
                        {
                            log.add_to_log(log_vrste.info, String.Format("Handler {0}: reset buffer", connection.uid), "InputOutput.cs EndRead()");
                            connection.position = 0;
                            connection.ResetRBuffer();
                            break;
                        }
                    }
                    //nema podataka pa utrpaj ostatak u buffer
                    else
                    {
                        log.add_to_log(log_vrste.info, String.Format("Handler {0}: opcode={1}, length={2}. Whole message not yet received", connection.uid, opcode, length), "InputOutput.cs EndRead()");
                        log.add_to_log(log_vrste.info, String.Format("Handler {0}: at position: {1}", connection.uid, connection.position), "InputOutput.cs EndRead()");
                        log.add_to_log(log_vrste.info, String.Format("Handler {0}: message not complete", connection.uid), "InputOutput.cs EndRead()");

                        status = IOStatus.INCOMPLETE;

                        connection.position = 0;
                        break;
                    }
                }
                //nema headera, utrpaj ostatak u buffer
                else
                {
                    log.add_to_log(log_vrste.info, String.Format("Handler {0}: message not complete, put received back to buffer", connection.uid), "InputOutput.cs EndRead()");
                    break;
                }
            }

            log.add_to_log(log_vrste.info, String.Format("Handler {0}: buffer: {1}", connection.uid, ByteArrayToString(rbuffer)), "InputOutput.cs EndRead()");

            if (status == IOStatus.NONE)
            {
                status = IOStatus.COMPLETE;
            }

            connection.tmp_rbuffer = new byte[Connection.RBUFFER_SIZE];
            return(readMessages);
        }