Beispiel #1
0
func (this *Array) RemoveObjectWithRange(start int, end int) Array {
	*this = sliceExt.SliceRemoveWithRange(*this, start, end).(Array)
	return *this
}
Beispiel #2
0
func (this *Array) RemoveAllObjects() Array {
	*this = sliceExt.SliceRemoveWithRange(*this, 0, this.Len()).(Array)
	return *this
}
func (this *TcpServer) readClient(conn net.Conn, reader *bufio.Reader) error {

	readBuffer := make([]byte, this.packageBufferSize)
	var readBufferWithSavedBytes []byte

	//一次性读出全部报文,如果缓冲区大小不够,系统会多次回调这里
	bufferReadLen, err := reader.Read(readBuffer[0:this.packageBufferSize])
	readBuffer = sliceExt.SliceRemoveWithRange(readBuffer, bufferReadLen, cap(readBuffer)).([]byte)

	/*
		read函数阻塞在这里,可以获取到客户端断开之类的error信息
		所有的delegate,都不要直接return,否则,read拿不到error的信息
	*/
	if nil != err {
		//客户端强行断开
		if strings.Contains(err.Error(), "connection reset by peer") {
			if nil != this.delegate {
				_, implemented := this.delegate.(ITcpServerDelegate)
				if implemented {
					this.delegate.TcpServerClientDisConnected(this.getClientInfoMapClient(conn))
				}
			}
			this.DisConnect(this.getClientInfoMapClient(conn))
			this.doCleanWorkAfterDisConnect(this.getClientInfoMapClient(conn))
			return err
		}
		if err == io.EOF {
			//对方断开连接
			if nil != this.delegate {
				_, implemented := this.delegate.(ITcpServerDelegate)
				if implemented {
					this.delegate.TcpServerClientDisConnected(this.getClientInfoMapClient(conn))
				}
			}
			this.DisConnect(this.getClientInfoMapClient(conn))
			this.doCleanWorkAfterDisConnect(this.getClientInfoMapClient(conn))
			return err
		} else if strings.Contains(err.Error(), "use of closed network connection") {
			//自己主动断开
			if nil != this.delegate {
				_, implemented := this.delegate.(ITcpServerDelegate)
				if implemented {
					this.delegate.TcpServerClientDisConnectedByServer(this.getClientInfoMapClient(conn))
				}
			}
			this.DisConnect(this.getClientInfoMapClient(conn))
			this.doCleanWorkAfterDisConnect(this.getClientInfoMapClient(conn))
			return err
		} else if strings.Contains(err.Error(), "i/o timeout") {
			//超时
			if nil != this.delegate {
				_, implemented := this.delegate.(ITcpServerDelegate)
				if implemented {
					this.delegate.TcpServerClientDisConnectedByServer(this.getClientInfoMapClient(conn))
				}
			}
			this.DisConnect(this.getClientInfoMapClient(conn))
			this.doCleanWorkAfterDisConnect(this.getClientInfoMapClient(conn))
			return err
		} else {
			//其他错误
			this.DisConnect(this.getClientInfoMapClient(conn))
			return err
		}
	} else {
		//read正常的时候
		savedBuffer := this.getClientInfoMapSavedBuffer(conn)
		readBufferWithSavedBytes = append(savedBuffer, readBuffer...)

		loopBufferPos := 0

		//循环拆解粘包
		for {
			loopBuffer := readBufferWithSavedBytes[loopBufferPos:]
			var packageHeader *PackageHeader
			packageHeader = (*PackageHeader)(unsafe.Pointer(&loopBuffer))

			//小于包头
			if len(loopBuffer) < binary.Size(packageHeader) {
				break
			}
			//大于包头,小于包长
			if len(loopBuffer) >= binary.Size(packageHeader) {
				headerDataLen := int(binary.BigEndian.Uint32(loopBuffer))

				if len(loopBuffer) < headerDataLen || headerDataLen > this.packageMaxSize {
					break
				}

				//完整数据包
				if nil != this.delegate {
					_, implemented := this.delegate.(ITcpServerDelegate)
					if implemented {
						beginPos := loopBufferPos + binary.Size(packageHeader)
						endPos := beginPos + headerDataLen
						this.delegate.TcpServerDidReceiveClientFullData(this.getClientInfoMapClient(conn), readBufferWithSavedBytes[beginPos:endPos])
					}
				}
				loopBufferPos = headerDataLen + binary.Size(packageHeader)
			}
		}
		this.removeClientInfoMapSavedBuffer(conn)
		this.addClientInfoMapSavedBuffer(conn, readBufferWithSavedBytes[loopBufferPos:])
	}

	return nil
}