private void ConnectCallback(IAsyncResult ar) { Socket connection = (Socket)ar.AsyncState; this.connection = new Connection(connection); /*io.AddMessageToWriteBuffer(this.connection, new Message(24, new byte[] { 1, 2, 3, 4,5,6 })); * Send(new Message(24, new byte[] { 1, 2, 3, 4,5,6 }));*/ connected.Set(); log.add_to_log(log_vrste.info, "BeginReceive", "AsyncTCPClient.cs ConnectCallback()"); this.connection.socket.BeginReceive(this.connection.tmp_rbuffer, 0, Connection.RBUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReadCallback), this.connection); }
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); }