| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package hello |
| |
| import ( |
| "encoding/binary" |
| "errors" |
| ) |
| |
| import ( |
| "github.com/dubbogo/getty" |
| ) |
| |
| type PackageHandler struct{} |
| |
| func (h *PackageHandler) Read(ss getty.Session, data []byte) (interface{}, int, error) { |
| dataLen := len(data) |
| if dataLen < 4 { |
| return nil, 0, nil |
| } |
| |
| start := 0 |
| pos := start + 4 |
| pkgLen := int(binary.LittleEndian.Uint32(data[start:pos])) |
| if dataLen < pos+pkgLen { |
| return nil, pos + pkgLen, nil |
| } |
| start = pos |
| |
| pos = start + pkgLen |
| s := string(data[start:pos]) |
| |
| return s, pos, nil |
| } |
| |
| func (h *PackageHandler) Write(ss getty.Session, p interface{}) ([]byte, error) { |
| pkg, ok := p.(string) |
| if !ok { |
| log.Infof("illegal pkg:%+v", p) |
| return nil, errors.New("invalid package") |
| } |
| |
| pkgLen := int32(len(pkg)) |
| pkgStreams := make([]byte, 0, 4+len(pkg)) |
| |
| // pkg len |
| start := 0 |
| pos := start + 4 |
| binary.LittleEndian.PutUint32(pkgStreams[start:pos], uint32(pkgLen)) |
| start = pos |
| |
| // pkg |
| pos = start + int(pkgLen) |
| copy(pkgStreams[start:pos], pkg[:]) |
| |
| return pkgStreams[:pos], nil |
| } |