remove some stupid errors
merge with Xue Meng's code
add buffer lock for sender and receiver to synchronize write buffer operation