public AsyncReply <bool> Connect(string hostname, ushort port) { var rt = new AsyncReply <bool>(); try { state = SocketState.Connecting; sock.ConnectAsync(hostname, port).ContinueWith((x) => { if (x.IsFaulted) { rt.TriggerError(x.Exception); } else { state = SocketState.Established; //OnConnect?.Invoke(); Receiver?.NetworkConnect(this); Begin(); rt.Trigger(true); } }); } catch (Exception ex) { rt.TriggerError(ex); } return(rt); }
public static unsafe AsyncReply EnumParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence) { var classId = data.GetGuid(offset); offset += 16; var index = data[offset++]; var template = Warehouse.GetTemplateByClassId((Guid)classId, TemplateType.Enum); if (template != null) { return(new AsyncReply(template.Constants[index].Value)); } else { var reply = new AsyncReply(); connection.GetTemplate((Guid)classId).Then(tmp => { reply.Trigger(tmp.Constants[index].Value); }).Error(x => reply.TriggerError(x)); return(reply); } }
public AsyncReply <int> ChildMethod(string childName) { var rt = new AsyncReply <int>(); _InvokeByArrayArguments(0, new object[] { childName }) .Then(x => rt.Trigger((int)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public AsyncReply <object> ConnectionOptional(object a1, int a2, string a3) { var rt = new AsyncReply <object>(); _InvokeByArrayArguments(4, new object[] { a1, a2, a3 }) .Then(x => rt.Trigger((object)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public AsyncReply <object> InvokeEvents(string msg) { var rt = new AsyncReply <object>(); _InvokeByArrayArguments(1, new object[] { msg }) .Then(x => rt.Trigger((object)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public AsyncReply <object> Void() { var rt = new AsyncReply <object>(); _InvokeByArrayArguments(0, new object[] { }) .Then(x => rt.Trigger((object)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public AsyncReply <bool> SendAsync(byte[] message, int offset, int length) { if (state == SocketState.Closed)// || state == SocketState.Terminated) { return(new AsyncReply <bool>(false)); } var msg = message.Clip((uint)offset, (uint)length); lock (sendLock) { if (state == SocketState.Closed)// || state == SocketState.Terminated) { return(new AsyncReply <bool>(false)); } if (!sock.Connected) { return(new AsyncReply <bool>(false)); } var rt = new AsyncReply <bool>(); if (asyncSending || held) { sendBufferQueue.Enqueue(new KeyValuePair <AsyncReply <bool>, byte[]>(rt, msg)); } else { asyncSending = true; try { currentReply = rt; sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, sendCallback, this);// null); } catch (Exception ex) { rt.TriggerError(ex); asyncSending = false; //state = SocketState.Terminated; Close(); } //sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent); } return(rt); } }
public AsyncReply <bool> SendAsync(byte[] message, int offset, int length) { var msg = message.Clip((uint)offset, (uint)length); lock (sendLock) { if (!sock.Connected) { return(new AsyncReply <bool>(false)); } var rt = new AsyncReply <bool>(); if (asyncSending || held) { sendBufferQueue.Enqueue(new KeyValuePair <AsyncReply <bool>, byte[]>(rt, msg)); } else { asyncSending = true; try { ssl.BeginWrite(msg, 0, msg.Length, SendCallback, rt);// null); } catch (Exception ex) { rt.TriggerError(ex); asyncSending = false; //state = SocketState.Terminated; Close(); } } return(rt); } }
public static unsafe AsyncReply RecordParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence) { var reply = new AsyncReply <IRecord>(); var classId = data.GetGuid(offset); offset += 16; length -= 16; var template = Warehouse.GetTemplateByClassId((Guid)classId, TemplateType.Record); var initRecord = (TypeTemplate template) => { ListParser(data, offset, length, connection, requestSequence).Then(r => { var ar = (object[])r; if (template.DefinedType != null) { var record = Activator.CreateInstance(template.DefinedType) as IRecord; for (var i = 0; i < template.Properties.Length; i++) { try { var v = Convert.ChangeType(ar[i], template.Properties[i].PropertyInfo.PropertyType); template.Properties[i].PropertyInfo.SetValue(record, v); } catch (Exception ex) { Console.WriteLine(ex); } } reply.Trigger(record); } else { var record = new Record(); for (var i = 0; i < template.Properties.Length; i++) { record.Add(template.Properties[i].Name, ar[i]); } reply.Trigger(record); } }); }; if (template != null) { initRecord(template); } else { connection.GetTemplate((Guid)classId).Then(tmp => { ListParser(data, offset, length, connection, requestSequence).Then(r => { if (tmp == null) { reply.TriggerError(new AsyncException(ErrorType.Management, (ushort)ExceptionCode.TemplateNotFound, "Template not found for record.")); } else { initRecord(tmp); } }); }).Error(x => reply.TriggerError(x)); } return(reply); }
public void SetException(Exception exception) { reply.TriggerError(exception); }