-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RPC over UDP support, --port udp://192.168.1.23:8910/
Device library is https://github.com/mongoose-os-libs/rpc-udp CL: mos: RPC over UDP support, --port udp://192.168.1.23:8910/
- Loading branch information
Showing
6 changed files
with
104 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// Copyright (c) 2014-2019 Cesanta Software Limited | ||
// All rights reserved | ||
// | ||
// Licensed 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 codec | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net" | ||
|
||
"github.com/cesanta/errors" | ||
|
||
"github.com/mongoose-os/mos/common/mgrpc/frame" | ||
) | ||
|
||
const ( | ||
UDPURLScheme = "udp" | ||
) | ||
|
||
type UDPCodecOptions struct { | ||
} | ||
|
||
type udpCodec struct { | ||
conn net.Conn | ||
} | ||
|
||
func UDP(addr string) Codec { | ||
conn, err := net.Dial("udp", addr) | ||
if err != nil { | ||
return nil | ||
} | ||
return &udpCodec{conn: conn} | ||
} | ||
|
||
func (c *udpCodec) Recv(ctx context.Context) (*frame.Frame, error) { | ||
buf := make([]byte, 10000) | ||
readLen, err := c.conn.Read(buf) | ||
if err != nil { | ||
c.Close() | ||
return nil, errors.Trace(err) | ||
} | ||
var f frame.Frame | ||
if err = json.Unmarshal(buf[:readLen], &f); err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
return &f, nil | ||
} | ||
|
||
func (c *udpCodec) Send(ctx context.Context, f *frame.Frame) error { | ||
b, err := json.Marshal(f) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
_, err = c.conn.Write(b) | ||
return errors.Trace(err) | ||
} | ||
|
||
func (c *udpCodec) Close() { | ||
c.conn.Close() | ||
} | ||
|
||
func (c *udpCodec) CloseNotify() <-chan struct{} { | ||
return nil | ||
} | ||
|
||
func (c *udpCodec) MaxNumFrames() int { | ||
return -1 | ||
} | ||
|
||
func (c *udpCodec) Info() ConnectionInfo { | ||
return ConnectionInfo{ | ||
IsConnected: true, | ||
TLS: false, | ||
RemoteAddr: c.conn.RemoteAddr().String(), | ||
} | ||
} | ||
|
||
func (c *udpCodec) SetOptions(opts *Options) error { | ||
return errors.NotImplementedf("SetOptions") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,5 @@ const ( | |
tAzureDM | ||
tGCP | ||
tWatson | ||
tUDP | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.